File: test_import.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (54 lines) | stat: -rw-r--r-- 1,967 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
from pypy.module._demo.moduledef import Module
from pypy.tool.option import make_config, make_objspace

class TestImport:

    def setup_method(self, func):
        Module.demo_events = []

    def test_startup(self):
        config = make_config(None, usemodules=('_demo',))
        space = make_objspace(config)
        w_modules = space.sys.get('modules')

        assert Module.demo_events == ['setup']
        assert not space.contains_w(w_modules, space.wrap('_demo'))

        # first import
        w_import = space.builtin.get('__import__')
        w_demo = space.call(w_import,
                            space.newlist([space.wrap('_demo')]))
        assert Module.demo_events == ['setup', 'startup']

        # reload the module, this should not call startup again
        space.delitem(w_modules,
                      space.wrap('_demo'))
        w_demo = space.call(w_import,
                            space.newlist([space.wrap('_demo')]))
        assert Module.demo_events == ['setup', 'startup']

        assert space.getattr(w_demo, space.wrap('measuretime'))


posixname = 'posix' if sys.platform != 'win32' else 'nt'

class TestMixedModuleUnfreeze:
    spaceconfig = dict(usemodules=('_demo', 'posix'))

    def test_random_stuff_can_unfreeze(self):
        # When a module contains an "import" statement in applevel code, the
        # imported module is initialized, possibly after it has been already
        # frozen.

        # This is important when the module startup() function does something
        # at runtime, like setting os.environ (posix module) or initializing
        # the winsock library (_socket module)
        w_posix = self.space.builtin_modules[posixname]
        w_demo = self.space.builtin_modules['_demo']

        w_posix._cleanup_()
        assert w_posix.startup_called == False
        w_demo._cleanup_() # w_demo.appleveldefs['DemoError'] imports posix
        assert w_posix.startup_called == False