diff --git a/jedi/evaluate/compiled/__init__.py b/jedi/evaluate/compiled/__init__.py index 062f14a8..ed3d01ae 100644 --- a/jedi/evaluate/compiled/__init__.py +++ b/jedi/evaluate/compiled/__init__.py @@ -15,6 +15,13 @@ from jedi.evaluate.helpers import FakeName from . import fake +_sep = os.path.sep +if os.path.altsep is not None: + _sep += os.path.altsep +_path_re = re.compile('(?:\.[^{0}]+|[{0}]__init__\.py)$'.format(re.escape(_sep))) +del _sep + + class CompiledObject(Base): # comply with the parser start_pos = 0, 0 @@ -220,21 +227,27 @@ def dotted_from_fs_path(fs_path, sys_path=None): compares the path with sys.path and then returns the dotted_path. If the path is not in the sys.path, just returns None. """ - sep = os.path.sep - shortest = None if sys_path is None: sys_path = get_sys_path() + + # prefer + # - UNIX + # /path/to/pythonX.Y/lib-dynload + # /path/to/pythonX.Y/site-packages + # - Windows + # C:\path\to\DLLs + # C:\path\to\Lib\site-packages + # over + # - UNIX + # /path/to/pythonX.Y + # - Windows + # C:\path\to\Lib + path = '' for s in sys_path: - if fs_path.startswith(s): - path = fs_path[len(s):].strip(sep) - path = re.sub('\.[^%s]*|%s__init__.py$' % (sep, sep), '', path) - dotted = path.split(sep) - dotted = '.'.join(dotted) - # At this point dotted could be both `lib-dynload.datetime` and - # `datetime`. The shorter one is typically the module we want. - if shortest is None or len(shortest) > len(dotted): - shortest = dotted - return shortest + if (fs_path.startswith(s) and + len(path) < len(s)): + path = s + return _path_re.sub('', fs_path[len(path):].lstrip(os.path.sep)).replace(os.path.sep, '.') def load_module(path, name): diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index 9bd2d10b..1d7c4937 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -103,7 +103,8 @@ class ImportWrapper(pr.Base): names += self._get_module_names([path]) if self._is_relative_import(): - rel_path = self._importer.get_relative_path() + '/__init__.py' + rel_path = os.path.join(self._importer.get_relative_path(), + '__init__.py') if os.path.exists(rel_path): m = _load_module(rel_path) names += m.get_defined_names() @@ -459,7 +460,7 @@ class _Importer(object): if is_package_directory or current_namespace[0]: # is a directory module if is_package_directory: - path += '/__init__.py' + path = os.path.join(path, '__init__.py') with open(path, 'rb') as f: source = f.read() else: diff --git a/test/run.py b/test/run.py index d912d634..12aa520e 100755 --- a/test/run.py +++ b/test/run.py @@ -139,7 +139,7 @@ class IntegrationTestCase(object): @property def module_name(self): - return re.sub('.*/|\.py$', '', self.path) + return os.path.splitext(os.path.basename(self.path))[0] @property def line_nr_test(self): diff --git a/test/test_evaluate/extensions/README.rst b/test/test_evaluate/extensions/README.rst deleted file mode 100644 index 5f0b69ff..00000000 --- a/test/test_evaluate/extensions/README.rst +++ /dev/null @@ -1,10 +0,0 @@ -This directory contains pre-compiled extensions modules used to test completions -for compiled modules on Travis-CI (Ubuntu 12.04 64bit). - -To build the extensions modules, run:: - - python setup.py build_ext -i - - -Then move the compiled modules to their testing package ( ./**compiledXX**, where XX is the -python version used to run setup.py). \ No newline at end of file diff --git a/test/test_evaluate/extensions/compiled.pyx b/test/test_evaluate/extensions/compiled.pyx deleted file mode 100644 index 1e7c6907..00000000 --- a/test/test_evaluate/extensions/compiled.pyx +++ /dev/null @@ -1,6 +0,0 @@ -cdef class Foo: - pass - - -cdef class Bar: - pass diff --git a/test/test_evaluate/extensions/compiled27/__init__.py b/test/test_evaluate/extensions/compiled27/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/test/test_evaluate/extensions/compiled27/compiled.so b/test/test_evaluate/extensions/compiled27/compiled.so deleted file mode 100755 index becbfb56..00000000 Binary files a/test/test_evaluate/extensions/compiled27/compiled.so and /dev/null differ diff --git a/test/test_evaluate/extensions/compiled32/__init__.py b/test/test_evaluate/extensions/compiled32/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/test/test_evaluate/extensions/compiled32/compiled.so b/test/test_evaluate/extensions/compiled32/compiled.so deleted file mode 100755 index df34cfa0..00000000 Binary files a/test/test_evaluate/extensions/compiled32/compiled.so and /dev/null differ diff --git a/test/test_evaluate/extensions/setup.py b/test/test_evaluate/extensions/setup.py deleted file mode 100644 index a13237d2..00000000 --- a/test/test_evaluate/extensions/setup.py +++ /dev/null @@ -1,6 +0,0 @@ -from distutils.core import setup -from Cython.Build import cythonize - -setup( - ext_modules=cythonize("compiled.pyx") -) diff --git a/test/test_evaluate/test_buildout_detection.py b/test/test_evaluate/test_buildout_detection.py index a279c5ff..70c0da1f 100644 --- a/test/test_evaluate/test_buildout_detection.py +++ b/test/test_evaluate/test_buildout_detection.py @@ -1,12 +1,12 @@ import os -from ..helpers import cwd_at + from jedi._compatibility import u +from jedi.evaluate.sys_path import (_get_parent_dir_with_file, + _get_buildout_scripts, + _check_module) from jedi.parser import Parser -from jedi.evaluate.sys_path import ( - _get_parent_dir_with_file, - _get_buildout_scripts, - _check_module -) + +from ..helpers import cwd_at @cwd_at('test/test_evaluate/buildout_project/src/proj_name') @@ -14,7 +14,7 @@ def test_parent_dir_with_file(): parent = _get_parent_dir_with_file( os.path.abspath(os.curdir), 'buildout.cfg') assert parent is not None - assert parent.endswith('test/test_evaluate/buildout_project') + assert parent.endswith(os.path.join('test', 'test_evaluate', 'buildout_project')) @cwd_at('test/test_evaluate/buildout_project/src/proj_name') diff --git a/test/test_evaluate/test_extension.py b/test/test_evaluate/test_extension.py index 2f198ea7..cf15b55c 100644 --- a/test/test_evaluate/test_extension.py +++ b/test/test_evaluate/test_extension.py @@ -2,41 +2,30 @@ Test compiled module """ import os -import platform -import sys + import jedi -from ..helpers import cwd_at - -@cwd_at('test/test_evaluate/extensions') def test_completions(): - if platform.architecture()[0] == '64bit': - package_name = "compiled%s%s" % sys.version_info[:2] - sys.path.insert(0, os.getcwd()) - if os.path.exists(package_name): - s = jedi.Script("from %s import compiled; compiled." % package_name) - assert len(s.completions()) >= 2 + s = jedi.Script('import _ctypes; _ctypes.') + assert len(s.completions()) >= 15 -@cwd_at('test/test_evaluate/extensions') def test_call_signatures_extension(): - # with a cython extension - if platform.architecture()[0] == '64bit': - package_name = "compiled%s%s" % sys.version_info[:2] - sys.path.insert(0, os.getcwd()) - if os.path.exists(package_name): - s = jedi.Script("from %s import compiled; compiled.Foo(" % - package_name) - defs = s.call_signatures() - for call_def in defs: - for param in call_def.params: - pass + if os.name == 'nt': + func = 'LoadLibrary' + params = 1 + else: + func = 'dlopen' + params = 2 + s = jedi.Script('import _ctypes; _ctypes.%s(' % (func,)) + sigs = s.call_signatures() + assert len(sigs) == 1 + assert len(sigs[0].params) == params def test_call_signatures_stdlib(): - code = "import math; math.cos(" - s = jedi.Script(code) - defs = s.call_signatures() - for call_def in defs: - assert len(call_def.params) == 1 + s = jedi.Script('import math; math.cos(') + sigs = s.call_signatures() + assert len(sigs) == 1 + assert len(sigs[0].params) == 1 diff --git a/test/test_parser/test_token.py b/test/test_parser/test_token.py index b9e6dbc5..0295040d 100644 --- a/test/test_parser/test_token.py +++ b/test/test_parser/test_token.py @@ -1,10 +1,7 @@ -from jedi import parser from jedi._compatibility import u +from jedi import parser -try: - import unittest2 as unittest -except ImportError: # pragma: no cover - import unittest +from ..helpers import unittest class TokenTest(unittest.TestCase): diff --git a/test/test_utils.py b/test/test_utils.py index 737c00a2..84b966ed 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,10 +1,15 @@ -import readline +try: + import readline +except ImportError: + readline = False from jedi import utils -from .helpers import TestCase, cwd_at + +from .helpers import unittest, cwd_at -class TestSetupReadline(TestCase): +@unittest.skipIf(not readline, "readline not found") +class TestSetupReadline(unittest.TestCase): class NameSpace(object): pass