[pypy-commit] cffi default: Use distutils. Took me a while to figure out these lines.

arigo noreply at buildbot.pypy.org
Fri Jun 15 11:21:05 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r363:b8bff874b2ce
Date: 2012-06-15 11:02 +0200
http://bitbucket.org/cffi/cffi/changeset/b8bff874b2ce/

Log:	Use distutils. Took me a while to figure out these lines.

diff --git a/cffi/ffiplatform.py b/cffi/ffiplatform.py
--- a/cffi/ffiplatform.py
+++ b/cffi/ffiplatform.py
@@ -29,3 +29,36 @@
             pass
         _tmpdir = os.path.abspath('__pycache__')
     return _tmpdir
+
+
+def compile(tmpdir, modname, **kwds):
+    """Compile a C extension module using distutils."""
+
+    saved_environ = os.environ.copy()
+    saved_path = os.getcwd()
+    try:
+        os.chdir(tmpdir)
+        outputfilename = _build(modname, kwds)
+        outputfilename = os.path.abspath(outputfilename)
+    finally:
+        os.chdir(saved_path)
+        # workaround for a distutils bugs where some env vars can
+        # become longer and longer every time it is used
+        for key, value in saved_environ.items():
+            if os.environ.get(key) != value:
+                os.environ[key] = value
+    return outputfilename
+
+def _build(modname, kwds):
+    # XXX compact but horrible :-(
+    from distutils.core import Distribution, Extension
+    ext = Extension(name=modname, sources=[modname + '.c'], **kwds)
+    dist = Distribution({'ext_modules': [ext]})
+    options = dist.get_option_dict('build_ext')
+    options['force'] = ('ffiplatform', True)
+    #
+    dist.run_command('build_ext')
+    #
+    cmd_obj = dist.get_command_obj('build_ext')
+    [soname] = cmd_obj.get_outputs()
+    return soname
diff --git a/cffi/verifier.py b/cffi/verifier.py
--- a/cffi/verifier.py
+++ b/cffi/verifier.py
@@ -18,10 +18,10 @@
             self.typesdict[type] = num
             return num
 
-    def verify(self, preamble, stop_on_warnings=True):
-        # XXX  take **kwds
+    def verify(self, preamble, **kwds):
         modname = ffiplatform.undercffi_module_name()
-        filebase = os.path.join(ffiplatform.tmpdir(), modname)
+        tmpdir = ffiplatform.tmpdir()
+        filebase = os.path.join(tmpdir, modname)
         self.chained_list_constants = None
         
         with open(filebase + '.c', 'w') as f:
@@ -62,21 +62,11 @@
             #
             del self.f
 
-        # XXX use more distutils?
-        import distutils.sysconfig
-        python_h = distutils.sysconfig.get_python_inc()
-        cmdline = "gcc -I'%s' -O2 -shared -fPIC %s.c -o %s.so" % (
-            python_h, filebase, filebase)
-        if stop_on_warnings:
-            cmdline += " -Werror"
-        err = os.system(cmdline)
-        if err:
-            raise ffiplatform.VerificationError(
-                '%s.c: see compilation errors above' % (filebase,))
+        outputfilename = ffiplatform.compile(tmpdir, modname, **kwds)
         #
         import imp
         try:
-            module = imp.load_dynamic(modname, '%s.so' % filebase)
+            module = imp.load_dynamic(modname, outputfilename)
         except ImportError, e:
             raise ffiplatform.VerificationError(str(e))
         #


More information about the pypy-commit mailing list