summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/distutils/command/build_src.py41
-rw-r--r--numpy/distutils/tests/pyrex_ext/__init__.py0
-rw-r--r--numpy/distutils/tests/pyrex_ext/primes.pyx22
-rw-r--r--numpy/distutils/tests/pyrex_ext/setup.py11
-rw-r--r--numpy/distutils/tests/pyrex_ext/tests/test_primes.py13
5 files changed, 87 insertions, 0 deletions
diff --git a/numpy/distutils/command/build_src.py b/numpy/distutils/command/build_src.py
index d3f926ffa..cc22115c7 100644
--- a/numpy/distutils/command/build_src.py
+++ b/numpy/distutils/command/build_src.py
@@ -214,6 +214,8 @@ class build_src(build_ext.build_ext):
sources = self.f2py_sources(sources, ext)
+ sources = self.pyrex_sources(sources, ext)
+
sources, py_files = self.filter_py_files(sources)
if not self.py_modules_dict.has_key(package):
@@ -328,6 +330,45 @@ class build_src(build_ext.build_ext):
new_sources.append(source)
return new_sources
+ def pyrex_sources(self, sources, extension):
+ have_pyrex = False
+ try:
+ import Pyrex
+ have_pyrex = True
+ except ImportError:
+ pass
+ new_sources = []
+ ext_name = extension.name.split('.')[-1]
+ for source in sources:
+ (base, ext) = os.path.splitext(source)
+ if ext == '.pyx':
+ if self.inplace or not have_pyrex:
+ target_dir = os.path.dirname(base)
+ else:
+ target_dir = appendpath(self.build_src, os.path.dirname(base))
+ target_file = os.path.join(target_dir, ext_name + '.c')
+ depends = [source] + extension.depends
+ if (self.force or newer_group(depends, target_file, 'newer')):
+ if have_pyrex:
+ log.info("pyrexc:> %s" % (target_file))
+ self.mkpath(target_dir)
+ from Pyrex.Compiler import Main
+ options = Main.CompilationOptions(
+ defaults=Main.default_options,
+ output_file=target_file)
+ pyrex_result = Main.compile(source, options=options)
+ if pyrex_result.num_errors != 0:
+ raise RuntimeError("%d errors in Pyrex compile" %
+ pyrex_result.num_errors)
+ else:
+ log.info("Pyrex needed to compile %s but not available."\
+ " Using old target %s"\
+ % (source, target_file))
+ new_sources.append(target_file)
+ else:
+ new_sources.append(source)
+ return new_sources
+
def f2py_sources(self, sources, extension):
new_sources = []
f2py_sources = []
diff --git a/numpy/distutils/tests/pyrex_ext/__init__.py b/numpy/distutils/tests/pyrex_ext/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/numpy/distutils/tests/pyrex_ext/__init__.py
diff --git a/numpy/distutils/tests/pyrex_ext/primes.pyx b/numpy/distutils/tests/pyrex_ext/primes.pyx
new file mode 100644
index 000000000..2ada0c5a0
--- /dev/null
+++ b/numpy/distutils/tests/pyrex_ext/primes.pyx
@@ -0,0 +1,22 @@
+#
+# Calculate prime numbers
+#
+
+def primes(int kmax):
+ cdef int n, k, i
+ cdef int p[1000]
+ result = []
+ if kmax > 1000:
+ kmax = 1000
+ k = 0
+ n = 2
+ while k < kmax:
+ i = 0
+ while i < k and n % p[i] <> 0:
+ i = i + 1
+ if i == k:
+ p[k] = n
+ k = k + 1
+ result.append(n)
+ n = n + 1
+ return result
diff --git a/numpy/distutils/tests/pyrex_ext/setup.py b/numpy/distutils/tests/pyrex_ext/setup.py
new file mode 100644
index 000000000..02b5d26f1
--- /dev/null
+++ b/numpy/distutils/tests/pyrex_ext/setup.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+def configuration(parent_package='',top_path=None):
+ from numpy.distutils.misc_util import Configuration
+ config = Configuration('pyrex_ext',parent_package,top_path)
+ config.add_extension('primes',
+ ['primes.pyx'])
+ return config
+
+if __name__ == "__main__":
+ from numpy.distutils.core import setup
+ setup(**configuration(top_path='').todict())
diff --git a/numpy/distutils/tests/pyrex_ext/tests/test_primes.py b/numpy/distutils/tests/pyrex_ext/tests/test_primes.py
new file mode 100644
index 000000000..4d3e01d81
--- /dev/null
+++ b/numpy/distutils/tests/pyrex_ext/tests/test_primes.py
@@ -0,0 +1,13 @@
+import sys
+from numpy.testing import *
+
+set_package_path()
+from pyrex_ext.primes import primes
+restore_path()
+
+class test_primes(ScipyTestCase):
+ def check_simple(self, level=1):
+ l = primes(10)
+ assert_equal(l, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29])
+if __name__ == "__main__":
+ ScipyTest().run()