mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 05:54:25 +08:00
Remove the u() unicode function
This commit is contained in:
@@ -134,18 +134,7 @@ class ImplicitNSInfo(object):
|
|||||||
self.paths = paths
|
self.paths = paths
|
||||||
|
|
||||||
|
|
||||||
def u(string, errors='strict'):
|
def cast_path(string):
|
||||||
"""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):
|
|
||||||
"""
|
"""
|
||||||
Take a bytes or str path and cast it to unicode.
|
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
|
Since this just really complicates everything and Python 2.7 will be EOL
|
||||||
soon anyway, just go with always strings.
|
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):
|
def pickle_load(file):
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ from inspect import Parameter
|
|||||||
from parso.python.parser import Parser
|
from parso.python.parser import Parser
|
||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
|
|
||||||
from jedi._compatibility import u
|
|
||||||
from jedi.inference.base_value import NO_VALUES
|
from jedi.inference.base_value import NO_VALUES
|
||||||
from jedi.inference.syntax_tree import infer_atom
|
from jedi.inference.syntax_tree import infer_atom
|
||||||
from jedi.inference.helpers import infer_call_of_leaf
|
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.
|
# If we're not on a comment simply get the previous leaf and proceed.
|
||||||
leaf = leaf.get_previous_leaf()
|
leaf = leaf.get_previous_leaf()
|
||||||
if leaf is None:
|
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'
|
is_after_newline = leaf.type == 'newline'
|
||||||
while leaf.type == 'newline':
|
while leaf.type == 'newline':
|
||||||
leaf = leaf.get_previous_leaf()
|
leaf = leaf.get_previous_leaf()
|
||||||
if leaf is None:
|
if leaf is None:
|
||||||
return u('')
|
return ''
|
||||||
|
|
||||||
if leaf.type == 'error_leaf' or leaf.type == 'string':
|
if leaf.type == 'error_leaf' or leaf.type == 'string':
|
||||||
if leaf.start_pos[0] < position[0]:
|
if leaf.start_pos[0] < position[0]:
|
||||||
# On a different line, we just begin anew.
|
# On a different line, we just begin anew.
|
||||||
return u('')
|
return ''
|
||||||
|
|
||||||
# Error leafs cannot be parsed, completion in strings is also
|
# Error leafs cannot be parsed, completion in strings is also
|
||||||
# impossible.
|
# impossible.
|
||||||
@@ -112,7 +111,7 @@ def _get_code_for_stack(code_lines, leaf, position):
|
|||||||
if user_stmt.start_pos[1] > position[1]:
|
if user_stmt.start_pos[1] > position[1]:
|
||||||
# This means that it's actually a dedent and that means that we
|
# This means that it's actually a dedent and that means that we
|
||||||
# start without value (part of a suite).
|
# start without value (part of a suite).
|
||||||
return u('')
|
return ''
|
||||||
|
|
||||||
# This is basically getting the relevant lines.
|
# This is basically getting the relevant lines.
|
||||||
return _get_code(code_lines, user_stmt.get_start_pos_of_prefix(), position)
|
return _get_code(code_lines, user_stmt.get_start_pos_of_prefix(), position)
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ import os
|
|||||||
import time
|
import time
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from jedi._compatibility import u
|
|
||||||
|
|
||||||
_inited = False
|
_inited = False
|
||||||
|
|
||||||
|
|
||||||
@@ -104,7 +102,7 @@ def dbg(message, *args, color='GREEN'):
|
|||||||
if debug_function and enable_notice:
|
if debug_function and enable_notice:
|
||||||
i = ' ' * _debug_indent
|
i = ' ' * _debug_indent
|
||||||
_lazy_colorama_init()
|
_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):
|
def warning(message, *args, **kwargs):
|
||||||
@@ -114,7 +112,7 @@ def warning(message, *args, **kwargs):
|
|||||||
if debug_function and enable_warning:
|
if debug_function and enable_warning:
|
||||||
i = ' ' * _debug_indent
|
i = ' ' * _debug_indent
|
||||||
if format:
|
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)
|
debug_function('RED', i + 'warning: ' + message)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -438,13 +438,12 @@ def get_global_filters(context, until_position, origin_scope):
|
|||||||
For global name lookups. The filters will handle name resolution
|
For global name lookups. The filters will handle name resolution
|
||||||
themselves, but here we gather possible filters downwards.
|
themselves, but here we gather possible filters downwards.
|
||||||
|
|
||||||
>>> from jedi._compatibility import u
|
|
||||||
>>> from jedi import Script
|
>>> from jedi import Script
|
||||||
>>> script = Script(u('''
|
>>> script = Script('''
|
||||||
... x = ['a', 'b', 'c']
|
... x = ['a', 'b', 'c']
|
||||||
... def func():
|
... def func():
|
||||||
... y = None
|
... y = None
|
||||||
... '''))
|
... ''')
|
||||||
>>> module_node = script._module_node
|
>>> module_node = script._module_node
|
||||||
>>> scope = next(module_node.iter_funcdefs())
|
>>> scope = next(module_node.iter_funcdefs())
|
||||||
>>> scope
|
>>> scope
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ from textwrap import dedent
|
|||||||
|
|
||||||
from parso import parse, ParserSyntaxError
|
from parso import parse, ParserSyntaxError
|
||||||
|
|
||||||
from jedi._compatibility import u
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.common import indent_block
|
from jedi.common import indent_block
|
||||||
from jedi.inference.cache import inference_state_method_cache
|
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):
|
def _infer_for_statement_string(module_context, string):
|
||||||
code = dedent(u("""
|
code = dedent("""
|
||||||
def pseudo_docstring_stuff():
|
def pseudo_docstring_stuff():
|
||||||
'''
|
'''
|
||||||
Create a pseudo function for docstring statements.
|
Create a pseudo function for docstring statements.
|
||||||
@@ -192,7 +191,7 @@ def _infer_for_statement_string(module_context, string):
|
|||||||
is still a function.
|
is still a function.
|
||||||
'''
|
'''
|
||||||
{}
|
{}
|
||||||
"""))
|
""")
|
||||||
if string is None:
|
if string is None:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ from docopt import docopt
|
|||||||
from jedi.parser.python import load_grammar
|
from jedi.parser.python import load_grammar
|
||||||
from jedi.parser.diff import DiffParser
|
from jedi.parser.diff import DiffParser
|
||||||
from jedi.parser.python import ParserWithRecovery
|
from jedi.parser.python import ParserWithRecovery
|
||||||
from jedi._compatibility import u
|
|
||||||
from jedi.common import splitlines
|
from jedi.common import splitlines
|
||||||
import jedi
|
import jedi
|
||||||
|
|
||||||
@@ -37,14 +36,15 @@ def main(args):
|
|||||||
with open(args['<file>']) as f:
|
with open(args['<file>']) as f:
|
||||||
code = f.read()
|
code = f.read()
|
||||||
grammar = load_grammar()
|
grammar = load_grammar()
|
||||||
parser = ParserWithRecovery(grammar, u(code))
|
parser = ParserWithRecovery(grammar, code)
|
||||||
# Make sure used_names is loaded
|
# Make sure used_names is loaded
|
||||||
parser.module.used_names
|
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)
|
lines = splitlines(code, keepends=True)
|
||||||
cProfile.runctx('run(parser, lines)', globals(), locals(), sort=args['-s'])
|
cProfile.runctx('run(parser, lines)', globals(), locals(), sort=args['-s'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
args = docopt(__doc__)
|
args = docopt(__doc__)
|
||||||
main(args)
|
main(args)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
All character set and unicode related tests.
|
All character set and unicode related tests.
|
||||||
"""
|
"""
|
||||||
from jedi._compatibility import u
|
|
||||||
from jedi import Project
|
from jedi import Project
|
||||||
|
|
||||||
|
|
||||||
@@ -11,12 +10,12 @@ def test_unicode_script(Script):
|
|||||||
assert len(completions)
|
assert len(completions)
|
||||||
assert type(completions[0].description) is str
|
assert type(completions[0].description) is str
|
||||||
|
|
||||||
s = u("author='öä'; author")
|
s = "author='öä'; author"
|
||||||
completions = Script(s).complete()
|
completions = Script(s).complete()
|
||||||
x = completions[0].description
|
x = completions[0].description
|
||||||
assert type(x) is str
|
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')
|
s = s.encode('latin-1')
|
||||||
completions = Script(s).complete()
|
completions = Script(s).complete()
|
||||||
assert type(completions[0].description) is str
|
assert type(completions[0].description) is str
|
||||||
@@ -24,21 +23,21 @@ def test_unicode_script(Script):
|
|||||||
|
|
||||||
def test_unicode_attribute(Script):
|
def test_unicode_attribute(Script):
|
||||||
""" github jedi-vim issue #94 """
|
""" github jedi-vim issue #94 """
|
||||||
s1 = u('#-*- coding: utf-8 -*-\nclass Person():\n'
|
s1 = ('#-*- coding: utf-8 -*-\nclass Person():\n'
|
||||||
' name = "e"\n\nPerson().name.')
|
' name = "e"\n\nPerson().name.')
|
||||||
completions1 = Script(s1).complete()
|
completions1 = Script(s1).complete()
|
||||||
assert 'strip' in [c.name for c in completions1]
|
assert 'strip' in [c.name for c in completions1]
|
||||||
s2 = u('#-*- coding: utf-8 -*-\nclass Person():\n'
|
s2 = ('#-*- coding: utf-8 -*-\nclass Person():\n'
|
||||||
' name = "é"\n\nPerson().name.')
|
' name = "é"\n\nPerson().name.')
|
||||||
completions2 = Script(s2).complete()
|
completions2 = Script(s2).complete()
|
||||||
assert 'strip' in [c.name for c in completions2]
|
assert 'strip' in [c.name for c in completions2]
|
||||||
|
|
||||||
|
|
||||||
def test_multibyte_script(Script):
|
def test_multibyte_script(Script):
|
||||||
""" `jedi.Script` must accept multi-byte string source. """
|
""" `jedi.Script` must accept multi-byte string source. """
|
||||||
code = u("import datetime; datetime.d")
|
code = "import datetime; datetime.d"
|
||||||
comment = u("# multi-byte comment あいうえおä")
|
comment = "# multi-byte comment あいうえおä"
|
||||||
s = (u('%s\n%s') % (code, comment))
|
s = ('%s\n%s') % (code, comment)
|
||||||
assert len(Script(s).complete(1, len(code)))
|
assert len(Script(s).complete(1, len(code)))
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +62,7 @@ def test_complete_at_zero(Script):
|
|||||||
def test_wrong_encoding(Script, tmpdir):
|
def test_wrong_encoding(Script, tmpdir):
|
||||||
x = tmpdir.join('x.py')
|
x = tmpdir.join('x.py')
|
||||||
# Use both latin-1 and utf-8 (a really broken file).
|
# 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])
|
project = Project('.', sys_path=[tmpdir.strpath])
|
||||||
c, = Script('import x; x.foo', project=project).complete()
|
c, = Script('import x; x.foo', project=project).complete()
|
||||||
|
|||||||
Reference in New Issue
Block a user