forked from mesonbuild/meson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If the user has specified the `limited_api` keyword arg on an 'extension_module' invocation, then add `CYTHON_LIMITED_API` to the C and C++ definitions of the target (along with the existing `Py_LIMITED_API`). Cython's support for this functionality was added in version 3.0, so we issue a warning if the detected Cython compiler is not at least this version. Two tests have been added: one based on Cython's limited API test which aims to test that it works in the case of the user's compiler supporting this function, and another to test that a warning is issued in the opposite case.
- Loading branch information
Showing
6 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## `limited_api` support for Cython extension modules | ||
|
||
The `limited_api` keyword of the Python module's `extension_module` | ||
now supports Cython by defining the `CYTHON_LIMITED_API` symbol. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This is taken from Cython's limited API tests and | ||
# as a result is under Apache 2.0 | ||
|
||
import cython | ||
|
||
@cython.binding(False) | ||
def fib(int n): | ||
cdef int a, b | ||
a, b = 0, 1 | ||
while b < n: | ||
a, b = b, a + b | ||
return b | ||
|
||
def lsum(values): | ||
cdef long result = 0 | ||
for value in values: | ||
result += value | ||
if type(values) is list: | ||
for value in reversed(<list>values): | ||
result += value | ||
elif type(values) is tuple: | ||
for value in reversed(<tuple>values): | ||
result += value | ||
return result | ||
|
||
@cython.binding(False) | ||
def raises(): | ||
raise RuntimeError() | ||
|
||
def decode(bytes b, bytearray ba): | ||
return b.decode("utf-8") + ba.decode("utf-8") | ||
|
||
def cast_float(object o): | ||
return float(o) | ||
|
||
class C: | ||
pass | ||
|
||
cdef class D: | ||
pass | ||
|
||
cdef class E(D): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
project('cython limited api', ['cython', 'c'] | ||
# setting limited_api in debug mode on Windows yields errors. | ||
default_options: ['buildtype=release'] | ||
) | ||
|
||
cythonc = meson.get_compiler('cython') | ||
if cythonc.version() < '3.0' | ||
error('MESON_SKIP_TEST: Cython compiler version does not support limited API') | ||
endif | ||
|
||
py3 = import('python').find_installation() | ||
|
||
ext_mod = py3.extension_module('limited', | ||
'limited.pyx', | ||
limited_api: '3.7' | ||
) | ||
|
||
test('limited_api runner', | ||
py3, | ||
args : files('run.py'), | ||
env : ['PYTHONPATH=' + meson.current_build_dir()] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# This is taken from Cython's limited API tests and | ||
# as a result is under Apache 2.0 | ||
|
||
import limited | ||
|
||
limited.fib(11) | ||
|
||
assert limited.lsum(list(range(10))) == 90 | ||
assert limited.lsum(tuple(range(10))) == 90 | ||
assert limited.lsum(iter(range(10))) == 45 | ||
|
||
try: | ||
limited.raises() | ||
except RuntimeError: | ||
pass | ||
|
||
limited.C() | ||
limited.D() | ||
limited.E() | ||
|
||
assert limited.decode(b'a', bytearray(b'b')) == "ab" | ||
|
||
assert limited.cast_float(1) == 1.0 | ||
assert limited.cast_float("2.0") == 2.0 | ||
assert limited.cast_float(bytearray(b"3")) == 3.0 |