8000 upgrade 'cythonize' and fix absolute imports to relative · numpy/numpy@0adab55 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0adab55

Browse files
committed
upgrade 'cythonize' and fix absolute imports to relative
1 parent e927920 commit 0adab55

14 files changed

+86
-66
lines changed

numpy/random/randomgen/generator.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ try:
1919
except ImportError:
2020
from dummy_threading import Lock
2121

22-
from randomgen.bounded_integers cimport *
23-
from randomgen.common cimport *
24-
from randomgen.distributions cimport *
25-
from randomgen.xoroshiro128 import Xoroshiro128
26-
import randomgen.pickle
22+
from .bounded_integers cimport *
23+
from .common cimport *
24+
from .distributions cimport *
25+
from .xoroshiro128 import Xoroshiro128
26+
from .pickle import __generator_ctor
2727

2828
np.import_array()
2929

@@ -128,7 +128,7 @@ cdef class RandomGenerator:
128128
self.state = state
129129

130130
def __reduce__(self):
131-
return (randomgen.pickle.__generator_ctor,
131+
return (__generator_ctor,
132132
(self.state['brng'],),
133133
self.state)
134134

@@ -4440,4 +4440,4 @@ uniform = _random_generator.uniform
44404440
vonmises = _random_generator.vonmises
44414441
wald = _random_generator.wald
44424442
weibull = _random_generator.weibull
4443-
zipf = _random_generator.zipf
4443+
zipf = _random_generator.zipf

numpy/random/randomgen/legacy/_legacy.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ try:
1616
except ImportError:
1717
from dummy_threading import Lock
1818

19-
from randomgen.common cimport cont, disc, CONS_NONE, CONS_POSITIVE, CONS_NON_NEGATIVE, CONS_BOUNDED_0_1
20-
from randomgen.distributions cimport brng_t
21-
from randomgen.legacy.legacy_distributions cimport *
22-
from randomgen.xoroshiro128 import Xoroshiro128
23-
import randomgen.pickle
19+
from ..common cimport cont, disc, CONS_NONE, CONS_POSITIVE, CONS_NON_NEGATIVE, CONS_BOUNDED_0_1
20+
from ..distributions cimport brng_t
21+
from .legacy_distributions cimport *
22+
from ..xoroshiro128 import Xoroshiro128
23+
from ..pickle import __legacy_ctor
2424

2525
np.import_array()
2626

@@ -119,7 +119,7 @@ cdef class _LegacyGenerator:
119119
self.state = state
120120

121121
def __reduce__(self):
122-
return (randomgen.pickle.__legacy_ctor,
122+
return (__legacy_ctor,
123123
(self.state['brng'],),
124124
self.state)
125125

numpy/random/randomgen/legacy/legacy_distributions.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from libc.stdint cimport uint64_t
55
import numpy as np
66
cimport numpy as np
77

8-
from randomgen.distributions cimport brng_t
8+
from ..distributions cimport brng_t
99

1010
cdef extern from "../src/legacy/distributions-boxmuller.h":
1111

numpy/random/randomgen/threefry32.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import numpy as np
44
from cpython.pycapsule cimport PyCapsule_New
55
from libc.stdlib cimport malloc, free
66

7-
from randomgen.common import interface
8-
from randomgen.common cimport *
9-
from randomgen.distributions cimport brng_t
10-
from randomgen.entropy import random_entropy, seed_by_array
11-
import randomgen.pickle
7+
from .common import interface
8+
from .common cimport *
9+
from .distributions cimport brng_t
10+
from .entropy import random_entropy, seed_by_array
11+
from .pickle import __brng_ctor
1212

1313
np.import_array()
1414

@@ -192,7 +192,7 @@ cdef class ThreeFry32:
192192
self.state = state
193193

194194
def __reduce__(self):
195-
return (randomgen.pickle.__brng_ctor,
195+
return (__brng_ctor,
196196
(self.state['brng'],),
197197
self.state)
198198

numpy/random/randomgen/xoroshiro128.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ from cpython.pycapsule cimport PyCapsule_New
66
import numpy as np
77
cimport numpy as np
88

9-
from randomgen.common import interface
10-
from randomgen.common cimport *
11-
from randomgen.distributions cimport brng_t
12-
from randomgen.entropy import random_entropy, seed_by_array
13-
import randomgen.pickle
9+
from .common import interface
10+
from .common cimport *
11+
from .distributions cimport brng_t
12+
from .entropy import random_entropy, seed_by_array
13+
from .pickle import __brng_ctor
1414

1515
np.import_array()
1616

@@ -154,7 +154,7 @@ cdef class Xoroshiro128:
154154
self.state = state
155155

156156
def __reduce__(self):
157-
return (randomgen.pickle.__brng_ctor,
157+
return (__brng_ctor,
158158
(self.state['brng'],),
159159
self.state)
160160

@@ -353,4 +353,4 @@ cdef class Xoroshiro128:
353353
if self._generator is None:
354354
from .generator import RandomGenerator
355355
self._generator = RandomGenerator(self)
356-
return self._generator
356+
return self._generator

numpy/random/randomgen/xorshift1024.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ from cpython.pycapsule cimport PyCapsule_New
66
import numpy as np
77
cimport numpy as np
88

9-
from randomgen.common import interface
10-
from randomgen.common cimport *
11-
from randomgen.distributions cimport brng_t
12-
from randomgen.entropy import random_entropy, seed_by_array
13-
import randomgen.pickle
9+
from .common import interface
10+
from .common cimport *
11+
from .distributions cimport brng_t
12+
from .entropy import random_entropy, seed_by_array
13+
from .pickle import __brng_ctor
1414

1515
np.import_array()
1616

@@ -160,7 +160,7 @@ cdef class Xorshift1024:
160160
self.state = state
161161

162162
def __reduce__(self):
163-
return (randomgen.pickle.__brng_ctor,
163+
return (__brng_ctor,
164164
(self.state['brng'],),
165165
self.state)
166166

@@ -386,4 +386,4 @@ cdef class Xorshift1024:
386386
if self._generator is None:
387387
from .generator import RandomGenerator
388388
self._generator = RandomGenerator(self)
389-
return self._generator
389+
return self._generator

numpy/random/randomgen/xoshiro256starstar.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ from cpython.pycapsule cimport PyCapsule_New
66
import numpy as np
77
cimport numpy as np
88

9-
from randomgen.common import interface
10-
from randomgen.common cimport *
11-
from randomgen.distributions cimport brng_t
12-
from randomgen.entropy import random_entropy, seed_by_array
13-
import randomgen.pickle
9+
from .common import interface
10+
from .common cimport *
11+
from .distributions cimport brng_t
12+
from .entropy import random_entropy, seed_by_array
13+
from .pickle import __brng_ctor
1414

1515
np.import_array()
1616

@@ -154,7 +154,7 @@ cdef class Xoshiro256StarStar:
154154
self.state = state
155155

156156
def __reduce__(self):
157-
return (randomgen.pickle.__brng_ctor,
157+
return (__brng_ctor,
158158
(self.state['brng'],),
159159
self.state)
160160

@@ -359,4 +359,4 @@ cdef class Xoshiro256StarStar:
359359
if self._generator is None:
360360
from .generator import RandomGenerator
361361
self._generator = RandomGenerator(self)
362-
return self._generator
362+
return self._generator

numpy/random/setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import os
66
import platform
7+
import struct
78
from distutils.dep_util import newer
89
from distutils.msvccompiler import get_build_version as get_msvc_build_version
910

@@ -123,8 +124,8 @@ def generate_libraries(ext, build_dir):
123124
config.add_extension('randomgen.dsfmt',
124125
sources=[join('randomgen', x) for x in
125126
['dsfmt.c', 'src/dsfmt/dSFMT.c',
126-
'src/dsfmt/DSFMT-jump.c',
127-
'src/aligned_malloc/allignd_malloc.c']],
127+
'src/dsfmt/dSFMT-jump.c',
128+
'src/aligned_malloc/aligned_malloc.c']],
128129
include 10000 _dirs=[join('randomgen', 'src', 'dsfmt')],
129130
libraries=EXTRA_LIBRARIES,
130131
extra_compile_args=EXTRA_COMPILE_ARGS,
@@ -139,7 +140,7 @@ def generate_libraries(ext, build_dir):
139140
['legacy/_legacy.c',
140141
'src/legacy/distributions-boxmuller.c',
141142
'src/distributions/distributions.c' ]],
142-
include_dirs=[join('randomgen', 'legacy')],
143+
include_dirs=['randomgen', join('randomgen', 'legacy')],
143144
libraries=EXTRA_LIBRARIES,
144145
extra_compile_args=EXTRA_COMPILE_ARGS,
145146
extra_link_args=EXTRA_LINK_ARGS,

tools/cythonize.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ def process_tempita_pyx(fromfile, tofile):
9999
process_pyx(pyxfile, tofile)
100100

101101

102+
def process_tempita_pyd(fromfile, tofile):
103+
import npy_tempita as tempita
104+
105+
assert fromfile.endswith('.pxd.in')
106+
assert tofile.endswith('.pxd')
107+
with open(fromfile, "r") as f:
108+
tmpl = f.read()
109+
pyxcontent = tempita.sub(tmpl)
110+
with open(tofile, "w") as f:
111+
f.write(pyxcontent)
112+
102113
def process_tempita_pxi(fromfile, tofile):
103114
import npy_tempita as tempita
104115

@@ -110,10 +121,24 @@ def process_tempita_pxi(fromfile, tofile):
110121
with open(tofile, "w") as f:
111122
f.write(pyxcontent)
112123

124+
def process_tempita_pxd(fromfile, tofile):
125+
import npy_tempita as tempita
126+
127+
assert fromfile.endswith('.pxd.in')
128+
assert tofile.endswith('.pxd')
129+
with open(fromfile, "r") as f:
130+
tmpl = 10000 f.read()
131+
pyxcontent = tempita.sub(tmpl)
132+
with open(tofile, "w") as f:
133+
f.write(pyxcontent)
134+
113135
rules = {
114-
# fromext : function
115-
'.pyx' : process_pyx,
116-
'.pyx.in' : process_tempita_pyx
136+
# fromext : function, toext
137+
'.pyx' : (process_pyx, '.c'),
138+
'.pyx.in' : (process_tempita_pyx, '.c'),
139+
'.pxi.in' : (process_tempita_pxi, '.pxi'),
140+
'.pxd.in' : (process_tempita_pxd, '.pxd'),
141+
'.pyd.in' : (process_tempita_pyd, '.pyd'),
117142
}
118143
#
119144
# Hash db
@@ -183,34 +208,28 @@ def find_process_files(root_dir):
183208
# .pxi or .pxi.in files are most likely dependencies for
184209
# .pyx files, so we need to process them first
185210
files.sort(key=lambda name: (name.endswith('.pxi') or
186-
name.endswith('.pxi.in')),
211+
name.endswith('.pxi.in') or
212+
name.endswith('.pxd.in')),
187213
reverse=True)
188214

189215
for filename in files:
190216
in_file = os.path.join(cur_dir, filename + ".in")
191-
if filename.endswith('.pyx') and os.path.isfile(in_file):
192-
continue
193-
elif filename.endswith('.pxi.in'):
194-
toext = '.pxi'
195-
fromext = '.pxi.in'
196-
fromfile = filename
197-
function = process_tempita_pxi
198-
tofile = filename[:-len(fromext)] + toext
199-
process(cur_dir, fromfile, tofile, function, hash_db)
200-
save_hashes(hash_db, HASH_FILE)
201-
else:
202-
for fromext, function in rules.items():
203-
if filename.endswith(fromext):
204-
toext = ".c"
217+
for fromext, value in rules.items():
218+
if filename.endswith(fromext):
219+
if not value:
220+
break
221+
function, toext = value
222+
if toext == '.c':
205223
with open(os.path.join(cur_dir, filename), 'rb') as f:
206224
data = f.read()
207225
m = re.search(br"^\s*#\s*distutils:\s*language\s*=\s*c\+\+\s*$", data, re.I|re.M)
208226
if m:
209227
toext = ".cxx"
210-
fromfile = filename
211-
tofile = filename[:-len(fromext)] + toext
212-
process(cur_dir, fromfile, tofile, function, hash_db)
213-
save_hashes(hash_db, HASH_FILE)
228+
fromfile = filename
229+
tofile = filename[:-len(fromext)] + toext
230+
process(cur_dir, fromfile, tofile, function, hash_db)
231+
save_hashes(hash_db, HASH_FILE)
232+
break
214233

215234
def main():
216235
try:

0 commit comments

Comments
 (0)
0