44#include "pycore_ceval.h" // _PyEval_GetBuiltin()
55#include "pycore_modsupport.h" // _PyArg_NoKeywords()
66#include "pycore_object.h"
7+ #include "pycore_typevarobject.h" // _Py_typing_type_repr
78#include "pycore_unionobject.h" // _Py_union_type_or, _PyGenericAlias_Check
89
910
@@ -50,69 +51,6 @@ ga_traverse(PyObject *self, visitproc visit, void *arg)
5051 return 0 ;
5152}
5253
53- static int
54- ga_repr_item (PyUnicodeWriter * writer , PyObject * p )
55- {
56- PyObject * qualname = NULL ;
57- PyObject * module = NULL ;
58- int rc ;
59-
60- if (p == Py_Ellipsis ) {
61- // The Ellipsis object
62- rc = PyUnicodeWriter_WriteUTF8 (writer , "..." , 3 );
63- goto done ;
64- }
65-
66- if ((rc = PyObject_HasAttrWithError (p , & _Py_ID (__origin__ ))) > 0 &&
67- (rc = PyObject_HasAttrWithError (p , & _Py_ID (__args__ ))) > 0 )
68- {
69- // It looks like a GenericAlias
70- goto use_repr ;
71- }
72- if (rc < 0 ) {
73- goto error ;
74- }
75-
76- if (PyObject_GetOptionalAttr (p , & _Py_ID (__qualname__ ), & qualname ) < 0 ) {
77- goto error ;
78- }
79- if (qualname == NULL ) {
80- goto use_repr ;
81- }
82- if (PyObject_GetOptionalAttr (p , & _Py_ID (__module__ ), & module ) < 0 ) {
83- goto error ;
84- }
85- if (module == NULL || module == Py_None ) {
86- goto use_repr ;
87- }
88-
89- // Looks like a class
90- if (PyUnicode_Check (module ) &&
91- _PyUnicode_EqualToASCIIString (module , "builtins" ))
92- {
93- // builtins don't need a module name
94- rc = PyUnicodeWriter_WriteStr (writer , qualname );
95- goto done ;
96- }
97- else {
98- rc = PyUnicodeWriter_Format (writer , "%S.%S" , module , qualname );
99- goto done ;
100- }
101-
102- error :
103- rc = -1 ;
104- goto done ;
105-
106- use_repr :
107- rc = PyUnicodeWriter_WriteRepr (writer , p );
108- goto done ;
109-
110- done :
111- Py_XDECREF (qualname );
112- Py_XDECREF (module );
113- return rc ;
114- }
115-
11654static int
11755ga_repr_items_list (PyUnicodeWriter * writer , PyObject * p )
11856{
@@ -131,7 +69,7 @@ ga_repr_items_list(PyUnicodeWriter *writer, PyObject *p)
13169 }
13270 }
13371 PyObject * item = PyList_GET_ITEM (p , i );
134- if (ga_repr_item (writer , item ) < 0 ) {
72+ if (_Py_typing_type_repr (writer , item ) < 0 ) {
13573 return -1 ;
13674 }
13775 }
@@ -162,7 +100,7 @@ ga_repr(PyObject *self)
162100 goto error ;
163101 }
164102 }
165- if (ga_repr_item (writer , alias -> origin ) < 0 ) {
103+ if (_Py_typing_type_repr (writer , alias -> origin ) < 0 ) {
166104 goto error ;
167105 }
168106 if (PyUnicodeWriter_WriteChar (writer , '[' ) < 0 ) {
@@ -181,7 +119,7 @@ ga_repr(PyObject *self)
181119 goto error ;
182120 }
183121 }
184- else if (ga_repr_item (writer , p ) < 0 ) {
122+ else if (_Py_typing_type_repr (writer , p ) < 0 ) {
185123 goto error ;
186124 }
187125 }
0 commit comments