summaryrefslogtreecommitdiffstats
path: root/Modules/_struct.c
diff options
context:
space:
mode:
authordenballakh <47365157+denballakh@users.noreply.github.com>2023-08-26 09:54:16 (GMT)
committerGitHub <noreply@github.com>2023-08-26 09:54:16 (GMT)
commite407cea1938b80b1d469f148a4ea65587820e3eb (patch)
tree21b9df69dbdb5ffff761f94107c10a8f59ea6768 /Modules/_struct.c
parent8ba47146111d714c7b61825d43b52311d9be366d (diff)
downloadcpython-e407cea1938b80b1d469f148a4ea65587820e3eb.zip
cpython-e407cea1938b80b1d469f148a4ea65587820e3eb.tar.gz
cpython-e407cea1938b80b1d469f148a4ea65587820e3eb.tar.bz2
gh-107406: Add better `struct.Struct` repr (#107407)
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 606ae5e..4da6542 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -2165,6 +2165,19 @@ s_sizeof(PyStructObject *self, void *unused)
return PyLong_FromSize_t(size);
}
+static PyObject *
+s_repr(PyStructObject *self)
+{
+ PyObject* fmt = PyUnicode_FromStringAndSize(
+ PyBytes_AS_STRING(self->s_format), PyBytes_GET_SIZE(self->s_format));
+ if (fmt == NULL) {
+ return NULL;
+ }
+ PyObject* s = PyUnicode_FromFormat("%s(%R)", _PyType_Name(Py_TYPE(self)), fmt);
+ Py_DECREF(fmt);
+ return s;
+}
+
/* List of functions */
static struct PyMethodDef s_methods[] = {
@@ -2197,6 +2210,7 @@ static PyType_Slot PyStructType_slots[] = {
{Py_tp_dealloc, s_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_setattro, PyObject_GenericSetAttr},
+ {Py_tp_repr, s_repr},
{Py_tp_doc, (void*)s__doc__},
{Py_tp_traverse, s_traverse},
{Py_tp_clear, s_clear},