[pypy-svn] r48987 - in pypy/branch/remove-extcompiler-rctypes/pypy/translator: . c c/test test
pedronis at codespeak.net
pedronis at codespeak.net
Fri Nov 23 13:56:20 CET 2007
Author: pedronis
Date: Fri Nov 23 13:56:19 2007
New Revision: 48987
Modified:
pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/genc.py
pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_boehm.py
pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_genc.py
pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_newgc.py
pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_typed.py
pypy/branch/remove-extcompiler-rctypes/pypy/translator/driver.py
pypy/branch/remove-extcompiler-rctypes/pypy/translator/test/test_extension.py
Log:
(cfbolz, pedronis)
don't depend on the so generated to be proper c extensions
Modified: pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/genc.py
==============================================================================
--- pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/genc.py (original)
+++ pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/genc.py Fri Nov 23 13:56:19 2007
@@ -5,6 +5,7 @@
from pypy.translator.c.database import LowLevelDatabase
from pypy.translator.c.extfunc import pre_include_code_lines
from pypy.translator.gensupp import uniquemodulename, NameManager
+from pypy.translator.tool.cbuild import so_ext
from pypy.translator.tool.cbuild import compile_c_module
from pypy.translator.tool.cbuild import build_executable, CCompiler, ProfOpt
from pypy.translator.tool.cbuild import import_module_from_directory
@@ -26,6 +27,7 @@
gcpolicy=None):
self.translator = translator
self.entrypoint = entrypoint
+ self.entrypoint_name = self.entrypoint.func_name
self.originalentrypoint = entrypoint
self.gcpolicy = gcpolicy
if gcpolicy is not None and gcpolicy.requires_stackless:
@@ -74,7 +76,8 @@
# build entrypoint and eventually other things to expose
pf = self.getentrypointptr()
pfname = db.get(pf)
- self.exports[self.entrypoint.func_name] = pf
+ self.exports[self.entrypoint_name] = pf
+ self.c_entrypoint_name = pfname
db.complete()
# add library dependencies
@@ -168,7 +171,7 @@
class CExtModuleBuilder(CBuilder):
standalone = False
- c_ext_module = None
+ _module = None
def getentrypointptr(self, obj=None):
if obj is None:
@@ -186,30 +189,58 @@
libraries=self.libraries)
self._compiled = True
- def import_module(self):
+ def _make_wrapper_module(self):
+ modfile = self.c_source_filename.new(ext=".py")
+ CODE = """
+import ctypes
+
+_lib = ctypes.PyDLL("%(so_name)s")
+
+_entry_point = getattr(_lib, "%(c_entrypoint_name)s")
+_entry_point.restype = ctypes.py_object
+_entry_point.argtypes = 3*(ctypes.py_object,)
+
+def %(entrypoint_name)s(*args, **kwds):
+ return _entry_point(None, args, kwds)
+
+_malloc_counters = _lib.malloc_counters
+_malloc_counters.restype = ctypes.py_object
+_malloc_counters.argtypes = 2*(ctypes.py_object,)
+
+def malloc_counters():
+ return _malloc_counters(None, None)
+""" % {'so_name': self.c_source_filename.new(ext=so_ext),
+ 'entrypoint_name': self.entrypoint_name,
+ 'c_entrypoint_name': self.c_entrypoint_name}
+ modfile.write(CODE)
+ self._module_path = modfile
+
+ def _import_module(self, isolated=False):
+ if self._module is not None:
+ return self._module
assert self._compiled
- assert not self.c_ext_module
- mod = import_module_from_directory(self.c_source_filename.dirpath(),
- self.c_source_filename.purebasename)
- self.c_ext_module = mod
+ assert not self._module
+ self._make_wrapper_module()
+ if not isolated:
+ mod = self._module_path.pyimport()
+ else:
+ mod = isolate.Isolate((str(self._module_path.dirpath()),
+ self._module_path.purebasename))
+ self._module = mod
return mod
-
- def isolated_import(self):
- assert self._compiled
- assert not self.c_ext_module
- self.c_ext_module = isolate.Isolate((str(self.c_source_filename.dirpath()),
- self.c_source_filename.purebasename))
- return self.c_ext_module
- def get_entry_point(self):
- assert self.c_ext_module
- return getattr(self.c_ext_module,
- self.entrypoint.func_name)
-
+ def get_entry_point(self, isolated=False):
+ self._import_module(isolated=isolated)
+ return getattr(self._module, self.entrypoint_name)
+
+ def get_malloc_counters(self, isolated=False):
+ self._import_module(isolated=isolated)
+ return self._module.malloc_counters
+
def cleanup(self):
- assert self.c_ext_module
- if isinstance(self.c_ext_module, isolate.Isolate):
- isolate.close_isolate(self.c_ext_module)
+ assert self._module
+ if isinstance(self._module, isolate.Isolate):
+ isolate.close_isolate(self._module)
class CStandaloneBuilder(CBuilder):
standalone = True
Modified: pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_boehm.py
==============================================================================
--- pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_boehm.py (original)
+++ pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_boehm.py Fri Nov 23 13:56:19 2007
@@ -41,9 +41,8 @@
if conftest.option.view:
t.view()
cbuilder.compile()
- mod = cbuilder.isolated_import()
self._cleanups.append(cbuilder.cleanup) # schedule cleanup after test
- return cbuilder.get_entry_point()
+ return cbuilder.get_entry_point(isolated=True)
return compile()
Modified: pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_genc.py
==============================================================================
--- pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_genc.py (original)
+++ pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_genc.py Fri Nov 23 13:56:19 2007
@@ -42,15 +42,14 @@
compiled_fn = t.compile_c()
if conftest.option.view:
t.view()
- # XXX fish fish fish some more
- module = t.driver.cbuilder.c_ext_module
+ malloc_counters = t.driver.cbuilder.get_malloc_counters()
def checking_fn(*args, **kwds):
if 'expected_extra_mallocs' in kwds:
expected_extra_mallocs = kwds.pop('expected_extra_mallocs')
else:
expected_extra_mallocs = 0
res = compiled_fn(*args, **kwds)
- mallocs, frees = module.malloc_counters()
+ mallocs, frees = malloc_counters()
if isinstance(expected_extra_mallocs, int):
assert mallocs - frees == expected_extra_mallocs
else:
Modified: pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_newgc.py
==============================================================================
--- pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_newgc.py (original)
+++ pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_newgc.py Fri Nov 23 13:56:19 2007
@@ -23,16 +23,15 @@
builder = genc.CExtModuleBuilder(t, fn, config=config)
builder.generate_source(defines={'COUNT_OP_MALLOCS': 1})
builder.compile()
- builder.import_module()
if conftest.option.view:
t.view()
- module = builder.c_ext_module
compiled_fn = builder.get_entry_point()
+ malloc_counters = builder.get_malloc_counters()
def checking_fn(*args, **kwds):
try:
return compiled_fn(*args, **kwds)
finally:
- mallocs, frees = module.malloc_counters()
+ mallocs, frees = malloc_counters()
assert mallocs == frees
return checking_fn
Modified: pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_typed.py
==============================================================================
--- pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_typed.py (original)
+++ pypy/branch/remove-extcompiler-rctypes/pypy/translator/c/test/test_typed.py Fri Nov 23 13:56:19 2007
@@ -35,7 +35,6 @@
builder = genc.CExtModuleBuilder(t, func, config=t.config)
builder.generate_source()
builder.compile()
- builder.import_module()
return builder.get_entry_point()
def getcompiled(self, func, argtypes=None, view=False):
Modified: pypy/branch/remove-extcompiler-rctypes/pypy/translator/driver.py
==============================================================================
--- pypy/branch/remove-extcompiler-rctypes/pypy/translator/driver.py (original)
+++ pypy/branch/remove-extcompiler-rctypes/pypy/translator/driver.py Fri Nov 23 13:56:19 2007
@@ -514,7 +514,6 @@
self.c_entryp = cbuilder.executable_name
self.create_exe()
else:
- cbuilder.import_module()
self.c_entryp = cbuilder.get_entry_point()
#
task_compile_c = taskdef(task_compile_c, ['source_c'], "Compiling c source")
Modified: pypy/branch/remove-extcompiler-rctypes/pypy/translator/test/test_extension.py
==============================================================================
--- pypy/branch/remove-extcompiler-rctypes/pypy/translator/test/test_extension.py (original)
+++ pypy/branch/remove-extcompiler-rctypes/pypy/translator/test/test_extension.py Fri Nov 23 13:56:19 2007
@@ -40,7 +40,6 @@
backend_optimizations(t)
if view:
t.viewcg()
- cbuilder.import_module()
return cbuilder.get_entry_point()
def example_int_long(arg=int):
More information about the Pypy-commit
mailing list