[pypy-svn] r73642 - in pypy/branch/cpython-extension/pypy/module: cpyext/test imp
xoraxax at codespeak.net
xoraxax at codespeak.net
Sun Apr 11 15:52:05 CEST 2010
Author: xoraxax
Date: Sun Apr 11 15:52:04 2010
New Revision: 73642
Modified:
pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py
pypy/branch/cpython-extension/pypy/module/cpyext/test/test_import.py
pypy/branch/cpython-extension/pypy/module/imp/importing.py
Log:
Add support for CPyExt .so/.pyd imports.
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_cpyext.py Sun Apr 11 15:52:04 2010
@@ -1,4 +1,5 @@
import sys
+import os.path
import py
@@ -122,7 +123,7 @@
cls.space = gettestobjspace(usemodules=['cpyext'])
cls.space.getbuiltinmodule("cpyext")
- def import_module(self, name, init=None, body=''):
+ def import_module(self, name, init=None, body='', load_it=True):
if init is not None:
code = """
#include <Python.h>
@@ -149,14 +150,16 @@
kwds["compile_extra"] = ["-Werror=implicit-function-declaration"]
mod = compile_module(name, **kwds)
- api.load_extension_module(self.space, mod, name)
self.name = name
- return self.space.getitem(
- self.space.sys.get('modules'),
- self.space.wrap(name))
+ if load_it:
+ api.load_extension_module(self.space, mod, name)
+ return self.space.getitem(
+ self.space.sys.get('modules'),
+ self.space.wrap(name))
+ else:
+ return os.path.dirname(mod)
def import_extension(self, modname, functions, prologue=""):
-
methods_table = []
codes = []
for funcname, flags, code in functions:
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_import.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_import.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_import.py Sun Apr 11 15:52:04 2010
@@ -1,6 +1,5 @@
-import py
-
from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
class TestImport(BaseApiTest):
def test_import(self, space, api):
@@ -9,3 +8,12 @@
pdb = api.PyImport_Import(space.wrap("pdb"))
assert pdb
assert pdb.get("pm")
+
+class AppTestImportLogic(AppTestCpythonExtensionBase):
+ def test_import_logic(self):
+ path = self.import_module(name='foo', load_it=False)
+ import sys
+ sys.path.append(path)
+ import foo
+ assert foo.fooType
+
Modified: pypy/branch/cpython-extension/pypy/module/imp/importing.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/imp/importing.py (original)
+++ pypy/branch/cpython-extension/pypy/module/imp/importing.py Sun Apr 11 15:52:04 2010
@@ -25,6 +25,11 @@
# PY_CODERESOURCE = 8
IMP_HOOK = 9
+if sys.platform.startswith('win'):
+ so_extension = ".pyd"
+else:
+ so_extension = ".so"
+
def find_modtype(space, filepart):
"""Check which kind of module to import for the given filepart,
which is a path without extension. Returns PY_SOURCE, PY_COMPILED or
@@ -40,16 +45,18 @@
# look for a lone .pyc file.
# The "imp" module does not respect this, and is allowed to find
# lone .pyc files.
- if not space.config.objspace.lonepycfiles:
- return SEARCH_ERROR, None, None
-
# check the .pyc file
- if space.config.objspace.usepycfiles:
+ if space.config.objspace.usepycfiles and space.config.objspace.lonepycfiles:
pycfile = filepart + ".pyc"
if os.path.exists(pycfile) and case_ok(pycfile):
# existing .pyc file
return PY_COMPILED, ".pyc", "rb"
+ if space.config.objspace.usemodules.cpyext:
+ pydfile = filepart + so_extension
+ if os.path.exists(pydfile) and case_ok(pydfile):
+ return C_EXTENSION, so_extension, "rb"
+
return SEARCH_ERROR, None, None
if sys.platform in ['linux2', 'freebsd']:
@@ -332,6 +339,9 @@
except:
stream.close()
raise
+ if modtype == C_EXTENSION:
+ filename = filepart + suffix
+ return FindInfo(modtype, filename, None, suffix, filemode)
except StreamErrors:
pass
@@ -356,7 +366,7 @@
if find_info.modtype == C_BUILTIN:
return space.getbuiltinmodule(find_info.filename, force_init=True)
- if find_info.modtype in (PY_SOURCE, PY_COMPILED, PKG_DIRECTORY):
+ if find_info.modtype in (PY_SOURCE, PY_COMPILED, C_EXTENSION, PKG_DIRECTORY):
w_mod = None
if reuse:
try:
@@ -397,6 +407,12 @@
# fetch the module again, in case of "substitution"
w_mod = check_sys_modules(space, w_modulename)
return w_mod
+ elif find_info.modtype == C_EXTENSION and space.config.objspace.usemodules.cpyext:
+ # the next line is mandantory to init cpyext
+ space.getbuiltinmodule("cpyext")
+ from pypy.module.cpyext.api import load_extension_module
+ load_extension_module(space, find_info.filename, space.str_w(w_modulename))
+ return check_sys_modules(space, w_modulename)
except OperationError:
w_mods = space.sys.get('modules')
space.call_method(w_mods, 'pop', w_modulename, space.w_None)
More information about the Pypy-commit
mailing list