| 1 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 2 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 3 |
from pypy.module.cpyext.import_ import * |
| 4 |
from pypy.module.cpyext.import_ import ( |
| 5 |
_PyImport_AcquireLock, _PyImport_ReleaseLock) |
| 6 |
from rpython.rtyper.lltypesystem import rffi |
| 7 |
|
| 8 |
class TestImport(BaseApiTest): |
| 9 |
def test_import(self, space): |
| 10 |
stat = PyImport_Import(space, space.wrap("stat")) |
| 11 |
assert stat |
| 12 |
assert space.getattr(stat, space.wrap("S_IMODE")) |
| 13 |
|
| 14 |
def test_addmodule(self, space): |
| 15 |
with rffi.scoped_str2charp("sys") as modname: |
| 16 |
w_sys = PyImport_AddModule(space, modname) |
| 17 |
assert w_sys is space.sys |
| 18 |
|
| 19 |
with rffi.scoped_str2charp("foobar") as modname: |
| 20 |
w_foobar = PyImport_AddModule(space, modname) |
| 21 |
assert space.text_w(space.getattr(w_foobar, |
| 22 |
space.wrap('__name__'))) == 'foobar' |
| 23 |
|
| 24 |
def test_getmoduledict(self, space, api): |
| 25 |
testmod = "imghdr" |
| 26 |
w_pre_dict = PyImport_GetModuleDict(space, ) |
| 27 |
assert not space.contains_w(w_pre_dict, space.wrap(testmod)) |
| 28 |
|
| 29 |
with rffi.scoped_str2charp(testmod) as modname: |
| 30 |
w_module = PyImport_ImportModule(space, modname) |
| 31 |
print w_module |
| 32 |
assert w_module |
| 33 |
|
| 34 |
w_dict = PyImport_GetModuleDict(space, ) |
| 35 |
assert space.contains_w(w_dict, space.wrap(testmod)) |
| 36 |
|
| 37 |
def test_reload(self, space): |
| 38 |
stat = PyImport_Import(space, space.wrap("stat")) |
| 39 |
space.delattr(stat, space.wrap("S_IMODE")) |
| 40 |
stat = PyImport_ReloadModule(space, stat) |
| 41 |
assert space.getattr(stat, space.wrap("S_IMODE")) |
| 42 |
|
| 43 |
def test_ImportModuleLevelObject(self, space): |
| 44 |
w_mod = PyImport_ImportModuleLevelObject( |
| 45 |
space, space.wrap('stat'), None, None, None, 0) |
| 46 |
assert w_mod |
| 47 |
assert space.getattr(w_mod, space.wrap("S_IMODE")) |
| 48 |
|
| 49 |
def test_lock(self, space): |
| 50 |
|
| 51 |
_PyImport_AcquireLock(space, ) |
| 52 |
_PyImport_AcquireLock(space, ) |
| 53 |
_PyImport_ReleaseLock(space, ) |
| 54 |
_PyImport_ReleaseLock(space, ) |
| 55 |
|
| 56 |
|
| 57 |
class AppTestImportLogic(AppTestCpythonExtensionBase): |
| 58 |
def test_import_logic(self): |
| 59 |
import sys, os |
| 60 |
path = self.compile_module('test_import_module', |
| 61 |
source_files=[os.path.join(self.here, 'test_import_module.c')]) |
| 62 |
sys.path.append(os.path.dirname(path)) |
| 63 |
import test_import_module |
| 64 |
assert test_import_module.TEST is None |
| 65 |
|
| 66 |
def test_getmodule(self): |
| 67 |
import sys |
| 68 |
module = self.import_extension('foo', [ |
| 69 |
("getmodule", "METH_O", |
| 70 |
''' |
| 71 |
PyObject *mod = PyImport_GetModule(args); |
| 72 |
if (mod == NULL) { |
| 73 |
Py_RETURN_NONE; |
| 74 |
} |
| 75 |
return mod; |
| 76 |
''')]) |
| 77 |
_sys = module.getmodule('sys') |
| 78 |
assert sys is _sys |
| 79 |
assert module.getmodule('not_in_sys_modules') is None |