From 60426d0b969e01f610f6c5c8893e00edd8aafdbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Wed, 1 Jun 2022 11:26:36 +0200 Subject: [PATCH] Support Python 3.11 and greenlet 2.0.0a2 --- 1872.patch | 167 +++++++++++++++++++++++++++++++++++++++++++++ greenlet2.patch | 43 ++++++++++++ python-gevent.spec | 18 ++++- 3 files changed, 225 insertions(+), 3 deletions(-) create mode 100644 1872.patch create mode 100644 greenlet2.patch diff --git a/1872.patch b/1872.patch new file mode 100644 index 0000000..85e0aa3 --- /dev/null +++ b/1872.patch @@ -0,0 +1,167 @@ +From 90e9169c915a640739880b55ed95f88ce21fa7b0 Mon Sep 17 00:00:00 2001 +From: Victor Stinner +Date: Tue, 1 Mar 2022 22:28:40 +0100 +Subject: [PATCH] Add Python 3.11 alpha 6 support + +* On Python 3.11a6 and newer, get the PyFrameObject structure from + the internal C API ("internal/pycore_frame.h"). +* On Python 3.9 and newer, use PyFrame_GetBack() and + PyFrame_GetCode(). +* Add frame getter and setter functions to greenlet: + + * get_f_code(frame) + * set_f_lineno(frame, lineno) + * set_f_code(frame, code) + +* greenlet.h: the CFrame type has been renamed to _PyCFrame. +--- + _setuputils.py | 4 +++ + deps/greenlet/greenlet.h | 6 +++- + src/gevent/_gevent_cgreenlet.pxd | 59 ++++++++++++++++++++++++-------- + src/gevent/greenlet.py | 7 ++-- + 4 files changed, 59 insertions(+), 17 deletions(-) + +diff --git a/_setuputils.py b/_setuputils.py +index 7257b3eea..0b14ab1f0 100644 +--- a/_setuputils.py ++++ b/_setuputils.py +@@ -244,6 +244,10 @@ def cythonize1(ext): + 'infer_types': True, + 'nonecheck': False, + }, ++ compile_time_env={ ++ 'PY39B1': sys.hexversion >= 0x030900B1, ++ 'PY311A6': sys.hexversion >= 0x030B00A6, ++ }, + # The common_utility_include_dir (not well documented) + # causes Cython to emit separate files for much of the + # static support code. Each of the modules then includes +diff --git a/deps/greenlet/greenlet.h b/deps/greenlet/greenlet.h +index 830bef8dd..f07ce1833 100644 +--- a/deps/greenlet/greenlet.h ++++ b/deps/greenlet/greenlet.h +@@ -14,6 +14,10 @@ extern "C" { + /* This is deprecated and undocumented. It does not change. */ + #define GREENLET_VERSION "1.0.0" + ++#if PY_VERSION_HEX < 0x30B00A6 ++# define _PyCFrame CFrame ++#endif ++ + typedef struct _greenlet { + PyObject_HEAD + char* stack_start; +@@ -39,7 +43,7 @@ typedef struct _greenlet { + PyObject* context; + #endif + #if PY_VERSION_HEX >= 0x30A00B1 +- CFrame* cframe; ++ _PyCFrame* cframe; + #endif + } PyGreenlet; + +diff --git a/src/gevent/_gevent_cgreenlet.pxd b/src/gevent/_gevent_cgreenlet.pxd +index cbb81a638..246773e24 100644 +--- a/src/gevent/_gevent_cgreenlet.pxd ++++ b/src/gevent/_gevent_cgreenlet.pxd +@@ -57,30 +57,61 @@ cdef extern from "Python.h": + ctypedef class types.CodeType [object PyCodeObject]: + pass + +-cdef extern from "frameobject.h": +- +- ctypedef class types.FrameType [object PyFrameObject]: +- cdef CodeType f_code +- # Accessing the f_lineno directly doesn't work. There is an accessor +- # function, PyFrame_GetLineNumber that is needed to turn the raw line number +- # into the executing line number. +- # cdef int f_lineno +- # We can't declare this in the object as an object, because it's +- # allowed to be NULL, and Cython can't handle that. +- # We have to go through the python machinery to get a +- # proper None instead, or use an inline function. +- cdef void* f_back ++IF PY311A6: ++ cdef extern from "internal/pycore_frame.h": ++ ctypedef class types._PyInterpreterFrame [object _PyInterpreterFrame]: ++ cdef CodeType f_code ++ ++ ctypedef class types.FrameType [object PyFrameObject]: ++ cdef _PyInterpreterFrame f_frame ++ # Accessing the f_lineno directly doesn't work. There is an accessor ++ # function, PyFrame_GetLineNumber that is needed to turn the raw line number ++ # into the executing line number. ++ # cdef int f_lineno ++ # We can't declare this in the object as an object, because it's ++ # allowed to be NULL, and Cython can't handle that. ++ # We have to go through the python machinery to get a ++ # proper None instead, or use an inline function. ++ cdef void* f_back ++ELSE: ++ cdef extern from "frameobject.h": ++ ctypedef class types.FrameType [object PyFrameObject]: ++ cdef CodeType f_code ++ cdef void* f_back + ++cdef extern from "frameobject.h": + int PyFrame_GetLineNumber(FrameType frame) ++ IF PY39B1: ++ CodeType PyFrame_GetCode(FrameType frame) ++ void* PyFrame_GetBack(FrameType frame) + + @cython.nonecheck(False) + cdef inline FrameType get_f_back(FrameType frame): ++ IF PY39B1: ++ f_back = PyFrame_GetBack(frame) ++ ELSE: ++ f_back = frame.f_back + if frame.f_back != NULL: +- return frame.f_back ++ return f_back + + cdef inline int get_f_lineno(FrameType frame): + return PyFrame_GetLineNumber(frame) + ++cdef inline void set_f_lineno(FrameType frame, int lineno): ++ frame.f_lineno = lineno ++ ++cdef inline CodeType get_f_code(FrameType frame): ++ IF PY39B1: ++ return PyFrame_GetCode(frame) ++ ELSE: ++ return frame.f_code ++ ++cdef inline void set_f_code(FrameType frame, CodeType code): ++ IF PY311A6: ++ frame.f_frame.f_code = code ++ ELSE: ++ frame.f_code = code ++ + cdef void _init() + + cdef class SpawnedLink: +diff --git a/src/gevent/greenlet.py b/src/gevent/greenlet.py +index bed12ed44..f925770bb 100644 +--- a/src/gevent/greenlet.py ++++ b/src/gevent/greenlet.py +@@ -58,6 +58,9 @@ + # Frame access + locals()['get_f_back'] = lambda frame: frame.f_back + locals()['get_f_lineno'] = lambda frame: frame.f_lineno ++locals()['set_f_lineno'] = lambda frame, lineno: setattr(frame, 'f_lineno', lineno) ++locals()['get_f_code'] = lambda frame: frame.f_code ++locals()['set_f_code'] = lambda frame, code: setattr(frame, 'f_code', code) + + if _PYPY: + import _continuation # pylint:disable=import-error +@@ -157,8 +160,8 @@ def _extract_stack(limit): + # Arguments are always passed to the constructor as Python objects, + # meaning we wind up boxing the f_lineno just to unbox it if we pass it. + # It's faster to simply assign once the object is created. +- older_Frame.f_code = frame.f_code +- older_Frame.f_lineno = get_f_lineno(frame) # pylint:disable=undefined-variable ++ set_f_code(older_Frame.f_code, get_f_code(frame)) ++ set_f_lineno(older_Frame.f_lineno, get_f_lineno(frame)) # pylint:disable=undefined-variable + if newer_Frame is not None: + newer_Frame.f_back = older_Frame + newer_Frame = older_Frame diff --git a/greenlet2.patch b/greenlet2.patch new file mode 100644 index 0000000..ad9ed8b --- /dev/null +++ b/greenlet2.patch @@ -0,0 +1,43 @@ +diff --git a/src/gevent/_gevent_cgreenlet.pxd b/src/gevent/_gevent_cgreenlet.pxd +index 246773e..fbc6107 100644 +--- a/src/gevent/_gevent_cgreenlet.pxd ++++ b/src/gevent/_gevent_cgreenlet.pxd +@@ -16,12 +16,14 @@ cdef InvalidSwitchError + cdef extern from "greenlet/greenlet.h": + + ctypedef class greenlet.greenlet [object PyGreenlet]: +- # Defining this as a void* means we can't access it as a python attribute +- # in the Python code; but we can't define it as a greenlet because that doesn't +- # properly handle the case that it can be NULL. So instead we inline a getparent +- # function that does the same thing as the green_getparent accessor but without +- # going through the overhead of generic attribute lookup. +- cdef void* parent ++ pass ++ ++ # Defining this as a void* means we can't access it as a python attribute ++ # in the Python code; but we can't define it as a greenlet because that doesn't ++ # properly handle the case that it can be NULL. So instead we inline a getparent ++ # function that does the same thing as the green_getparent accessor but without ++ # going through the overhead of generic attribute lookup. ++ cdef void* PyGreenlet_GET_PARENT(greenlet s) + + # These are actually macros and so must be included + # (defined) in each .pxd, as are the two functions +@@ -36,13 +38,13 @@ cdef inline greenlet getcurrent(): + cdef inline object get_generic_parent(greenlet s): + # We don't use any typed functions on the return of this, + # so save the type check by making it just an object. +- if s.parent != NULL: +- return s.parent ++ if PyGreenlet_GET_PARENT(s) != NULL: ++ return PyGreenlet_GET_PARENT(s) + + cdef inline SwitchOutGreenletWithLoop get_my_hub(greenlet s): + # Must not be called with s = None +- if s.parent != NULL: +- return s.parent ++ if PyGreenlet_GET_PARENT(s) != NULL: ++ return PyGreenlet_GET_PARENT(s) + + cdef bint _greenlet_imported + diff --git a/python-gevent.spec b/python-gevent.spec index 5da01e9..13e09cb 100644 --- a/python-gevent.spec +++ b/python-gevent.spec @@ -4,13 +4,20 @@ Name: python-%{modname} Version: 21.12.0 -Release: 1%{?dist} +Release: 2%{?dist} Summary: A coroutine-based Python networking library License: MIT URL: http://www.gevent.org/ Source0: %{pypi_source %{modname} %{version} tar.gz} +# Support Python 3.11 +Patch: https://github.com/gevent/gevent/pull/1872.patch + +# greenlet 2 has encapsulated the parent attribute: +# https://github.com/python-greenlet/greenlet/commit/5bbd0fcde8629066d1b3fc6c54dba1cc66bc9b41 +Patch: greenlet2.patch + BuildRequires: gcc BuildRequires: c-ares-devel BuildRequires: libev-devel @@ -34,14 +41,13 @@ Summary: %{summary} %{?python_provide:%python_provide python3-%{modname}} BuildRequires: python3-devel BuildRequires: python3-Cython -BuildRequires: python3-greenlet-devel >= 0.4.17 +BuildRequires: (python3-greenlet-devel >= 2~~ with python3-greenlet-devel < 3) BuildRequires: python3-setuptools # For tests BuildRequires: python3-dns BuildRequires: python3-psutil BuildRequires: python3-zope-event BuildRequires: python3-zope-interface -Requires: python3-greenlet >= 0.4.17 %description -n python3-%{modname} gevent is a coroutine-based Python networking library that uses greenlet to @@ -60,6 +66,9 @@ Python 3 version. %prep %autosetup -p1 -n %{modname}-%{version} +# Allow greenlet 2.0 only, as our patches do not support previous versions +sed -i 's/greenlet >= 1.1.0, < 2.0/greenlet == 2.0.*/' setup.py + # Remove bundled libraries rm -r deps # Upstream intentionally includes C extension sources in the built package, @@ -95,6 +104,9 @@ cd src/gevent/tests && GEVENT_FILE=thread %__python3 -mgevent.tests test__*subpr %{python3_sitearch}/%{modname}* %changelog +* Wed Jun 01 2022 Miro HronĨok - 21.12.0-2 +- Support Python 3.11 and greenlet 2.0.0a2 + * Sun Mar 06 2022 Orion Poplawski - 21.12.0-1 - Update to 21.12.0