Adapt Cython for removed wstr type unicode strings in Python 3.12

Avoid using asyncio.get_event_loop

Fixes: rhbz#2155702
Fixes: rhbz#2155496
This commit is contained in:
Tomáš Hrnčiar 2023-01-18 16:06:37 +01:00
commit 288584e711
3 changed files with 295 additions and 1 deletions

217
5188.patch Normal file
View file

@ -0,0 +1,217 @@
From cafb5394bcca2772383437c6adfe1cbf40fbcca0 Mon Sep 17 00:00:00 2001
From: da-woods <dw-git@d-woods.co.uk>
Date: Sat, 31 Dec 2022 11:24:01 +0000
Subject: [PATCH 1/2] Avoid using asyncio.get_event_loop
The behaviour of creating a new event loop if one doesn't
already exist was removed in Python 3.12 alpha and was allegedly
deprecated before then.
Fixes #5183
---
tests/run/asyncio_generators.srctree | 37 ++++++++++++++----------
tests/run/py35_asyncio_async_def.srctree | 2 +-
2 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/tests/run/asyncio_generators.srctree b/tests/run/asyncio_generators.srctree
index d49b777f41..da8c39fa7d 100644
--- a/tests/run/asyncio_generators.srctree
+++ b/tests/run/asyncio_generators.srctree
@@ -24,11 +24,12 @@ setup(
import from_asyncio_import
import asyncio
+from contextlib import closing
def runloop(task):
- loop = asyncio.get_event_loop()
- result = loop.run_until_complete(task())
- assert 3 == result, result
+ with closing(asyncio.new_event_loop()) as loop:
+ result = loop.run_until_complete(task())
+ assert 3 == result, result
import sys
if sys.version_info < (3, 7):
@@ -39,11 +40,12 @@ if sys.version_info < (3, 7):
import import_asyncio
import asyncio
+from contextlib import closing
def runloop(task):
- loop = asyncio.get_event_loop()
- result = loop.run_until_complete(task())
- assert 3 == result, result
+ with closing(asyncio.new_event_loop()) as loop:
+ result = loop.run_until_complete(task())
+ assert 3 == result, result
import sys
if sys.version_info < (3, 7):
@@ -59,11 +61,12 @@ ASYNCIO_SUPPORTS_COROUTINE = sys.version_info[:2] >= (3, 5)
if ASYNCIO_SUPPORTS_COROUTINE:
import async_def
import asyncio
+ from contextlib import closing
def runloop(task):
- loop = asyncio.get_event_loop()
- result = loop.run_until_complete(task())
- assert 3 == result, result
+ with closing(asyncio.new_event_loop()) as loop:
+ result = loop.run_until_complete(task())
+ assert 3 == result, result
runloop(async_def.wait3)
@@ -77,12 +80,13 @@ ASYNCIO_SUPPORTS_COROUTINE = sys.version_info[:2] >= (3, 5)
if ASYNCIO_SUPPORTS_COROUTINE:
from async_def_future import await_future
import asyncio
+ from contextlib import closing
def runloop():
- loop = asyncio.get_event_loop()
- task, events, expected = await_future(loop)
- result = loop.run_until_complete(task())
- assert events == expected, 'expected %s, got %s' % (expected, events)
+ with closing(asyncio.new_event_loop()) as loop:
+ task, events, expected = await_future(loop)
+ result = loop.run_until_complete(task())
+ assert events == expected, 'expected %s, got %s' % (expected, events)
runloop()
@@ -91,14 +95,15 @@ if ASYNCIO_SUPPORTS_COROUTINE:
import sys
import asyncio
+from contextlib import closing
ASYNCIO_SUPPORTS_COROUTINE = sys.version_info[:2] >= (3, 5)
ASYNCIO_SUPPORTS_YIELD_FROM = sys.version_info[:2] < (3, 7)
def runloop(task):
- loop = asyncio.get_event_loop()
- result = loop.run_until_complete(task())
- assert 3 == result, result
+ with closing(asyncio.new_event_loop()) as loop:
+ result = loop.run_until_complete(task())
+ assert 3 == result, result
import import_asyncio
if ASYNCIO_SUPPORTS_YIELD_FROM:
diff --git a/tests/run/py35_asyncio_async_def.srctree b/tests/run/py35_asyncio_async_def.srctree
index 9da5560b30..457ef9dae6 100644
--- a/tests/run/py35_asyncio_async_def.srctree
+++ b/tests/run/py35_asyncio_async_def.srctree
@@ -24,7 +24,7 @@ from contextlib import closing
async def main():
await cy_test.say()
-with closing(asyncio.get_event_loop()) as loop:
+with closing(asyncio.new_event_loop()) as loop:
print("Running Python coroutine ...")
loop.run_until_complete(main())
From 8afcb52df651b26f3f95ab0ad5330d396e3c402f Mon Sep 17 00:00:00 2001
From: da-woods <dw-git@d-woods.co.uk>
Date: Sun, 1 Jan 2023 09:51:34 +0000
Subject: [PATCH 2/2] Fix on Py 3.4
---
tests/run/asyncio_generators.srctree | 27 ++++++++++++++++++---------
1 file changed, 18 insertions(+), 9 deletions(-)
diff --git a/tests/run/asyncio_generators.srctree b/tests/run/asyncio_generators.srctree
index da8c39fa7d..2a01a065bc 100644
--- a/tests/run/asyncio_generators.srctree
+++ b/tests/run/asyncio_generators.srctree
@@ -24,14 +24,15 @@ setup(
import from_asyncio_import
import asyncio
+import sys
from contextlib import closing
+new_event_loop = asyncio.new_event_loop if sys.version_info >= (3, 5) else asyncio.get_event_loop
def runloop(task):
- with closing(asyncio.new_event_loop()) as loop:
+ with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
-import sys
if sys.version_info < (3, 7):
runloop(from_asyncio_import.wait3)
@@ -40,14 +41,15 @@ if sys.version_info < (3, 7):
import import_asyncio
import asyncio
+import sys
from contextlib import closing
+new_event_loop = asyncio.new_event_loop if sys.version_info >= (3, 5) else asyncio.get_event_loop
def runloop(task):
- with closing(asyncio.new_event_loop()) as loop:
+ with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
-import sys
if sys.version_info < (3, 7):
runloop(import_asyncio.wait3)
@@ -95,13 +97,20 @@ if ASYNCIO_SUPPORTS_COROUTINE:
import sys
import asyncio
-from contextlib import closing
+from contextlib import closing, contextmanager
+new_event_loop = asyncio.new_event_loop if sys.version_info >= (3, 5) else asyncio.get_event_loop
+if sys.version_info < (3, 5):
+ # don't close loop on Py 3.4
+ @contextmanager
+ def closing(o):
+ yield o
+
ASYNCIO_SUPPORTS_COROUTINE = sys.version_info[:2] >= (3, 5)
ASYNCIO_SUPPORTS_YIELD_FROM = sys.version_info[:2] < (3, 7)
def runloop(task):
- with closing(asyncio.new_event_loop()) as loop:
+ with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
@@ -135,7 +144,7 @@ except ImportError:
try:
from collections import Generator
except ImportError:
- assert sys.version_info < (3,5), "Python 3.5+ should have collections.abc.Generator"
+ assert sys.version_info < (3, 5), "Python 3.5+ should have collections.abc.Generator"
Generator = object # easy win :)
assert isinstance(from_asyncio_import.wait3(), Generator)
@@ -148,7 +157,7 @@ except ImportError:
try:
from collections import Awaitable
except ImportError:
- assert sys.version_info < (3,5), "Python 3.5+ should have collections.abc.Awaitable"
+ assert sys.version_info < (3, 5), "Python 3.5+ should have collections.abc.Awaitable"
Awaitable = object # easy win :)
assert isinstance(async_def.wait3(), Awaitable)
@@ -159,7 +168,7 @@ except ImportError:
try:
from collections import Coroutine
except ImportError:
- assert sys.version_info < (3,5), "Python 3.5+ should have collections.abc.Coroutine"
+ assert sys.version_info < (3, 5), "Python 3.5+ should have collections.abc.Coroutine"
Coroutine = object # easy win :)
assert isinstance(async_def.wait3(), Coroutine)

View file

@ -0,0 +1,61 @@
From 6911945f2284a84e6021651df716474f97bfe100 Mon Sep 17 00:00:00 2001
From: Stefan Behnel <stefan_ml@behnel.de>
Date: Wed, 18 Jan 2023 16:03:09 +0100
Subject: [PATCH] Adapt PEP-623 support to latest Py3.12 which removes the
wstr field in PyUnicode but kept the PyUnicode_*() macros around.
---
Cython/Utility/ModuleSetupCode.c | 28 ++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/Cython/Utility/ModuleSetupCode.c b/Cython/Utility/ModuleSetupCode.c
index f8bf885..ca9c00f 100644
--- a/Cython/Utility/ModuleSetupCode.c
+++ b/Cython/Utility/ModuleSetupCode.c
@@ -664,12 +664,12 @@ static CYTHON_INLINE void * PyThread_tss_get(Py_tss_t *key) {
#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND)
#define CYTHON_PEP393_ENABLED 1
- #if defined(PyUnicode_IS_READY)
- #define __Pyx_PyUnicode_READY(op) (likely(PyUnicode_IS_READY(op)) ? \
- 0 : _PyUnicode_Ready((PyObject *)(op)))
+ #if PY_VERSION_HEX >= 0x030C0000
+ // Py3.12 / PEP-623 removed wstr type unicode strings and all of the PyUnicode_READY() machinery.
+ #define __Pyx_PyUnicode_READY(op) (0)
#else
- // Py3.12 / PEP-623 will remove wstr type unicode strings and all of the PyUnicode_READY() machinery.
- #define __Pyx_PyUnicode_READY(op) (0)
+ #define __Pyx_PyUnicode_READY(op) (likely(PyUnicode_IS_READY(op)) ? \
+ 0 : _PyUnicode_Ready((PyObject *)(op)))
#endif
#define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_LENGTH(u)
@@ -679,16 +679,16 @@ static CYTHON_INLINE void * PyThread_tss_get(Py_tss_t *key) {
#define __Pyx_PyUnicode_DATA(u) PyUnicode_DATA(u)
#define __Pyx_PyUnicode_READ(k, d, i) PyUnicode_READ(k, d, i)
#define __Pyx_PyUnicode_WRITE(k, d, i, ch) PyUnicode_WRITE(k, d, i, ch)
- #if defined(PyUnicode_IS_READY) && defined(PyUnicode_GET_SIZE)
- #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x03090000
- // Avoid calling deprecated C-API functions in Py3.9+ that PEP-623 schedules for removal in Py3.12.
- // https://www.python.org/dev/peps/pep-0623/
- #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : ((PyCompactUnicodeObject *)(u))->wstr_length))
- #else
- #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : PyUnicode_GET_SIZE(u)))
- #endif
+ #if PY_VERSION_HEX >= 0x030C0000
+ #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_LENGTH(u))
#else
- #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_LENGTH(u))
+ #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x03090000
+ // Avoid calling deprecated C-API functions in Py3.9+ that PEP-623 schedules for removal in Py3.12.
+ // https://www.python.org/dev/peps/pep-0623/
+ #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : ((PyCompactUnicodeObject *)(u))->wstr_length))
+ #else
+ #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : PyUnicode_GET_SIZE(u)))
+ #endif
#endif
#else
#define CYTHON_PEP393_ENABLED 0
--
2.38.1

View file

@ -6,7 +6,7 @@
Name: Cython
Version: 0.29.33
Release: 1%{?dist}
Release: 2%{?dist}
Summary: Language for writing Python extension modules
License: Apache-2.0
@ -18,6 +18,16 @@ Source: https://github.com/cython/cython/archive/%{version}/Cython-%{ver
# Fixes https://bugzilla.redhat.com/2155090
Patch: emacs-docstring-wrap.patch
# Adapt PEP-623 support to latest Py3.12 which removes the wstr
# field in PyUnicode but kept the PyUnicode_*() macros around.
# https://github.com/cython/cython/commit/b4595a04a78b79dc59276a9f64aab7b884e945e8
Patch: Adapt-PEP-623-support-to-latest-Py3.12-which-removes.patch
# Avoid using asyncio.get_event_loop.
# The behaviour of creating a new event loop if one doesn't
# already exist was removed in Python 3.12
Patch: https://github.com/cython/cython/pull/5188.patch
BuildRequires: python3-devel
BuildRequires: python3-setuptools
@ -133,6 +143,12 @@ cp -p cython-mode-init.el cython-mode-init.elc %{buildroot}%{_emacs_sitestartdir
%changelog
* Wed Jan 18 2023 Tomáš Hrnčiar <thrnciar@redhat.com> - 0.29.33-2
- Adapt Cython for removed wstr type unicode strings in Python 3.12
- Avoid using asyncio.get_event_loop.
- Fixes: rhbz#2155702
- Fixes: rhbz#2155496
* Fri Feb 17 2023 Miro Hrončok <mhroncok@redhat.com> - 0.29.33-1
- Update to 0.29.33
- Fix test failures with NumPy 1.24