mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-107406: Add better struct.Struct
repr (#107407)
This commit is contained in:
parent
8ba4714611
commit
e407cea193
4 changed files with 29 additions and 0 deletions
|
@ -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},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue