make pyxbuild.py Py3 compatible with .c compile fallback if Cython import fails
authorStefan Behnel <scoder@users.berlios.de>
Sun, 22 Feb 2009 18:57:11 +0000 (19:57 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sun, 22 Feb 2009 18:57:11 +0000 (19:57 +0100)
pyximport/pyxbuild.py

index 50a6c3c868e586379aca3de7211c591b065f5307..0fb296be9ed8aa7ad93391379f54f0b5b4a5a3f6 100644 (file)
@@ -4,13 +4,18 @@ the installed distutils infrastructure. Call:
 out_fname = pyx_to_dll("foo.pyx")
 """
 import os
+import sys
 
 import distutils
 from distutils.dist import Distribution
 from distutils.errors import DistutilsArgError, DistutilsError, CCompilerError
 from distutils.extension import Extension
 from distutils.util import grok_environment_error
-from Cython.Distutils import build_ext
+try:
+    from Cython.Distutils import build_ext
+    HAS_CYTHON = True
+except ImportError:
+    HAS_CYTHON = False
 import shutil
 
 DEBUG = 0
@@ -25,6 +30,8 @@ def pyx_to_dll(filename, ext = None, force_rebuild = 0,
     if not ext:
         modname, extension = os.path.splitext(name)
         assert extension in (".pyx", ".py"), extension
+        if not HAS_CYTHON:
+            filename = filename[:-len(extension)] + '.c'
         ext = Extension(name=modname, sources=[filename])
 
     if not pyxbuild_dir:
@@ -37,23 +44,24 @@ def pyx_to_dll(filename, ext = None, force_rebuild = 0,
     args = [quiet, "build_ext"]
     if force_rebuild:
         args.append("--force")
-    if build_in_temp:
+    if HAS_CYTHON and build_in_temp:
         args.append("--pyrex-c-in-temp")
     dist = Distribution({"script_name": None, "script_args": args})
     if not dist.ext_modules:
         dist.ext_modules = []
     dist.ext_modules.append(ext)
-    dist.cmdclass = {'build_ext': build_ext}
+    if HAS_CYTHON:
+        dist.cmdclass = {'build_ext': build_ext}
     build = dist.get_command_obj('build')
     build.build_base = pyxbuild_dir
 
     try:
         ok = dist.parse_command_line()
-    except DistutilsArgError, msg:
+    except DistutilsArgError:
         raise
 
     if DEBUG:
-        print "options (after parsing command line):"
+        print("options (after parsing command line):")
         dist.dump_option_dicts()
     assert ok
 
@@ -62,22 +70,23 @@ def pyx_to_dll(filename, ext = None, force_rebuild = 0,
         dist.run_commands()
         return dist.get_command_obj("build_ext").get_outputs()[0]
     except KeyboardInterrupt:
-        raise SystemExit, "interrupted"
-    except (IOError, os.error), exc:
+        sys.exit(1)
+    except (IOError, os.error):
+        exc = sys.exc_info()[1]
         error = grok_environment_error(exc)
 
         if DEBUG:
             sys.stderr.write(error + "\n")
             raise
         else:
-            raise RuntimeError, error
+            raise RuntimeError(error)
 
-    except (DistutilsError,
-        CCompilerError), msg:
+    except (DistutilsError, CCompilerError):
         if DEBUG:
             raise
         else:
-            raise RuntimeError(repr(msg))
+            exc = sys.exc_info()[1]
+            raise RuntimeError(repr(exc))
 
 if __name__=="__main__":
     pyx_to_dll("dummy.pyx")