| 1 |
# tag: numpy |
| 2 |
|
| 3 |
""" |
| 4 |
CYTHONIZE -M dep_np.pyx |
| 5 |
PYTHON check_np.py |
| 6 |
""" |
| 7 |
|
| 8 |
######## dep_np.pyx ######## |
| 9 |
|
| 10 |
cimport numpy as np |
| 11 |
np.import_array() |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
######## check_np.py ######## |
| 16 |
|
| 17 |
import os.path |
| 18 |
import re |
| 19 |
|
| 20 |
import numpy as np |
| 21 |
import Cython |
| 22 |
|
| 23 |
def safe_relpath(path, start=None): |
| 24 |
try: |
| 25 |
result = os.path.relpath(path, start) |
| 26 |
except ValueError: |
| 27 |
# if they are on different Windows drives, absolute is fine |
| 28 |
result = os.path.abspath(path) |
| 29 |
|
| 30 |
return result |
| 31 |
|
| 32 |
with open("dep_np.c.dep", "r") as f: |
| 33 |
contents = f.read().replace('\\\n', ' ').replace('\n', ' ') |
| 34 |
|
| 35 |
contents = contents.split() |
| 36 |
|
| 37 |
cy_prefix = os.path.dirname(safe_relpath(Cython.__file__)) |
| 38 |
contents = [fname.replace(cy_prefix, "cy_prefix") for fname in contents] |
| 39 |
|
| 40 |
np_prefix = os.path.dirname(safe_relpath(np.__file__, '.')) |
| 41 |
contents = [fname.replace(np_prefix, "np_prefix") for fname in contents] |
| 42 |
|
| 43 |
# filter out the version number from `np_prefix/__init__.cython-30.pxd`. |
| 44 |
contents = [re.sub('[.]cython-[0-9]+', '', entry) for entry in contents] |
| 45 |
|
| 46 |
contents = [path.split(os.sep) for path in contents] |
| 47 |
contents.sort() |
| 48 |
|
| 49 |
expected = [path.split('/') for path in [ |
| 50 |
'cy_prefix/Includes/cpython/object.pxd', |
| 51 |
'cy_prefix/Includes/cpython/ref.pxd', |
| 52 |
'cy_prefix/Includes/cpython/type.pxd', |
| 53 |
'cy_prefix/Includes/libc/stdio.pxd', |
| 54 |
'cy_prefix/Includes/libc/string.pxd', |
| 55 |
'dep_np.c:', |
| 56 |
'dep_np.pyx', |
| 57 |
]] |
| 58 |
|
| 59 |
# Also account for legacy numpy versions, which do not ship |
| 60 |
# `__init__.pxd` hence the fallback is used: |
| 61 |
if ['cy_prefix', 'Includes', 'numpy', '__init__.pxd'] in contents: |
| 62 |
expected.append(['cy_prefix', 'Includes', 'numpy', '__init__.pxd']) |
| 63 |
else: |
| 64 |
expected.append(['np_prefix', '__init__.pxd']) |
| 65 |
|
| 66 |
expected.sort() |
| 67 |
assert contents == expected, contents |