1 """Build a Pyrex file from .pyx source to .so loadable module using
2 the installed distutils infrastructure. Call:
4 out_fname = pyx_to_dll("foo.pyx")
10 from distutils.dist import Distribution
11 from distutils.errors import DistutilsArgError, DistutilsError, CCompilerError
12 from distutils.extension import Extension
13 from distutils.util import grok_environment_error
15 from Cython.Distutils import build_ext
22 def pyx_to_dll(filename, ext = None, force_rebuild = 0,
23 build_in_temp=False, pyxbuild_dir=None):
24 """Compile a PYX file to a DLL and return the name of the generated .so
26 assert os.path.exists(filename)
28 path, name = os.path.split(filename)
31 modname, extension = os.path.splitext(name)
32 assert extension in (".pyx", ".py"), extension
34 filename = filename[:-len(extension)] + '.c'
35 ext = Extension(name=modname, sources=[filename])
38 pyxbuild_dir = os.path.join(path, "_pyxbld")
44 args = [quiet, "build_ext"]
46 args.append("--force")
47 if HAS_CYTHON and build_in_temp:
48 args.append("--pyrex-c-in-temp")
49 dist = Distribution({"script_name": None, "script_args": args})
50 if not dist.ext_modules:
52 dist.ext_modules.append(ext)
54 dist.cmdclass = {'build_ext': build_ext}
55 build = dist.get_command_obj('build')
56 build.build_base = pyxbuild_dir
59 ok = dist.parse_command_line()
60 except DistutilsArgError:
64 print("options (after parsing command line):")
65 dist.dump_option_dicts()
71 return dist.get_command_obj("build_ext").get_outputs()[0]
72 except KeyboardInterrupt:
74 except (IOError, os.error):
75 exc = sys.exc_info()[1]
76 error = grok_environment_error(exc)
79 sys.stderr.write(error + "\n")
82 raise RuntimeError(error)
84 except (DistutilsError, CCompilerError):
88 exc = sys.exc_info()[1]
89 raise RuntimeError(repr(exc))
91 if __name__=="__main__":
92 pyx_to_dll("dummy.pyx")