Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions Lib/reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def wrapper(self):
return decorating_function

class Repr:
_lookup = {
"repr_tuple": ("builtins", "tuple"),
"repr_list": ("builtins", "list"),
"repr_array": ("array", "array"),
"repr_set": ("builtins", "set"),
"repr_frozenset": ("builtins", "frozenset"),
"repr_deque": ("collections", "deque"),
"repr_dict": ("builtins", "dict"),
"repr_str": ("builtins", "str"),
"repr_int": ("builtins", "int"),
}

def __init__(
self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4,
Expand All @@ -60,15 +71,33 @@ def repr(self, x):
return self.repr1(x, self.maxlevel)

def repr1(self, x, level):
typename = type(x).__name__
if ' ' in typename:
_type = type(x)
typename = _type.__name__

if " " in typename:
parts = typename.split()
typename = '_'.join(parts)
if hasattr(self, 'repr_' + typename):
return getattr(self, 'repr_' + typename)(x, level)
else:
typename = "_".join(parts)

method_name = "repr_" + typename

if not hasattr(self, method_name):
return self.repr_instance(x, level)

# we have a predefined method for this type,
# but it has been set in a subclass
if method_name not in self._lookup:
return getattr(self, method_name)(x, level)

module = getattr(_type, "__module__", None)
is_shadowed = (module, typename) != self._lookup[method_name]

if is_shadowed:
# we have a predefined method for a class with
# the same name as x, but it is not x
return self.repr_instance(x, level)

return getattr(self, method_name)(x, level)

def _join(self, pieces, level):
if self.indent is None:
return ', '.join(pieces)
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,42 @@ def test_invalid_indent(self):
with self.assertRaisesRegex(expected_error, expected_msg):
r.repr(test_object)

def test_shadowed_builtin(self):
# Issue #113570: repr() should not be fooled by a shadowed builtin
# function
class array:
def __repr__(self):
return "not array.array"

self.assertEqual(r(array()), "not array.array")

def test_custom_repr(self):
class MyRepr(Repr):

def repr_TextIOWrapper(self, obj, level):
if obj.name in {'<stdin>', '<stdout>', '<stderr>'}:
return obj.name
return repr(obj)

aRepr = MyRepr()
self.assertEqual(aRepr.repr(sys.stdin), "<stdin>")

def test_custom_repr_class_with_spaces(self):
class TypeWithSpaces:
pass

t = TypeWithSpaces()
type(t).__name__ = "type with spaces"
self.assertEqual(type(t).__name__, "type with spaces")

class MyRepr(Repr):
def repr_type_with_spaces(self, obj, level):
return "Type With Spaces"


aRepr = MyRepr()
self.assertEqual(aRepr.repr(t), "Type With Spaces")

def write_file(path, text):
with open(path, 'w', encoding='ASCII') as fp:
fp.write(text)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug in ``reprlib.repr`` where it incorrectly called the repr method on shadowed Python built-in types.