forked from VimPlug/jedi
unicode strings should not raise an error if used in repr.
Python 2 doesn't allow unicode objects in __repr__ methods. Therefore we need to encode them as utf-8 bytes.
This commit is contained in:
@@ -180,3 +180,21 @@ def no_unicode_pprint(dct):
|
|||||||
import pprint
|
import pprint
|
||||||
s = pprint.pformat(dct)
|
s = pprint.pformat(dct)
|
||||||
print(re.sub("u'", "'", s))
|
print(re.sub("u'", "'", s))
|
||||||
|
|
||||||
|
|
||||||
|
def utf8_repr(func):
|
||||||
|
"""
|
||||||
|
``__repr__`` methods in Python 2 don't allow unicode objects to be
|
||||||
|
returned. Therefore cast them to utf-8 bytes in this decorator.
|
||||||
|
"""
|
||||||
|
def wrapper(self):
|
||||||
|
result = func(self)
|
||||||
|
if isinstance(result, unicode):
|
||||||
|
return result.encode('utf-8')
|
||||||
|
else:
|
||||||
|
return result
|
||||||
|
|
||||||
|
if is_py3:
|
||||||
|
return func
|
||||||
|
else:
|
||||||
|
return wrapper
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from jedi._compatibility import encoding, is_py3
|
from jedi._compatibility import encoding, is_py3, u
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
@@ -63,13 +63,13 @@ def dbg(message, *args):
|
|||||||
mod = inspect.getmodule(frm[0])
|
mod = inspect.getmodule(frm[0])
|
||||||
if not (mod.__name__ in ignored_modules):
|
if not (mod.__name__ in ignored_modules):
|
||||||
i = ' ' * _debug_indent
|
i = ' ' * _debug_indent
|
||||||
debug_function(NOTICE, i + 'dbg: ' + message % args)
|
debug_function(NOTICE, i + 'dbg: ' + message % tuple(u(repr(a)) for a in args))
|
||||||
|
|
||||||
|
|
||||||
def warning(message, *args):
|
def warning(message, *args):
|
||||||
if debug_function and enable_warning:
|
if debug_function and enable_warning:
|
||||||
i = ' ' * _debug_indent
|
i = ' ' * _debug_indent
|
||||||
debug_function(WARNING, i + 'warning: ' + message % args)
|
debug_function(WARNING, i + 'warning: ' + message % tuple(u(repr(a)) for a in args))
|
||||||
|
|
||||||
|
|
||||||
def speed(name):
|
def speed(name):
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ from inspect import cleandoc
|
|||||||
from itertools import chain
|
from itertools import chain
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
from jedi._compatibility import (Python3Method, encoding, is_py3,
|
from jedi._compatibility import (Python3Method, encoding, is_py3, utf8_repr,
|
||||||
literal_eval, use_metaclass, unicode)
|
literal_eval, use_metaclass, unicode)
|
||||||
from jedi import cache
|
from jedi import cache
|
||||||
|
|
||||||
@@ -223,8 +223,9 @@ class Leaf(Base):
|
|||||||
return None
|
return None
|
||||||
return self.parent.children[i - 1]
|
return self.parent.children[i - 1]
|
||||||
|
|
||||||
|
@utf8_repr
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s: %s>" % (type(self).__name__, repr(self.value))
|
return "<%s: %s>" % (type(self).__name__, self.value)
|
||||||
|
|
||||||
|
|
||||||
class LeafWithNewLines(Leaf):
|
class LeafWithNewLines(Leaf):
|
||||||
@@ -336,16 +337,6 @@ class Literal(LeafWithNewLines):
|
|||||||
def eval(self):
|
def eval(self):
|
||||||
return literal_eval(self.value)
|
return literal_eval(self.value)
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
# TODO remove?
|
|
||||||
"""
|
|
||||||
if is_py3:
|
|
||||||
s = self.literal
|
|
||||||
else:
|
|
||||||
s = self.literal.encode('ascii', 'replace')
|
|
||||||
"""
|
|
||||||
return "<%s: %s>" % (type(self).__name__, self.value)
|
|
||||||
|
|
||||||
|
|
||||||
class Number(Literal):
|
class Number(Literal):
|
||||||
type = 'number'
|
type = 'number'
|
||||||
@@ -472,6 +463,7 @@ class BaseNode(Base):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
return self.children[0]
|
return self.children[0]
|
||||||
|
|
||||||
|
@utf8_repr
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
code = self.get_code().replace('\n', ' ')
|
code = self.get_code().replace('\n', ' ')
|
||||||
if not is_py3:
|
if not is_py3:
|
||||||
|
|||||||
@@ -202,3 +202,8 @@ def test_param_splitting():
|
|||||||
|
|
||||||
check('def x(a, (b, c)):\n pass', ['a'])
|
check('def x(a, (b, c)):\n pass', ['a'])
|
||||||
check('def x((b, c)):\n pass', [])
|
check('def x((b, c)):\n pass', [])
|
||||||
|
|
||||||
|
|
||||||
|
def test_unicode_string():
|
||||||
|
s = pt.String(None, u'bö', (0, 0))
|
||||||
|
assert repr(s) # Should not raise an Error!
|
||||||
|
|||||||
Reference in New Issue
Block a user