Skip to content

Commit

Permalink
pyximport for compiling .pyx files on import
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulPrescod committed Aug 16, 2008
1 parent cc77438 commit 53c3e2b
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pyximport/PKG-INFO
@@ -0,0 +1,11 @@
Metadata-Version: 1.0
Name: pyximport
Version: 1.0
Summary: Hooks to build and run Pyrex files as if they were simple Python files
Home-page: http://www.prescod.net/pyximport
Author: Paul Prescod
Author-email: paul@prescod.net
License: Python
Description: UNKNOWN
Keywords: pyrex import hook
Platform: UNKNOWN
79 changes: 79 additions & 0 deletions pyximport/README
@@ -0,0 +1,79 @@

== Pyximport ==

Download: pyx-import-1.0.tar.gz
<http://www.prescod.net/pyximport/pyximport-1.0.tar.gz>

Pyrex is a compiler. Therefore it is natural that people tend to go
through an edit/compile/test cycle with Pyrex modules. But my personal
opinion is that one of the deep insights in Python's implementation is
that a language can be compiled (Python modules are compiled to .pyc)
files and hide that compilation process from the end-user so that they
do not have to worry about it. Pyximport does this for Pyrex modules.
For instance if you write a Pyrex module called "foo.pyx", with
Pyximport you can import it in a regular Python module like this:


import pyximport; pyximport.install()
import foo

Doing so will result in the compilation of foo.pyx (with appropriate
exceptions if it has an error in it).

If you would always like to import pyrex files without building them
specially, you can also the first line above to your sitecustomize.py.
That will install the hook every time you run Python. Then you can use
Pyrex modules just with simple import statements. I like to test my
Pyrex modules like this:


python -c "import foo"

== Dependency Handling ==

In Pyximport 1.1 it is possible to declare that your module depends on
multiple files, (likely ".h" and ".pxd" files). If your Pyrex module is
named "foo" and thus has the filename "foo.pyx" then you should make
another file in the same directory called "foo.pyxdep". The
"modname.pyxdep" file can be a list of filenames or "globs" (like
"*.pxd" or "include/*.h"). Each filename or glob must be on a separate
line. Pyximport will check the file date for each of those files before
deciding whether to rebuild the module. In order to keep track of the
fact that the dependency has been handled, Pyximport updates the
modification time of your ".pyx" source file. Future versions may do
something more sophisticated like informing distutils of the
dependencies directly.

== Limitations ==

Pyximport does not give you any control over how your Pyrex file is
compiled. Usually the defaults are fine. You might run into problems if
you wanted to write your program in half-C, half-Pyrex and build them
into a single library. Pyximport 1.2 will probably do this.

Pyximport does not hide the Distutils/GCC warnings and errors generated
by the import process. Arguably this will give you better feedback if
something went wrong and why. And if nothing went wrong it will give you
the warm fuzzy that pyximport really did rebuild your module as it was
supposed to.

== For further thought and discussion ==

I don't think that Python's "reload" will do anything for changed .SOs
on some (all?) platforms. It would require some (easy) experimentation
that I haven't gotten around to. But reload is rarely used in
applications outside of the Python interactive interpreter and certainly
not used much for C extension modules. Info about Windows
<http://mail.python.org/pipermail/python-list/2001-July/053798.html>

"setup.py install" does not modify sitecustomize.py for you. Should it?
Modifying Python's "standard interpreter" behaviour may be more than
most people expect of a package they install..

Pyximport puts your ".c" file beside your ".pyx" file (analogous to
".pyc" beside ".py"). But it puts the platform-specific binary in a
build directory as per normal for Distutils. If I could wave a magic
wand and get Pyrex or distutils or whoever to put the build directory I
might do it but not necessarily: having it at the top level is VERY
HELPFUL for debugging Pyrex problems.

35 changes: 35 additions & 0 deletions pyximport/Setup.py
@@ -0,0 +1,35 @@
from distutils.core import setup
import sys, os
from StringIO import StringIO

if "sdist" in sys.argv:
try:
os.remove("MANIFEST")
except (IOError, OSError):
pass

import html2text
out = StringIO()
html2text.convert_files(open("index.html"), out)
out.write("\n\n")
open("README", "w").write(out.getvalue())

setup(
name = "pyximport",
fullname = "Pyrex Import Hooks",
version = "1.0",
description = "Hooks to build and run Pyrex files as if they were simple Python files",
author = "Paul Prescod",
author_email = "paul@prescod.net",
url = "http://www.prescod.net/pyximport",
license = "Python",
keywords = "pyrex import hook",
scripts = ["pyxrun"],
data_files = [("examples/multi_file_extension",
["README", "ccode.c", "test.pyx", "test.pyxbld"]),
("examples/dependencies",
["README", "test.pyx", "test.pyxdep", "header.h",
"header2.h", "header3.h", "header4.h"])
],
py_modules = ["pyximport", "pyxbuild"])

79 changes: 79 additions & 0 deletions pyximport/pyxbuild.py
@@ -0,0 +1,79 @@
"""Build a Pyrex file from .pyx source to .so loadable module using
the installed distutils infrastructure. Call:
out_fname = pyx_to_dll("foo.pyx")
"""
import os, md5

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 Pyrex.Distutils import build_ext
import shutil

DEBUG = 0
def pyx_to_dll(filename, ext = None, force_rebuild = 0):
"""Compile a PYX file to a DLL and return the name of the generated .so
or .dll ."""
assert os.path.exists(filename)

path, name = os.path.split(filename)

if not ext:
modname, extension = os.path.splitext(name)
assert extension == ".pyx", extension
ext = Extension(name=modname, sources=[filename])

if DEBUG:
quiet = "--verbose"
else:
quiet = "--quiet"
args = [quiet, "build_ext"]
if force_rebuild:
args.append("--force")
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}
build = dist.get_command_obj('build')
build.build_base = os.path.join(path, "_pyxbld")

try:
ok = dist.parse_command_line()
except DistutilsArgError, msg:
raise

if DEBUG:
print "options (after parsing command line):"
dist.dump_option_dicts()
assert ok


try:
dist.run_commands()
return dist.get_command_obj("build_ext").get_outputs()[0]
except KeyboardInterrupt:
raise SystemExit, "interrupted"
except (IOError, os.error), exc:
error = grok_environment_error(exc)

if DEBUG:
sys.stderr.write(error + "\n")
raise
else:
raise SystemExit, error

except (DistutilsError,
CCompilerError), msg:
if DEBUG:
raise
else:
raise SystemExit, "error: " + str(msg)

if __name__=="__main__":
pyx_to_dll("dummy.pyx")
import test

0 comments on commit 53c3e2b

Please sign in to comment.