Debian Codegraph @

test/test_inference/test_pyc.py

raw symbols SCIP
1 """
2 Test completions from *.pyc files:
3
4 - generate a dummy python module
5 - compile the dummy module to generate a *.pyc
6 - delete the pure python dummy module
7 - try jedi on the generated *.pyc
8 """
9 import os
10 import shutil
11 import sys
12
13 import pytest
14
15 import jedi
16 from jedi.api.environment import SameEnvironment, InterpreterEnvironment
17
18
19 SRC = """class Foo:
20 pass
21
22 class Bar:
23 pass
24 """
25
26
27 @pytest.fixture
28 def pyc_project_path(tmpdir):
29 path = tmpdir.strpath
30 dummy_package_path = os.path.join(path, "dummy_package")
31 os.mkdir(dummy_package_path)
32 with open(os.path.join(dummy_package_path, "__init__.py"), 'w', newline=''):
33 pass
34
35 dummy_path = os.path.join(dummy_package_path, 'dummy.py')
36 with open(dummy_path, 'w', newline='') as f:
37 f.write(SRC)
38 import compileall
39 compileall.compile_file(dummy_path)
40 os.remove(dummy_path)
41
42 # To import pyc modules, we must move them out of the __pycache__
43 # directory and rename them to remove ".cpython-%s%d"
44 # see: http://stackoverflow.com/questions/11648440/python-does-not-detect-pyc-files
45 pycache = os.path.join(dummy_package_path, "__pycache__")
46 for f in os.listdir(pycache):
47 dst = f.replace('.cpython-%s%s' % sys.version_info[:2], "")
48 dst = os.path.join(dummy_package_path, dst)
49 shutil.copy(os.path.join(pycache, f), dst)
50 try:
51 yield path
52 finally:
53 shutil.rmtree(path)
54
55
56 @pytest.mark.parametrize('load_unsafe_extensions', [False, True])
57 def test_pyc(pyc_project_path, environment, load_unsafe_extensions):
58 """
59 The list of completion must be greater than 2.
60 """
61 path = os.path.join(pyc_project_path, 'blub.py')
62 if not isinstance(environment, InterpreterEnvironment):
63 # We are using the same version for pyc completions here, because it
64 # was compiled in that version. However with interpreter environments
65 # we also have the same version and it's easier to debug.
66 environment = SameEnvironment()
67 environment = environment
68 project = jedi.Project(pyc_project_path, load_unsafe_extensions=load_unsafe_extensions)
69 s = jedi.Script(
70 "from dummy_package import dummy; dummy.",
71 path=path,
72 environment=environment,
73 project=project,
74 )
75 if load_unsafe_extensions:
76 assert len(s.complete()) >= 2
77 else:
78 assert not s.complete()