Skip to content

Commit

Permalink
Remove more unneeded BWC code for pre-3.7.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Jun 21, 2023
1 parent ba01d59 commit 9496f90
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 220 deletions.
56 changes: 12 additions & 44 deletions src/greenlet/greenlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2454,18 +2454,12 @@ green_setparent(BorrowedGreenlet self, BorrowedObject nparent, void* UNUSED(cont
return 0;
}

#ifdef Py_CONTEXT_H
# define GREENLET_NO_CONTEXTVARS_REASON "This build of greenlet"
#else
# define GREENLET_NO_CONTEXTVARS_REASON "This Python interpreter"
#endif

namespace greenlet
{

template<>

const OwnedObject
Greenlet::context<GREENLET_WHEN_PY37>(GREENLET_WHEN_PY37::Yes) const
Greenlet::context() const
{
using greenlet::PythonStateContext;
OwnedObject result;
Expand All @@ -2474,7 +2468,7 @@ Greenlet::context<GREENLET_WHEN_PY37>(GREENLET_WHEN_PY37::Yes) const
/* Currently running greenlet: context is stored in the thread state,
not the greenlet object. */
if (GET_THREAD_STATE().state().is_current(this->self())) {
result = PythonStateContext<G_IS_PY37>::context(PyThreadState_GET());
result = PythonStateContext::context(PyThreadState_GET());
}
else {
throw ValueError(
Expand All @@ -2492,18 +2486,8 @@ Greenlet::context<GREENLET_WHEN_PY37>(GREENLET_WHEN_PY37::Yes) const
return result;
}

template<>
const OwnedObject
Greenlet::context<GREENLET_WHEN_NOT_PY37>(GREENLET_WHEN_NOT_PY37::No) const
{
throw AttributeError(
GREENLET_NO_CONTEXTVARS_REASON
"does not support context variables"
);
}

template<>
void Greenlet::context<GREENLET_WHEN_PY37>(BorrowedObject given, GREENLET_WHEN_PY37::Yes)
void Greenlet::context(BorrowedObject given)
{
using greenlet::PythonStateContext;
if (!given) {
Expand All @@ -2526,8 +2510,8 @@ void Greenlet::context<GREENLET_WHEN_PY37>(BorrowedObject given, GREENLET_WHEN_P

/* Currently running greenlet: context is stored in the thread state,
not the greenlet object. */
OwnedObject octx = OwnedObject::consuming(PythonStateContext<G_IS_PY37>::context(tstate));
PythonStateContext<G_IS_PY37>::context(tstate, context.relinquish_ownership());
OwnedObject octx = OwnedObject::consuming(PythonStateContext::context(tstate));
PythonStateContext::context(tstate, context.relinquish_ownership());
}
else {
/* Greenlet is not running: just set context. Note that the
Expand All @@ -2536,24 +2520,14 @@ void Greenlet::context<GREENLET_WHEN_PY37>(BorrowedObject given, GREENLET_WHEN_P
}
}

template<>
void
Greenlet::context<GREENLET_WHEN_NOT_PY37>(BorrowedObject UNUSED(given), GREENLET_WHEN_NOT_PY37::No)
{
throw AttributeError(
GREENLET_NO_CONTEXTVARS_REASON
"does not support context variables"
);
}

};
}; //namespace greenlet

static PyObject*
green_getcontext(const PyGreenlet* self, void* UNUSED(context))
{
const Greenlet *const g = self->pimpl;
try {
OwnedObject result(g->context<G_IS_PY37>());
OwnedObject result(g->context());
return result.relinquish_ownership();
}
catch(const PyErrOccurred&) {
Expand All @@ -2565,15 +2539,14 @@ static int
green_setcontext(BorrowedGreenlet self, PyObject* nctx, void* UNUSED(context))
{
try {
self->context<G_IS_PY37>(nctx, G_IS_PY37::IsIt());
self->context(nctx);
return 0;
}
catch(const PyErrOccurred&) {
return -1;
}
}

#undef GREENLET_NO_CONTEXTVARS_REASON

static PyObject*
green_getframe(BorrowedGreenlet self, void* UNUSED(context))
Expand Down Expand Up @@ -2629,7 +2602,7 @@ green_repr(BorrowedGreenlet self)
? " current"
: (self->started() ? " suspended" : "");
}
result = GNative_FromFormat(
result = PyUnicode_FromFormat(
"<%s object at %p (otid=%p)%s%s%s%s>",
tp_name,
self.borrow_o(),
Expand All @@ -2641,7 +2614,7 @@ green_repr(BorrowedGreenlet self)
);
}
else {
result = GNative_FromFormat(
result = PyUnicode_FromFormat(
"<%s object at %p (otid=%p) %sdead>",
tp_name,
self.borrow_o(),
Expand Down Expand Up @@ -2815,9 +2788,6 @@ static PyNumberMethods green_as_number = {
NULL, /* nb_add */
NULL, /* nb_subtract */
NULL, /* nb_multiply */
#if PY_MAJOR_VERSION < 3
NULL, /* nb_divide */
#endif
NULL, /* nb_remainder */
NULL, /* nb_divmod */
NULL, /* nb_power */
Expand Down Expand Up @@ -3108,9 +3078,7 @@ greenlet_internal_mod_init() noexcept

m.PyAddObject("GREENLET_USE_GC", 1);
m.PyAddObject("GREENLET_USE_TRACING", 1);
// The macros are eithre 0 or 1; the 0 case can be interpreted
// the same as NULL, which is ambiguous with a pointer.
m.PyAddObject("GREENLET_USE_CONTEXT_VARS", (long)GREENLET_PY37);
m.PyAddObject("GREENLET_USE_CONTEXT_VARS", 1L);
m.PyAddObject("GREENLET_USE_STANDARD_THREADING", 1L);

OwnedObject clocks_per_sec = OwnedObject::consuming(PyLong_FromSsize_t(CLOCKS_PER_SEC));
Expand Down
70 changes: 9 additions & 61 deletions src/greenlet/greenlet_cpython_compat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,6 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"

// These enable writing template functions or classes specialized
// based on the Python version. Write both versions of the function,
// one with the WHEN version, one with the WHEN_NOT version.
// Instantiate the template using the G_IS_PY37 macro.
struct GREENLET_WHEN_PY37
{
typedef GREENLET_WHEN_PY37* Yes;
// We really just want an alias, `using Yes = IsIt`,
// but old MSVC for Py27 doesn't support that.
typedef GREENLET_WHEN_PY37* IsIt;
};

struct GREENLET_WHEN_NOT_PY37
{
typedef GREENLET_WHEN_NOT_PY37* No;
typedef GREENLET_WHEN_NOT_PY37* IsIt;
};


#if PY_VERSION_HEX >= 0x030700A3
# define GREENLET_PY37 1
typedef GREENLET_WHEN_PY37 G_IS_PY37;
#else
# define GREENLET_PY37 0
typedef GREENLET_WHEN_NOT_PY37 G_IS_PY37;
#endif


#if PY_VERSION_HEX >= 0x30A00B1
/*
Expand All @@ -48,11 +21,7 @@ We have to save and restore this as well.
# define GREENLET_USE_CFRAME 0
#endif

#if PY_VERSION_HEX >= 0x30C0000
# define GREENLET_PY312 1
#else
# define GREENLET_PY312 0
#endif


#if PY_VERSION_HEX >= 0x30B00A4
/*
Expand All @@ -72,6 +41,13 @@ Greenlet won't compile on anything older than Python 3.11 alpha 4 (see
# define GREENLET_PY311 0
#endif


#if PY_VERSION_HEX >= 0x30C0000
# define GREENLET_PY312 1
#else
# define GREENLET_PY312 0
#endif

#ifndef Py_SET_REFCNT
/* Py_REFCNT and Py_SIZE macros are converted to functions
https://bugs.python.org/issue39573 */
Expand All @@ -98,47 +74,19 @@ Greenlet won't compile on anything older than Python 3.11 alpha 4 (see
#ifndef Py_TPFLAGS_HAVE_NEWBUFFER
#define Py_TPFLAGS_HAVE_NEWBUFFER 0
#endif
#ifndef Py_TPFLAGS_HAVE_FINALIZE
#define Py_TPFLAGS_HAVE_FINALIZE 0
#endif

#ifndef Py_TPFLAGS_HAVE_VERSION_TAG
#define Py_TPFLAGS_HAVE_VERSION_TAG 0
#endif

#define G_TPFLAGS_DEFAULT Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VERSION_TAG | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_HAVE_GC

#if PY_MAJOR_VERSION >= 3
# define GNative_FromFormat PyUnicode_FromFormat
#else
# define GNative_FromFormat PyString_FromFormat
#endif

#if PY_MAJOR_VERSION >= 3
# define Greenlet_Intern PyUnicode_InternFromString
#else
# define Greenlet_Intern PyString_InternFromString
#endif

#if PY_VERSION_HEX < 0x03090000
// The official version only became available in 3.9
# define PyObject_GC_IsTracked(o) _PyObject_GC_IS_TRACKED(o)
#endif

#if PY_MAJOR_VERSION < 3
struct PyModuleDef {
int unused;
const char* const m_name;
const char* m_doc;
Py_ssize_t m_size;
PyMethodDef* m_methods;
// Then several more fields we're not currently using.
};
#define PyModuleDef_HEAD_INIT 1
PyObject* PyModule_Create(PyModuleDef* m)
{
return Py_InitModule(m->m_name, m->m_methods);
}
#endif

// bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
Expand Down

0 comments on commit 9496f90

Please sign in to comment.