Skip to content

Commit 0c937b3

Browse files
Issue python#22031: Reprs now always use hexadecimal format with the "0x" prefix
when contain an id in form " at 0x...".
1 parent fbc877b commit 0c937b3

File tree

8 files changed

+19
-8
lines changed

8 files changed

+19
-8
lines changed

Lib/ctypes/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class _FuncPtr(_CFuncPtr):
353353
self._handle = handle
354354

355355
def __repr__(self):
356-
return "<%s '%s', handle %x at %x>" % \
356+
return "<%s '%s', handle %x at %#x>" % \
357357
(self.__class__.__name__, self._name,
358358
(self._handle & (_sys.maxsize*2 + 1)),
359359
id(self) & (_sys.maxsize*2 + 1))

Lib/reprlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def repr_instance(self, x, level):
136136
# Bugs in x.__repr__() can cause arbitrary
137137
# exceptions -- then make up something
138138
except Exception:
139-
return '<%s instance at %x>' % (x.__class__.__name__, id(x))
139+
return '<%s instance at %#x>' % (x.__class__.__name__, id(x))
140140
if len(s) > self.maxother:
141141
i = max(0, (self.maxother-3)//2)
142142
j = max(0, self.maxother-3-i)

Lib/test/test_reprlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def test_instance(self):
123123
eq(r(i2), expected)
124124

125125
i3 = ClassWithFailingRepr()
126-
eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3)))
126+
eq(r(i3), ("<ClassWithFailingRepr instance at %#x>"%id(i3)))
127127

128128
s = r(ClassWithFailingRepr)
129129
self.assertTrue(s.startswith("<class "))

Lib/test/test_weakref.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,14 @@ def __eq__(self, other):
15361536
self.assertEqual(len(d), 0)
15371537
self.assertEqual(count, 2)
15381538

1539+
def test_make_weak_valued_dict_repr(self):
1540+
dict = weakref.WeakValueDictionary()
1541+
self.assertRegex(repr(dict), '<WeakValueDictionary at 0x.*>')
1542+
1543+
def test_make_weak_keyed_dict_repr(self):
1544+
dict = weakref.WeakKeyDictionary()
1545+
self.assertRegex(repr(dict), '<WeakKeyDictionary at 0x.*>')
1546+
15391547
from test import mapping_tests
15401548

15411549
class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):

Lib/test/test_xmlrpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def test_datetime_datetime(self):
287287
def test_repr(self):
288288
d = datetime.datetime(2007,1,2,3,4,5)
289289
t = xmlrpclib.DateTime(d)
290-
val ="<DateTime '20070102T03:04:05' at %x>" % id(t)
290+
val ="<DateTime '20070102T03:04:05' at %#x>" % id(t)
291291
self.assertEqual(repr(t), val)
292292

293293
def test_decode(self):

Lib/weakref.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def __contains__(self, key):
144144
return o is not None
145145

146146
def __repr__(self):
147-
return "<WeakValueDictionary at %s>" % id(self)
147+
return "<WeakValueDictionary at %#x>" % id(self)
148148

149149
def __setitem__(self, key, value):
150150
if self._pending_removals:
@@ -348,7 +348,7 @@ def __len__(self):
348348
return len(self.data) - len(self._pending_removals)
349349

350350
def __repr__(self):
351-
return "<WeakKeyDictionary at %s>" % id(self)
351+
return "<WeakKeyDictionary at %#x>" % id(self)
352352

353353
def __setitem__(self, key, value):
354354
self.data[ref(key, self._remove)] = value

Lib/xmlrpc/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def __str__(self):
354354
return self.value
355355

356356
def __repr__(self):
357-
return "<DateTime %r at %x>" % (self.value, id(self))
357+
return "<DateTime %r at %#x>" % (self.value, id(self))
358358

359359
def decode(self, data):
360360
self.value = str(data).strip()
@@ -846,7 +846,7 @@ def __init__(self, server):
846846
self.__call_list = []
847847

848848
def __repr__(self):
849-
return "<MultiCall at %x>" % id(self)
849+
return "<MultiCall at %#x>" % id(self)
850850

851851
__str__ = __repr__
852852

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Core and Builtins
108108
Library
109109
-------
110110

111+
- Issue #22031: Reprs now always use hexadecimal format with the "0x" prefix
112+
when contain an id in form " at 0x...".
113+
111114
- Issue #22018: signal.set_wakeup_fd() now raises an OSError instead of a
112115
ValueError on ``fstat()`` failure.
113116

0 commit comments

Comments
 (0)