fix some python 3 compatibility things (which involves some real bugs, but py2 was passing)

This commit is contained in:
Dave Halter
2014-01-12 17:02:26 +01:00
parent e4f3f5bea2
commit c75cef0882
5 changed files with 16 additions and 17 deletions

View File

@@ -563,11 +563,10 @@ class Script(object):
match = re.match(r'^(.*?)(\.|)(\w?[\w\d]*)$', path, flags=re.S) match = re.match(r'^(.*?)(\.|)(\w?[\w\d]*)$', path, flags=re.S)
return match.groups() return match.groups()
@staticmethod def _sorted_defs(self, d):
def _sorted_defs(d):
# Note: `or ''` below is required because `module_path` could be # Note: `or ''` below is required because `module_path` could be
# None and you can't compare None and str in Python 3. # None and you can't compare None and str in Python 3.
return sorted(d, key=lambda x: (x.module_path or '', x.line, x.column)) return sorted(d, key=lambda x: (x.module_path or '', x.line or 0, x.column or 0))
class Interpreter(Script): class Interpreter(Script):

View File

@@ -173,11 +173,11 @@ class BaseDefinition(object):
The module name. The module name.
>>> from jedi import Script >>> from jedi import Script
>>> source = 'import datetime' >>> source = 'import json'
>>> script = Script(source, 1, len(source), 'example.py') >>> script = Script(source, path='example.py')
>>> d = script.goto_definitions()[0] >>> d = script.goto_definitions()[0]
>>> print(d.module_name) # doctest: +ELLIPSIS >>> print(d.module_name) # doctest: +ELLIPSIS
datetime json
""" """
return str(self._module.name) return str(self._module.name)

View File

@@ -154,6 +154,9 @@ def load_module(path, name):
name = os.path.basename(path) name = os.path.basename(path)
name = name.rpartition('.')[0] # cut file type (normally .so) name = name.rpartition('.')[0] # cut file type (normally .so)
# sometimes there are endings like `_sqlite3.cpython-32mu`
name = re.sub(r'\..*', '', name)
sys_path = get_sys_path() sys_path = get_sys_path()
if path: if path:
sys_path.insert(0, path) sys_path.insert(0, path)

View File

@@ -45,9 +45,6 @@ def _load_fakes(module_name):
raise NotImplementedError() raise NotImplementedError()
return funcs return funcs
# sometimes there are stupid endings like `_sqlite3.cpython-32mu`
module_name = re.sub(r'\..*', '', module_name)
if module_name == '__builtin__' and not is_py3k: if module_name == '__builtin__' and not is_py3k:
module_name = 'builtins' module_name = 'builtins'
path = os.path.dirname(os.path.abspath(__file__)) path = os.path.dirname(os.path.abspath(__file__))

View File

@@ -2,24 +2,23 @@
Test all things related to the ``jedi.api`` module. Test all things related to the ``jedi.api`` module.
""" """
from jedi import common, api from jedi import api
from pytest import raises from pytest import raises
def test_preload_modules(): def test_preload_modules():
def check_loaded(*modules): def check_loaded(*modules):
# +1 for None module (currently used) # +1 for None module (currently used)
assert len(new) == len(modules) + 1 assert len(parser_cache) == len(modules) + 1
for i in modules + ('__builtin__',): for i in modules:
assert [i in k for k in new.keys() if k is not None] assert [i in k for k in parser_cache.keys() if k is not None]
from jedi import cache from jedi import cache
temp_cache, cache.parser_cache = cache.parser_cache, {} temp_cache, cache.parser_cache = cache.parser_cache, {}
new = cache.parser_cache parser_cache = cache.parser_cache
with common.ignored(KeyError): # performance of tests -> no reload
new['__builtin__'] = temp_cache['__builtin__']
api.preload_module('datetime') api.preload_module('sys')
check_loaded() # compiled (c_builtin) modules shouldn't be in the cache.
api.preload_module('json', 'token') api.preload_module('json', 'token')
check_loaded('json', 'token') check_loaded('json', 'token')
@@ -29,6 +28,7 @@ def test_preload_modules():
def test_empty_script(): def test_empty_script():
assert api.Script('') assert api.Script('')
def test_line_number_errors(): def test_line_number_errors():
""" """
Script should raise a ValueError if line/column numbers are not in a Script should raise a ValueError if line/column numbers are not in a