Skip to content

Commit 6507b80

Browse files
committed
Add repr(alt=True)
1 parent 5a86327 commit 6507b80

8 files changed

Lines changed: 86 additions & 7 deletions

File tree

‎Include/internal/pycore_global_objects_fini_generated.h‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_global_strings.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ struct _Py_global_strings {
310310
STRUCT_FOR_ID(all_threads)
311311
STRUCT_FOR_ID(allow_code)
312312
STRUCT_FOR_ID(alphabet)
313+
STRUCT_FOR_ID(alt)
313314
STRUCT_FOR_ID(any)
314315
STRUCT_FOR_ID(append)
315316
STRUCT_FOR_ID(arg)

‎Include/internal/pycore_runtime_init_generated.h‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_unicodeobject_generated.h‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Lib/test/test_string/_support.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def convert(value, conversion):
4848
return ascii(value)
4949
elif conversion == "r":
5050
return repr(value)
51+
elif conversion == "R":
52+
return repr(value, alt=True)
5153
elif conversion == "s":
5254
return str(value)
5355
return value

‎Lib/test/test_tstring.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ def test_conversions(self):
107107
self.assertEqual(fstring(t), f"ASCII: {ascii(text)}")
108108

109109
# Test !R conversion (repr)
110+
class StrSubclass(str):
111+
def __repr__(self):
112+
return '<custom repr>'
113+
obj = StrSubclass('abc')
110114
t = t"Data: {obj!R}"
111115
self.assertTStringEqual(t, ("Data: ", ""), [(obj, "obj", "R")])
112-
self.assertEqual(fstring(t), f"Data: {repr(obj)}")
116+
self.assertEqual(fstring(t), f"Data: 'abc'")
113117

114118
# Test !z conversion (error)
115119
num = 1

‎Python/bltinmodule.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,17 +2639,24 @@ repr as builtin_repr
26392639
26402640
obj: object
26412641
/
2642+
*
2643+
alt: bool = False
26422644
26432645
Return the canonical string representation of the object.
26442646
26452647
For many object types, including most builtins, eval(repr(obj)) == obj.
26462648
[clinic start generated code]*/
26472649

26482650
static PyObject *
2649-
builtin_repr(PyObject *module, PyObject *obj)
2650-
/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
2651+
builtin_repr_impl(PyObject *module, PyObject *obj, int alt)
2652+
/*[clinic end generated code: output=fc5168bb1b4c8846 input=0b1b2969c594dc9a]*/
26512653
{
2652-
return PyObject_Repr(obj);
2654+
if (!alt) {
2655+
return PyObject_Repr(obj);
2656+
}
2657+
else {
2658+
return _PyObject_AltRepr(obj);
2659+
}
26532660
}
26542661

26552662

‎Python/clinic/bltinmodule.c.h‎

Lines changed: 62 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)