Skip to content

Commit

Permalink
BUG: make cythonization build to pass on python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
pv committed Feb 16, 2013
1 parent 8ecaed1 commit 0493cf3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include setup.py
include scipy/*.py
# Cython generation
include cythonize.dat
include tools/cythonize
include tools/cythonize.py
include scipy/cluster/_vq_rewrite.c # unused, but ship still
# Add Cython files
recursive-include scipy *.pyx *.pyx.in *.pxd *.pxi
Expand Down
2 changes: 1 addition & 1 deletion bscript
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _set_mangling_var(conf, u, du, case, f2pycompat=True):
def _generate_cython():
cwd = os.path.dirname(__file__)
subprocess.call([sys.executable,
os.path.join(cwd, 'tools', 'cythonize'),
os.path.join(cwd, 'tools', 'cythonize.py'),
os.path.join(cwd, 'scipy')])

@hooks.post_configure
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ def write_version_py(filename='scipy/version.py'):
def generate_cython():
cwd = os.path.dirname(__file__)
p = subprocess.Popen([sys.executable,
os.path.join(cwd, 'tools', 'cythonize'),
os.path.join(cwd, 'tools', 'cythonize.py'),
os.path.join(cwd, 'scipy')],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out, err = p.communicate()
if p.returncode != 0:
print(out)
print(out.decode('latin1'))
raise RuntimeError("Running cythonize failed!")


Expand Down
24 changes: 13 additions & 11 deletions tools/cythonize → tools/cythonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
operates on the Cython .pyx files.
"""

from __future__ import division, print_function, absolute_import

import os
import re
import sys
import hashlib
import cPickle
import pickle
import subprocess
from subprocess import Popen, PIPE

Expand Down Expand Up @@ -68,17 +70,17 @@ def process_pyx(fromfile, tofile):

def process_tempita_pyx(fromfile, tofile):
import tempita
with file(fromfile) as f:
with open(fromfile, "rb") as f:
tmpl = f.read()
pyxcontent = tempita.sub(tmpl)
assert fromfile.endswith('.pyx.in')
pyxfile = fromfile[:-len('.pyx.in')] + '.pyx'
with file(pyxfile, 'w') as f:
with open(pyxfile, "wb") as f:
f.write(pyxcontent)
process_pyx(pyxfile, tofile)

rules = {
# fromext : (toext, function)
# fromext : function
'.pyx' : process_pyx,
'.pyx.in' : process_tempita_pyx
}
Expand All @@ -89,7 +91,7 @@ def load_hashes(filename):
# Return { filename : (sha1 of input, sha1 of output) }
if os.path.isfile(filename):
hashes = {}
with open(filename, 'rb') as f:
with open(filename, 'r') as f:
for line in f:
filename, inhash, outhash = line.split()
hashes[filename] = (inhash, outhash)
Expand All @@ -98,13 +100,13 @@ def load_hashes(filename):
return hashes

def save_hashes(hash_db, filename):
with file(filename, 'wb') as f:
with open(filename, 'w') as f:
for key, value in sorted(hash_db.items()):
f.write("%s %s %s\n" % (key, value[0], value[1]))

def sha1_of_file(filename):
h = hashlib.sha1()
with file(filename) as f:
with open(filename, "rb") as f:
h.update(f.read())
return h.hexdigest()

Expand All @@ -128,13 +130,13 @@ def process(path, fromfile, tofile, processor_function, hash_db):
fulltopath = os.path.join(path, tofile)
current_hash = get_hash(fullfrompath, fulltopath)
if current_hash == hash_db.get(normpath(fullfrompath), None):
print '%s has not changed' % fullfrompath
print('%s has not changed' % fullfrompath)
return

orig_cwd = os.getcwd()
try:
os.chdir(path)
print 'Processing %s' % fullfrompath
print('Processing %s' % fullfrompath)
processor_function(fromfile, tofile)
finally:
os.chdir(orig_cwd)
Expand All @@ -148,12 +150,12 @@ def find_process_files(root_dir):
hash_db = load_hashes(HASH_FILE)
for cur_dir, dirs, files in os.walk(root_dir):
for filename in files:
for fromext, function in rules.iteritems():
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(r"^\s*#\s*distutils:\s*language\s*=\s*c\+\+\s*$", data, re.I|re.M)
m = re.search(br"^\s*#\s*distutils:\s*language\s*=\s*c\+\+\s*$", data, re.I|re.M)
if m:
toext = ".cxx"
fromfile = filename
Expand Down

0 comments on commit 0493cf3

Please sign in to comment.