Remove the u() unicode function

This commit is contained in:
Dave Halter
2020-07-02 10:35:39 +02:00
parent 7f67324210
commit f1366b8a74
7 changed files with 27 additions and 42 deletions

View File

@@ -134,18 +134,7 @@ class ImplicitNSInfo(object):
self.paths = paths
def u(string, errors='strict'):
"""Cast to unicode DAMMIT!
Written because Python2 repr always implicitly casts to a string, so we
have to cast back to a unicode (and we now that we always deal with valid
unicode, because we check that in the beginning).
"""
if isinstance(string, bytes):
return str(string, encoding='UTF-8', errors=errors)
return string
def cast_path(obj):
def cast_path(string):
"""
Take a bytes or str path and cast it to unicode.
@@ -156,7 +145,9 @@ def cast_path(obj):
Since this just really complicates everything and Python 2.7 will be EOL
soon anyway, just go with always strings.
"""
return u(obj, errors='replace')
if isinstance(string, bytes):
return str(string, encoding='UTF-8', errors='replace')
return string
def pickle_load(file):

View File

@@ -11,7 +11,6 @@ from inspect import Parameter
from parso.python.parser import Parser
from parso.python import tree
from jedi._compatibility import u
from jedi.inference.base_value import NO_VALUES
from jedi.inference.syntax_tree import infer_atom
from jedi.inference.helpers import infer_call_of_leaf
@@ -85,18 +84,18 @@ def _get_code_for_stack(code_lines, leaf, position):
# If we're not on a comment simply get the previous leaf and proceed.
leaf = leaf.get_previous_leaf()
if leaf is None:
return u('') # At the beginning of the file.
return '' # At the beginning of the file.
is_after_newline = leaf.type == 'newline'
while leaf.type == 'newline':
leaf = leaf.get_previous_leaf()
if leaf is None:
return u('')
return ''
if leaf.type == 'error_leaf' or leaf.type == 'string':
if leaf.start_pos[0] < position[0]:
# On a different line, we just begin anew.
return u('')
return ''
# Error leafs cannot be parsed, completion in strings is also
# impossible.
@@ -112,7 +111,7 @@ def _get_code_for_stack(code_lines, leaf, position):
if user_stmt.start_pos[1] > position[1]:
# This means that it's actually a dedent and that means that we
# start without value (part of a suite).
return u('')
return ''
# This is basically getting the relevant lines.
return _get_code(code_lines, user_stmt.get_start_pos_of_prefix(), position)

View File

@@ -2,8 +2,6 @@ import os
import time
from contextlib import contextmanager
from jedi._compatibility import u
_inited = False
@@ -104,7 +102,7 @@ def dbg(message, *args, color='GREEN'):
if debug_function and enable_notice:
i = ' ' * _debug_indent
_lazy_colorama_init()
debug_function(color, i + 'dbg: ' + message % tuple(u(repr(a)) for a in args))
debug_function(color, i + 'dbg: ' + message % tuple(repr(a) for a in args))
def warning(message, *args, **kwargs):
@@ -114,7 +112,7 @@ def warning(message, *args, **kwargs):
if debug_function and enable_warning:
i = ' ' * _debug_indent
if format:
message = message % tuple(u(repr(a)) for a in args)
message = message % tuple(repr(a) for a in args)
debug_function('RED', i + 'warning: ' + message)

View File

@@ -438,13 +438,12 @@ def get_global_filters(context, until_position, origin_scope):
For global name lookups. The filters will handle name resolution
themselves, but here we gather possible filters downwards.
>>> from jedi._compatibility import u
>>> from jedi import Script
>>> script = Script(u('''
>>> script = Script('''
... x = ['a', 'b', 'c']
... def func():
... y = None
... '''))
... ''')
>>> module_node = script._module_node
>>> scope = next(module_node.iter_funcdefs())
>>> scope

View File

@@ -21,7 +21,6 @@ from textwrap import dedent
from parso import parse, ParserSyntaxError
from jedi._compatibility import u
from jedi import debug
from jedi.common import indent_block
from jedi.inference.cache import inference_state_method_cache
@@ -184,7 +183,7 @@ def _strip_rst_role(type_str):
def _infer_for_statement_string(module_context, string):
code = dedent(u("""
code = dedent("""
def pseudo_docstring_stuff():
'''
Create a pseudo function for docstring statements.
@@ -192,7 +191,7 @@ def _infer_for_statement_string(module_context, string):
is still a function.
'''
{}
"""))
""")
if string is None:
return []

View File

@@ -18,7 +18,6 @@ from docopt import docopt
from jedi.parser.python import load_grammar
from jedi.parser.diff import DiffParser
from jedi.parser.python import ParserWithRecovery
from jedi._compatibility import u
from jedi.common import splitlines
import jedi
@@ -37,14 +36,15 @@ def main(args):
with open(args['<file>']) as f:
code = f.read()
grammar = load_grammar()
parser = ParserWithRecovery(grammar, u(code))
parser = ParserWithRecovery(grammar, code)
# Make sure used_names is loaded
parser.module.used_names
code = code + '\na\n' # Add something so the diff parser needs to run.
code = code + '\na\n' # Add something so the diff parser needs to run.
lines = splitlines(code, keepends=True)
cProfile.runctx('run(parser, lines)', globals(), locals(), sort=args['-s'])
if __name__ == '__main__':
args = docopt(__doc__)
main(args)

View File

@@ -1,7 +1,6 @@
"""
All character set and unicode related tests.
"""
from jedi._compatibility import u
from jedi import Project
@@ -11,12 +10,12 @@ def test_unicode_script(Script):
assert len(completions)
assert type(completions[0].description) is str
s = u("author='öä'; author")
s = "author='öä'; author"
completions = Script(s).complete()
x = completions[0].description
assert type(x) is str
s = u("#-*- coding: iso-8859-1 -*-\nauthor='öä'; author")
s = "#-*- coding: iso-8859-1 -*-\nauthor='öä'; author"
s = s.encode('latin-1')
completions = Script(s).complete()
assert type(completions[0].description) is str
@@ -24,21 +23,21 @@ def test_unicode_script(Script):
def test_unicode_attribute(Script):
""" github jedi-vim issue #94 """
s1 = u('#-*- coding: utf-8 -*-\nclass Person():\n'
' name = "e"\n\nPerson().name.')
s1 = ('#-*- coding: utf-8 -*-\nclass Person():\n'
' name = "e"\n\nPerson().name.')
completions1 = Script(s1).complete()
assert 'strip' in [c.name for c in completions1]
s2 = u('#-*- coding: utf-8 -*-\nclass Person():\n'
' name = "é"\n\nPerson().name.')
s2 = ('#-*- coding: utf-8 -*-\nclass Person():\n'
' name = "é"\n\nPerson().name.')
completions2 = Script(s2).complete()
assert 'strip' in [c.name for c in completions2]
def test_multibyte_script(Script):
""" `jedi.Script` must accept multi-byte string source. """
code = u("import datetime; datetime.d")
comment = u("# multi-byte comment あいうえおä")
s = (u('%s\n%s') % (code, comment))
code = "import datetime; datetime.d"
comment = "# multi-byte comment あいうえおä"
s = ('%s\n%s') % (code, comment)
assert len(Script(s).complete(1, len(code)))
@@ -63,7 +62,7 @@ def test_complete_at_zero(Script):
def test_wrong_encoding(Script, tmpdir):
x = 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') + u'ä'.encode('utf-8'))
x.write_binary('foobar = 1\nä'.encode('latin-1') + 'ä'.encode('utf-8'))
project = Project('.', sys_path=[tmpdir.strpath])
c, = Script('import x; x.foo', project=project).complete()