| 1 |
# mode: run |
| 2 |
# tag: limited-api |
| 3 |
|
| 4 |
# This is a bare minimum test to test compilation of the limited |
| 5 |
# API with the Py_LIMITED_API macro defined, with an example that's |
| 6 |
# known and working. It should be dropped once the limited API is |
| 7 |
# better tested |
| 8 |
|
| 9 |
UNSET CFLAGS |
| 10 |
PYTHON setup.py build_ext --inplace |
| 11 |
PYTHON run.py limited_single_phase |
| 12 |
PYTHON run.py limited_single_phase_modstate |
| 13 |
PYTHON run.py limited_multi_phase |
| 14 |
|
| 15 |
|
| 16 |
##################### setup.py ################################ |
| 17 |
|
| 18 |
import sysconfig |
| 19 |
from setuptools import setup |
| 20 |
from Cython.Build import cythonize |
| 21 |
|
| 22 |
if sysconfig.get_config_var("Py_GIL_DISABLED"): |
| 23 |
print("Skipping isolated_limited_api tests on free-threading build") |
| 24 |
exit(0) # At least for 3.13, freethreading and limited API don't mix |
| 25 |
|
| 26 |
modules = [ |
| 27 |
"limited_single_phase.pyx", |
| 28 |
"limited_single_phase_modstate.pyx", |
| 29 |
"limited_multi_phase.pyx" |
| 30 |
] |
| 31 |
|
| 32 |
setup( |
| 33 |
ext_modules = cythonize(modules), |
| 34 |
) |
| 35 |
|
| 36 |
##################### run.py ################################## |
| 37 |
|
| 38 |
import decimal |
| 39 |
|
| 40 |
import weakref |
| 41 |
import sys |
| 42 |
import sysconfig |
| 43 |
|
| 44 |
if sysconfig.get_config_var("Py_GIL_DISABLED"): |
| 45 |
print("Skipping isolated_limited_api tests on free-threading build") |
| 46 |
exit(0) # At least for 3.13, freethreading and limited API don't mix |
| 47 |
|
| 48 |
module_name = sys.argv[1] |
| 49 |
|
| 50 |
limited = __import__(module_name) |
| 51 |
|
| 52 |
limited.fib(11) |
| 53 |
|
| 54 |
assert limited.lsum(list(range(10))) == 90 |
| 55 |
assert limited.lsum(tuple(range(10))) == 90 |
| 56 |
assert limited.lsum(iter(range(10))) == 45 |
| 57 |
|
| 58 |
try: |
| 59 |
limited.raises() |
| 60 |
except RuntimeError: |
| 61 |
pass |
| 62 |
|
| 63 |
limited.catches() |
| 64 |
|
| 65 |
limited.C() |
| 66 |
limited.D() |
| 67 |
limited.E() |
| 68 |
if sys.version_info >= (3, 9): |
| 69 |
# Weak-referenceable classes won't work in Python <3.9 because |
| 70 |
# of an interpreter bug (https://github.com/python/cpython/issues/82321). |
| 71 |
# This depends on the interpreter version, not the limited API version. |
| 72 |
weakref.ref(limited.D()) |
| 73 |
weakref.ref(limited.E()) |
| 74 |
|
| 75 |
assert limited.C.cm() == limited.C().cm() == limited.C |
| 76 |
assert limited.D.cm() == limited.D().cm() == limited.D |
| 77 |
|
| 78 |
assert limited.decode(b'a', bytearray(b'b')) == "ab" |
| 79 |
|
| 80 |
assert limited.cast_float(1) == 1.0 |
| 81 |
assert limited.cast_float("2.0") == 2.0 |
| 82 |
assert limited.cast_float(bytearray(b"3")) == 3.0 |
| 83 |
|
| 84 |
gen = limited.my_generator(1) |
| 85 |
assert next(gen) == 1 |
| 86 |
assert next(gen) == 1 |
| 87 |
try: |
| 88 |
next(gen) |
| 89 |
except StopIteration: |
| 90 |
pass |
| 91 |
else: |
| 92 |
assert False # should have finished |
| 93 |
|
| 94 |
gen = limited.my_generator("raise") |
| 95 |
try: |
| 96 |
next(gen) |
| 97 |
except RuntimeError: |
| 98 |
pass |
| 99 |
else: |
| 100 |
assert False # should have raised |
| 101 |
|
| 102 |
assert limited.add_one(2) == 3 |
| 103 |
assert limited.float_equals(1.5) |
| 104 |
assert not limited.float_equals(2) |
| 105 |
assert not limited.float_equals(None) |
| 106 |
assert limited.float_equals(decimal.Decimal("1.5")) |
| 107 |
|
| 108 |
|
| 109 |
##################### limited_single_phase.pyx ################ |
| 110 |
|
| 111 |
# distutils: extra_compile_args = -DPy_LIMITED_API=0x03070000 -DCYTHON_PEP489_MULTI_PHASE_INIT=0 |
| 112 |
|
| 113 |
include 'limited.pxi' |
| 114 |
|
| 115 |
##################### limited_multi_phase.pyx ################ |
| 116 |
|
| 117 |
# distutils: extra_compile_args = -DPy_LIMITED_API=0x03070000 -DCYTHON_PEP489_MULTI_PHASE_INIT=1 |
| 118 |
|
| 119 |
include 'limited.pxi' |
| 120 |
|
| 121 |
##################### limited_single_phase_modstate.pyx ################ |
| 122 |
|
| 123 |
# distutils: extra_compile_args = -DPy_LIMITED_API=0x03070000 -DCYTHON_PEP489_MULTI_PHASE_INIT=0 -DCYTHON_USE_MODULE_STATE=1 |
| 124 |
|
| 125 |
include 'limited.pxi' |
| 126 |
|
| 127 |
##################### limited.pxi ############################# |
| 128 |
|
| 129 |
import cython |
| 130 |
|
| 131 |
# Cimporting from cpython imports a range of types with their internals exposed. |
| 132 |
# These can't necessarily be used in the limited API but simply cimporting them |
| 133 |
# shouldn't break compilation. |
| 134 |
cimport cpython |
| 135 |
cimport cpython.bool |
| 136 |
cimport cpython.complex |
| 137 |
cimport cpython.descr |
| 138 |
cimport cpython.longintrepr |
| 139 |
cimport cpython.type |
| 140 |
|
| 141 |
@cython.binding(False) |
| 142 |
def fib(int n): |
| 143 |
cdef int a, b |
| 144 |
a, b = 0, 1 |
| 145 |
while b < n: |
| 146 |
a, b = b, a + b |
| 147 |
return b |
| 148 |
|
| 149 |
def lsum(values): |
| 150 |
cdef long result = 0 |
| 151 |
for value in values: |
| 152 |
result += value |
| 153 |
if type(values) is list: |
| 154 |
for value in reversed(<list>values): |
| 155 |
result += value |
| 156 |
elif type(values) is tuple: |
| 157 |
for value in reversed(<tuple>values): |
| 158 |
result += value |
| 159 |
return result |
| 160 |
|
| 161 |
def add_one(value): |
| 162 |
return value + 1 |
| 163 |
|
| 164 |
@cython.binding(False) |
| 165 |
def raises(): |
| 166 |
raise RuntimeError() |
| 167 |
|
| 168 |
def decode(bytes b, bytearray ba): |
| 169 |
return b.decode("utf-8") + ba.decode("utf-8") |
| 170 |
|
| 171 |
def catches(): |
| 172 |
try: |
| 173 |
raises() |
| 174 |
except Exception: |
| 175 |
pass |
| 176 |
|
| 177 |
def cast_float(object o): |
| 178 |
return float(o) |
| 179 |
|
| 180 |
def float_equals(o): |
| 181 |
return o == 1.5 |
| 182 |
|
| 183 |
class C: |
| 184 |
@classmethod |
| 185 |
def cm(cls): |
| 186 |
return cls |
| 187 |
|
| 188 |
cdef class D: |
| 189 |
cdef object __weakref__ |
| 190 |
|
| 191 |
@classmethod |
| 192 |
def cm(cls): |
| 193 |
return cls |
| 194 |
|
| 195 |
cdef class E(D): |
| 196 |
pass |
| 197 |
|
| 198 |
def my_generator(x): |
| 199 |
if x == "raise": |
| 200 |
raise RuntimeError |
| 201 |
yield x |
| 202 |
yield x |