Skip to content

Commit e407cea

Browse files
authored
gh-107406: Add better struct.Struct repr (#107407)
1 parent 8ba4714 commit e407cea

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

Doc/library/struct.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
:mod:`struct` --- Interpret bytes as packed binary data
22
=======================================================
33

4+
.. testsetup:: *
5+
6+
from struct import *
7+
48
.. module:: struct
59
:synopsis: Interpret bytes as packed binary data.
610

@@ -597,6 +601,11 @@ The :mod:`struct` module also defines the following type:
597601
The calculated size of the struct (and hence of the bytes object produced
598602
by the :meth:`pack` method) corresponding to :attr:`format`.
599603

604+
.. versionchanged:: 3.13 The *repr()* of structs has changed. It
605+
is now:
606+
607+
>>> Struct('i')
608+
Struct('i')
600609

601610
.. _half precision format: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
602611

Lib/test/test_struct.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,10 @@ def test_error_propagation(fmt_str):
774774
test_error_propagation('N')
775775
test_error_propagation('n')
776776

777+
def test_repr(self):
778+
s = struct.Struct('=i2H')
779+
self.assertEqual(repr(s), f'Struct({s.format!r})')
780+
777781
class UnpackIteratorTest(unittest.TestCase):
778782
"""
779783
Tests for iterative unpacking (struct.Struct.iter_unpack).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Implement new :meth:`__repr__` method for :class:`struct.Struct`.
2+
Now it returns ``Struct(<format repr>)``.

Modules/_struct.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,19 @@ s_sizeof(PyStructObject *self, void *unused)
21652165
return PyLong_FromSize_t(size);
21662166
}
21672167

2168+
static PyObject *
2169+
s_repr(PyStructObject *self)
2170+
{
2171+
PyObject* fmt = PyUnicode_FromStringAndSize(
2172+
PyBytes_AS_STRING(self->s_format), PyBytes_GET_SIZE(self->s_format));
2173+
if (fmt == NULL) {
2174+
return NULL;
2175+
}
2176+
PyObject* s = PyUnicode_FromFormat("%s(%R)", _PyType_Name(Py_TYPE(self)), fmt);
2177+
Py_DECREF(fmt);
2178+
return s;
2179+
}
2180+
21682181
/* List of functions */
21692182

21702183
static struct PyMethodDef s_methods[] = {
@@ -2197,6 +2210,7 @@ static PyType_Slot PyStructType_slots[] = {
21972210
{Py_tp_dealloc, s_dealloc},
21982211
{Py_tp_getattro, PyObject_GenericGetAttr},
21992212
{Py_tp_setattro, PyObject_GenericSetAttr},
2213+
{Py_tp_repr, s_repr},
22002214
{Py_tp_doc, (void*)s__doc__},
22012215
{Py_tp_traverse, s_traverse},
22022216
{Py_tp_clear, s_clear},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy