From 147655125703da4eb69df23384d19932ebcc1a06 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 20 Jan 2018 19:33:47 +0100 Subject: [PATCH 1/4] Add better error reporting for potential issues --- jedi/evaluate/syntax_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jedi/evaluate/syntax_tree.py b/jedi/evaluate/syntax_tree.py index aa0a225c..be1b31f2 100644 --- a/jedi/evaluate/syntax_tree.py +++ b/jedi/evaluate/syntax_tree.py @@ -492,7 +492,7 @@ def tree_name_to_contexts(evaluator, context, tree_name): exceptions = context.eval_node(tree_name.get_previous_sibling().get_previous_sibling()) types = exceptions.execute_evaluated() else: - raise ValueError("Should not happen.") + raise ValueError("Should not happen. type: %s" % typ) return types From 9c0b344962b54db37e6c41769a817c88e9e01c35 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 20 Jan 2018 20:30:44 +0100 Subject: [PATCH 2/4] Small mistake when opening a file --- jedi/evaluate/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index 683c33a0..e377aa34 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -363,7 +363,7 @@ class Evaluator(object): def parse_and_get_code(self, code, path, **kwargs): if self.allow_different_encoding: if code is None: - with open('rb') as f: + with open(path, 'rb') as f: code = f.read() code = python_bytes_to_unicode(code, errors='replace') From 27a3be3b4284699bd2c8cbfab0d301be849b72dc Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 17 Dec 2017 21:35:39 +0100 Subject: [PATCH 3/4] Merge a commit that adds the build folder to the ignored paths --- conftest.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/conftest.py b/conftest.py index 2567fcde..f693b940 100644 --- a/conftest.py +++ b/conftest.py @@ -5,7 +5,12 @@ import pytest import jedi -collect_ignore = ["setup.py"] +collect_ignore = [ + 'setup.py', + '__main__.py', + 'jedi/evaluate/compiled/subprocess/__main__.py', + 'build/', +] # The following hooks (pytest_configure, pytest_unconfigure) are used From 20d64cf2b36fd3d43479efc9f824bfe2d554f5f9 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 20 Jan 2018 21:21:58 +0100 Subject: [PATCH 4/4] Fix issues with a recent refactoring --- jedi/evaluate/__init__.py | 2 +- jedi/evaluate/filters.py | 2 +- jedi/evaluate/imports.py | 15 +++++++++++++-- jedi/utils.py | 2 +- test/test_api/test_unicode.py | 2 +- test/test_evaluate/test_sys_path.py | 2 +- 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index e377aa34..125a9fb5 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -360,7 +360,7 @@ class Evaluator(object): scope_node = parent_scope(node) return from_scope_node(scope_node, is_nested=True, node_is_object=node_is_object) - def parse_and_get_code(self, code, path, **kwargs): + def parse_and_get_code(self, code=None, path=None, **kwargs): if self.allow_different_encoding: if code is None: with open(path, 'rb') as f: diff --git a/jedi/evaluate/filters.py b/jedi/evaluate/filters.py index 1b4754eb..50197bd3 100644 --- a/jedi/evaluate/filters.py +++ b/jedi/evaluate/filters.py @@ -379,7 +379,7 @@ def get_global_filters(evaluator, context, until_position, origin_scope): ... def func(): ... y = None ... ''')) - >>> module_node = script._get_module_node() + >>> module_node = script._module_node >>> scope = next(module_node.iter_funcdefs()) >>> scope diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index 97529172..f43255f8 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -369,9 +369,20 @@ class Importer(object): else: module_path = get_init_path(module_path) elif module_file: + if module_path.endswith(('.zip', '.egg')): + # Unfortunately we are reading unicode here already, not byes. + # It seems however hard to get bytes, because the zip importer + # logic just unpacks the zip file and returns a file descriptor + # that we cannot as easily access. Therefore we just read it as + # a string. + code = module_file.read() + else: + # Read the code with a binary file, because the binary file + # might not be proper unicode. This is handled by the parser + # wrapper. + with open(module_path, 'rb') as f: + code = f.read() module_file.close() - with open(module_path, 'rb') as f: - code = f.read() if isinstance(module_path, ImplicitNSInfo): from jedi.evaluate.context.namespace import ImplicitNamespaceContext diff --git a/jedi/utils.py b/jedi/utils.py index 177524c5..12b36b76 100644 --- a/jedi/utils.py +++ b/jedi/utils.py @@ -89,7 +89,7 @@ def setup_readline(namespace_module=__main__): lines = split_lines(text) position = (len(lines), len(lines[-1])) name = get_on_completion_name( - interpreter._get_module_node(), + interpreter._module_node, lines, position ) diff --git a/test/test_api/test_unicode.py b/test/test_api/test_unicode.py index 0ea1a8e6..5802730f 100644 --- a/test/test_api/test_unicode.py +++ b/test/test_api/test_unicode.py @@ -69,7 +69,7 @@ def test_complete_at_zero(): def test_wrong_encoding(cwd_tmpdir): x = cwd_tmpdir.join('x.py') # Use both latin-1 and utf-8 (a really broken file). - x.write_binary(u'foobar = 1\nä'.encode('latin-1') + 'ä'.encode()) + x.write_binary(u'foobar = 1\nä'.encode('latin-1') + u'ä'.encode('utf-8')) c, = Script('import x; x.foo', sys_path=['.']).completions() assert c.name == 'foobar' diff --git a/test/test_evaluate/test_sys_path.py b/test/test_evaluate/test_sys_path.py index e825912d..3d8f43f9 100644 --- a/test/test_evaluate/test_sys_path.py +++ b/test/test_evaluate/test_sys_path.py @@ -11,7 +11,7 @@ from jedi import Script def test_paths_from_assignment(): def paths(src): script = Script(src, path='/foo/bar.py') - expr_stmt = script._get_module_node().children[0] + expr_stmt = script._module_node.children[0] return set(sys_path._paths_from_assignment(script._get_module(), expr_stmt)) assert paths('sys.path[0:0] = ["a"]') == {'/foo/a'}