| 1 | import sys |
| 2 | import pytest |
| 3 | |
| 4 | from pypy.config.pypyoption import get_pypy_config |
| 5 | from pypy.objspace.std.objspace import StdObjSpace |
| 6 | from rpython.tool.udir import udir |
| 7 | |
| 8 | mod_def = """ |
| 9 | from pypy.interpreter.mixedmodule import MixedModule |
| 10 | |
| 11 | import time |
| 12 | |
| 13 | class Module(MixedModule): |
| 14 | |
| 15 | appleveldefs = {} |
| 16 | |
| 17 | interpleveldefs = { |
| 18 | 'clock' : 'interp_time.clock', |
| 19 | 'time' : 'interp_time.time_', |
| 20 | 'sleep' : 'interp_time.sleep', |
| 21 | } |
| 22 | """ |
| 23 | |
| 24 | mod_interp = """ |
| 25 | import time |
| 26 | |
| 27 | from pypy.interpreter.gateway import unwrap_spec |
| 28 | |
| 29 | def clock(space): |
| 30 | return space.wrap(time.clock()) |
| 31 | |
| 32 | def time_(space): |
| 33 | return space.wrap(time.time()) |
| 34 | |
| 35 | @unwrap_spec(seconds=float) |
| 36 | def sleep(space, seconds): |
| 37 | '''sleep(seconds: float) -> None |
| 38 | |
| 39 | pause for a float number of seconds''' |
| 40 | time.sleep(seconds) |
| 41 | """ |
| 42 | |
| 43 | old_sys_path = [] |
| 44 | |
| 45 | def init_extmodule_code(): |
| 46 | pkg = udir.join("testext") |
| 47 | pkg.ensure(dir=True) |
| 48 | pkg.join("__init__.py").write("# package") |
| 49 | mod = pkg.join("extmod") |
| 50 | mod.ensure(dir=True) |
| 51 | mod.join("__init__.py").write("#") |
| 52 | mod.join("interp_time.py").write(mod_interp) |
| 53 | mod.join("moduledef.py").write(mod_def) |
| 54 | |
| 55 | class AppTestExtModules(object): |
| 56 | def setup_class(cls): |
| 57 | init_extmodule_code() |
| 58 | conf = get_pypy_config() |
| 59 | conf.objspace.extmodules = 'testext.extmod' |
| 60 | old_sys_path[:] = sys.path[:] |
| 61 | sys.path.insert(0, str(udir)) |
| 62 | space = StdObjSpace(conf) |
| 63 | cls.space = space |
| 64 | |
| 65 | def teardown_class(cls): |
| 66 | sys.path[:] = old_sys_path |
| 67 | |
| 68 | @pytest.mark.skipif("config.option.runappdirect") |
| 69 | def test_import(self): |
| 70 | import extmod |
| 71 | assert not hasattr(extmod, '__file__') |
| 72 | assert type(extmod.time()) is float |
| 73 | assert extmod.sleep.__doc__.endswith('float number of seconds') |