diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index c5b586f6..f52f1391 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -369,7 +369,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/compiled/subprocess/functions.py b/jedi/evaluate/compiled/subprocess/functions.py index 543e7708..49b23733 100644 --- a/jedi/evaluate/compiled/subprocess/functions.py +++ b/jedi/evaluate/compiled/subprocess/functions.py @@ -49,7 +49,20 @@ def get_module_info(evaluator, sys_path=None, full_name=None, **kwargs): else: module_path = _get_init_path(module_path) elif module_file: - code = module_file.read() + 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() return code, cast_path(module_path), is_pkg diff --git a/jedi/evaluate/syntax_tree.py b/jedi/evaluate/syntax_tree.py index 98711943..3ffb6d3a 100644 --- a/jedi/evaluate/syntax_tree.py +++ b/jedi/evaluate/syntax_tree.py @@ -490,7 +490,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 diff --git a/test/test_api/test_unicode.py b/test/test_api/test_unicode.py index b4d11012..ede5757d 100644 --- a/test/test_api/test_unicode.py +++ b/test/test_api/test_unicode.py @@ -68,7 +68,7 @@ def test_complete_at_zero(Script): def test_wrong_encoding(Script, 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_precedence.py b/test/test_evaluate/test_precedence.py index fe3d4bc5..722d2d05 100644 --- a/test/test_evaluate/test_precedence.py +++ b/test/test_evaluate/test_precedence.py @@ -12,6 +12,6 @@ def test_equals(Script, environment, source): if environment.version_info.major < 3: pytest.skip("Ellipsis does not exists in 2") script = Script(source) - node = script._get_module_node().children[0] + node = script._module_node.children[0] first, = script._get_module().eval_node(node) assert isinstance(first, CompiledObject) and first.get_safe_value() is True diff --git a/test/test_evaluate/test_sys_path.py b/test/test_evaluate/test_sys_path.py index bd966b77..224d3471 100644 --- a/test/test_evaluate/test_sys_path.py +++ b/test/test_evaluate/test_sys_path.py @@ -12,7 +12,7 @@ from jedi.api.environment import Environment def test_paths_from_assignment(Script): 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'}