Debian Codegraph @

pypy/module/_demo/test/test_import.py

raw symbols SCIP
1 import sys
2 from pypy.module._demo.moduledef import Module
3 from pypy.tool.option import make_config, make_objspace
4
5 class TestImport:
6
7 def setup_method(self, func):
8 Module.demo_events = []
9
10 def test_startup(self):
11 config = make_config(None, usemodules=('_demo',))
12 space = make_objspace(config)
13 w_modules = space.sys.get('modules')
14
15 assert Module.demo_events == ['setup']
16 assert not space.contains_w(w_modules, space.wrap('_demo'))
17
18 # first import
19 w_import = space.builtin.get('__import__')
20 w_demo = space.call(w_import,
21 space.newlist([space.wrap('_demo')]))
22 assert Module.demo_events == ['setup', 'startup']
23
24 # reload the module, this should not call startup again
25 space.delitem(w_modules,
26 space.wrap('_demo'))
27 w_demo = space.call(w_import,
28 space.newlist([space.wrap('_demo')]))
29 assert Module.demo_events == ['setup', 'startup']
30
31 assert space.getattr(w_demo, space.wrap('measuretime'))
32
33
34 posixname = 'posix' if sys.platform != 'win32' else 'nt'
35
36 class TestMixedModuleUnfreeze:
37 spaceconfig = dict(usemodules=('_demo', 'posix'))
38
39 def test_random_stuff_can_unfreeze(self):
40 # When a module contains an "import" statement in applevel code, the
41 # imported module is initialized, possibly after it has been already
42 # frozen.
43
44 # This is important when the module startup() function does something
45 # at runtime, like setting os.environ (posix module) or initializing
46 # the winsock library (_socket module)
47 w_posix = self.space.builtin_modules[posixname]
48 w_demo = self.space.builtin_modules['_demo']
49
50 w_posix._cleanup_()
51 assert w_posix.startup_called == False
52 w_demo._cleanup_() # w_demo.appleveldefs['DemoError'] imports posix
53 assert w_posix.startup_called == False
54