summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorgfyoung <gfyoung17@gmail.com>2016-09-20 11:20:42 -0400
committergfyoung <gfyoung17@gmail.com>2016-09-20 20:00:27 -0400
commit7fdfa6b2c0a5da5115c523c6869898873050e41c (patch)
treef2506ce8fc900afecd9c58e6548cd70cbd6d8e87 /tools
parent55ece5839d3e9327de7a23ed346a12ec9526899d (diff)
downloadpython-numpy-7fdfa6b2c0a5da5115c523c6869898873050e41c.tar.gz
python-numpy-7fdfa6b2c0a5da5115c523c6869898873050e41c.tar.bz2
python-numpy-7fdfa6b2c0a5da5115c523c6869898873050e41c.zip
MAINT: Add Tempita to randint helpers
Refactors the randint helpers to use a Tempita template. This will reduce technical debt in the long run.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/cythonize.py57
1 files changed, 45 insertions, 12 deletions
diff --git a/tools/cythonize.py b/tools/cythonize.py
index 2db0cbd52..1085f2a91 100755
--- a/tools/cythonize.py
+++ b/tools/cythonize.py
@@ -100,6 +100,24 @@ def process_tempita_pyx(fromfile, tofile):
f.write(pyxcontent)
process_pyx(pyxfile, tofile)
+
+def process_tempita_pxi(fromfile, tofile):
+ try:
+ try:
+ from Cython import Tempita as tempita
+ except ImportError:
+ import tempita
+ except ImportError:
+ raise Exception('Building %s requires Tempita: '
+ 'pip install --user Tempita' % VENDOR)
+ assert fromfile.endswith('.pxi.in')
+ assert tofile.endswith('.pxi')
+ with open(fromfile, "r") as f:
+ tmpl = f.read()
+ pyxcontent = tempita.sub(tmpl)
+ with open(tofile, "w") as f:
+ f.write(pyxcontent)
+
rules = {
# fromext : function
'.pyx' : process_pyx,
@@ -170,22 +188,37 @@ def process(path, fromfile, tofile, processor_function, hash_db):
def find_process_files(root_dir):
hash_db = load_hashes(HASH_FILE)
for cur_dir, dirs, files in os.walk(root_dir):
+ # .pxi or .pxi.in files are most likely dependencies for
+ # .pyx files, so we need to process them first
+ files.sort(key=lambda name: (name.endswith('.pxi') or
+ name.endswith('.pxi.in')),
+ reverse=True)
+
for filename in files:
in_file = os.path.join(cur_dir, filename + ".in")
if filename.endswith('.pyx') and os.path.isfile(in_file):
continue
- for fromext, function in rules.items():
- if filename.endswith(fromext):
- toext = ".c"
- with open(os.path.join(cur_dir, filename), 'rb') as f:
- data = f.read()
- m = re.search(br"^\s*#\s*distutils:\s*language\s*=\s*c\+\+\s*$", data, re.I|re.M)
- if m:
- toext = ".cxx"
- fromfile = filename
- tofile = filename[:-len(fromext)] + toext
- process(cur_dir, fromfile, tofile, function, hash_db)
- save_hashes(hash_db, HASH_FILE)
+ elif filename.endswith('.pxi.in'):
+ toext = '.pxi'
+ fromext = '.pxi.in'
+ fromfile = filename
+ function = process_tempita_pxi
+ tofile = filename[:-len(fromext)] + toext
+ process(cur_dir, fromfile, tofile, function, hash_db)
+ save_hashes(hash_db, HASH_FILE)
+ else:
+ for fromext, function in rules.items():
+ if filename.endswith(fromext):
+ toext = ".c"
+ with open(os.path.join(cur_dir, filename), 'rb') as f:
+ data = f.read()
+ m = re.search(br"^\s*#\s*distutils:\s*language\s*=\s*c\+\+\s*$", data, re.I|re.M)
+ if m:
+ toext = ".cxx"
+ fromfile = filename
+ tofile = filename[:-len(fromext)] + toext
+ process(cur_dir, fromfile, tofile, function, hash_db)
+ save_hashes(hash_db, HASH_FILE)
def main():
try: