[pypy-commit] cffi default: Final fixes.
arigo
noreply at buildbot.pypy.org
Sun Jul 15 14:45:59 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r649:006f22f50bb8
Date: 2012-07-15 14:45 +0200
http://bitbucket.org/cffi/cffi/changeset/006f22f50bb8/
Log: Final fixes.
diff --git a/cffi/ffiplatform.py b/cffi/ffiplatform.py
--- a/cffi/ffiplatform.py
+++ b/cffi/ffiplatform.py
@@ -24,14 +24,18 @@
return _tmpdir
-def compile(tmpdir, srcfilename, modname, **kwds):
+def get_extension(srcfilename, modname, **kwds):
+ from distutils.core import Extension
+ return Extension(name=modname, sources=[srcfilename], **kwds)
+
+def compile(tmpdir, ext):
"""Compile a C extension module using distutils."""
saved_environ = os.environ.copy()
saved_path = os.getcwd()
try:
os.chdir(tmpdir)
- outputfilename = _build(srcfilename, modname, kwds)
+ outputfilename = _build(ext)
outputfilename = os.path.abspath(outputfilename)
finally:
os.chdir(saved_path)
@@ -42,12 +46,11 @@
os.environ[key] = value
return outputfilename
-def _build(srcfilename, modname, kwds):
+def _build(ext):
# XXX compact but horrible :-(
- from distutils.core import Distribution, Extension
+ from distutils.core import Distribution
import distutils.errors
#
- ext = Extension(name=modname, sources=[srcfilename], **kwds)
dist = Distribution({'ext_modules': [ext]})
options = dist.get_option_dict('build_ext')
options['force'] = ('ffiplatform', True)
diff --git a/cffi/verifier.py b/cffi/verifier.py
--- a/cffi/verifier.py
+++ b/cffi/verifier.py
@@ -57,9 +57,14 @@
assert self._status == 'module'
return self._load_library()
- def getmodulename(self):
+ def get_module_name(self):
return os.path.splitext(os.path.basename(self.modulefilename))[0]
+ def get_extension(self):
+ sourcename = os.path.abspath(self.sourcefilename)
+ modname = self.get_module_name()
+ return ffiplatform.get_extension(sourcename, modname, **self.kwds)
+
# ----------
@staticmethod
@@ -71,7 +76,7 @@
def _locate_module(self):
try:
- f, filename, descr = imp.find_module(self.getmodulename())
+ f, filename, descr = imp.find_module(self.get_module_name())
except ImportError:
return
if f is not None:
@@ -161,7 +166,7 @@
prnt()
#
# standard init.
- modname = self.getmodulename()
+ modname = self.get_module_name()
prnt('PyMODINIT_FUNC')
prnt('init%s(void)' % modname)
prnt('{')
@@ -176,10 +181,7 @@
def _compile_module(self):
# compile this C source
tmpdir = os.path.dirname(self.sourcefilename)
- sourcename = os.path.basename(self.sourcefilename)
- modname = self.getmodulename()
- outputfilename = ffiplatform.compile(tmpdir, sourcename,
- modname, **self.kwds)
+ outputfilename = ffiplatform.compile(tmpdir, self.get_extension())
try:
same = os.path.samefile(outputfilename, self.modulefilename)
except OSError:
@@ -192,7 +194,8 @@
# XXX review all usages of 'self' here!
# import it as a new extension module
try:
- module = imp.load_dynamic(self.getmodulename(), self.modulefilename)
+ module = imp.load_dynamic(self.get_module_name(),
+ self.modulefilename)
except ImportError, e:
error = "importing %r: %s" % (self.modulefilename, e)
raise ffiplatform.VerificationError(error)
diff --git a/testing/test_zdistutils.py b/testing/test_zdistutils.py
--- a/testing/test_zdistutils.py
+++ b/testing/test_zdistutils.py
@@ -1,4 +1,4 @@
-import imp, math, StringIO
+import os, imp, math, StringIO
import py
from cffi import FFI, FFIError
from cffi.verifier import Verifier
@@ -42,8 +42,8 @@
csrc = '/*hi there!*/\n#include <math.h>\n'
v = Verifier(ffi, csrc)
v.compile_module()
- assert v.getmodulename().startswith('_cffi_')
- mod = imp.load_dynamic(v.getmodulename(), v.modulefilename)
+ assert v.get_module_name().startswith('_cffi_')
+ mod = imp.load_dynamic(v.get_module_name(), v.modulefilename)
assert hasattr(mod, '_cffi_setup')
def test_compile_module_explicit_filename():
@@ -54,8 +54,8 @@
v.modulefilename = filename = str(udir.join('test_compile_module.so'))
v.compile_module()
assert filename == v.modulefilename
- assert v.getmodulename() == 'test_compile_module'
- mod = imp.load_dynamic(v.getmodulename(), v.modulefilename)
+ assert v.get_module_name() == 'test_compile_module'
+ mod = imp.load_dynamic(v.get_module_name(), v.modulefilename)
assert hasattr(mod, '_cffi_setup')
def test_name_from_md5_of_cdef():
@@ -64,7 +64,7 @@
ffi = FFI()
ffi.cdef("%s sin(double x);" % csrc)
v = Verifier(ffi, "#include <math.h>")
- names.append(v.getmodulename())
+ names.append(v.get_module_name())
assert names[0] == names[1] != names[2]
def test_name_from_md5_of_csrc():
@@ -73,7 +73,7 @@
ffi = FFI()
ffi.cdef("double sin(double x);")
v = Verifier(ffi, csrc)
- names.append(v.getmodulename())
+ names.append(v.get_module_name())
assert names[0] == names[1] != names[2]
def test_load_library():
@@ -118,6 +118,6 @@
v = ffi.verifier
ext = v.get_extension()
assert str(ext.__class__) == 'distutils.extension.Extension'
- assert ext.sources == [v.sourcefilename]
- assert ext.name == v.getmodulename()
+ assert ext.sources == [os.path.abspath(v.sourcefilename)]
+ assert ext.name == v.get_module_name()
assert ext.define_macros == [('TEST_EXTENSION_OBJECT', '1')]
More information about the pypy-commit
mailing list