File: test_import.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (64 lines) | stat: -rw-r--r-- 2,502 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.import_ import *
from pypy.module.cpyext.import_ import (
    _PyImport_AcquireLock, _PyImport_ReleaseLock)
from rpython.rtyper.lltypesystem import rffi

class TestImport(BaseApiTest):
    def test_import(self, space):
        stat = PyImport_Import(space, space.wrap("stat"))
        assert stat
        assert space.getattr(stat, space.wrap("S_IMODE"))

    def test_addmodule(self, space):
        with rffi.scoped_str2charp("sys") as modname:
            w_sys = PyImport_AddModule(space, modname)
        assert w_sys is space.sys

        with rffi.scoped_str2charp("foobar") as modname:
            w_foobar = PyImport_AddModule(space, modname)
        assert space.str_w(space.getattr(w_foobar,
                                         space.wrap('__name__'))) == 'foobar'

    def test_getmoduledict(self, space, api):
        testmod = "imghdr"
        w_pre_dict = PyImport_GetModuleDict(space, )
        assert not space.contains_w(w_pre_dict, space.wrap(testmod))

        with rffi.scoped_str2charp(testmod) as modname:
            w_module = PyImport_ImportModule(space, modname)
            print w_module
            assert w_module

        w_dict = PyImport_GetModuleDict(space, )
        assert space.contains_w(w_dict, space.wrap(testmod))

    def test_reload(self, space):
        stat = PyImport_Import(space, space.wrap("stat"))
        space.delattr(stat, space.wrap("S_IMODE"))
        stat = PyImport_ReloadModule(space, stat)
        assert space.getattr(stat, space.wrap("S_IMODE"))

    def test_ImportModuleLevelObject(self, space):
        w_mod = PyImport_ImportModuleLevelObject(
            space, space.wrap('stat'), None, None, None, 0)
        assert w_mod
        assert space.getattr(w_mod, space.wrap("S_IMODE"))

    def test_lock(self, space):
        # "does not crash"
        _PyImport_AcquireLock(space, )
        _PyImport_AcquireLock(space, )
        _PyImport_ReleaseLock(space, )
        _PyImport_ReleaseLock(space, )


class AppTestImportLogic(AppTestCpythonExtensionBase):
    def test_import_logic(self):
        import sys, os
        path = self.compile_module('test_import_module',
            source_files=[os.path.join(self.here, 'test_import_module.c')])
        sys.path.append(os.path.dirname(path))
        import test_import_module
        assert test_import_module.TEST is None