File: test_pyc.py

package info (click to toggle)
python-jedi 0.10.0~git1%2Bf05c071-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,064 kB
  • ctags: 3,014
  • sloc: python: 16,997; makefile: 149; ansic: 13
file content (64 lines) | stat: -rw-r--r-- 1,684 bytes parent folder | download | duplicates (2)
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
"""
Test completions from *.pyc files:

 - generate a dummy python module
 - compile the dummy module to generate a *.pyc
 - delete the pure python dummy module
 - try jedi on the generated *.pyc
"""
import os
import shutil
import sys

import pytest

import jedi
from ..helpers import cwd_at


SRC = """class Foo:
    pass

class Bar:
    pass
"""


def generate_pyc():
    os.mkdir("dummy_package")
    with open("dummy_package/__init__.py", 'w'):
        pass
    with open("dummy_package/dummy.py", 'w') as f:
        f.write(SRC)
    import compileall
    compileall.compile_file("dummy_package/dummy.py")
    os.remove("dummy_package/dummy.py")

    if sys.version_info[0] == 3:
        # Python3 specific:
        # To import pyc modules, we must move them out of the __pycache__
        # directory and rename them to remove ".cpython-%s%d"
        # see: http://stackoverflow.com/questions/11648440/python-does-not-detect-pyc-files
        for f in os.listdir("dummy_package/__pycache__"):
            dst = f.replace('.cpython-%s%s' % sys.version_info[:2], "")
            dst = os.path.join("dummy_package", dst)
            shutil.copy(os.path.join("dummy_package/__pycache__", f), dst)


# Python 2.6 does not necessarily come with `compileall.compile_file`.
@pytest.mark.skipif("sys.version_info > (2,6)")
@cwd_at('test/test_evaluate')
def test_pyc():
    """
    The list of completion must be greater than 2.
    """
    try:
        generate_pyc()
        s = jedi.Script("from dummy_package import dummy; dummy.", path='blub.py')
        assert len(s.completions()) >= 2
    finally:
        shutil.rmtree("dummy_package")


if __name__ == "__main__":
    test_pyc()