]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107406: Add better `struct.Struct` repr (#107407)
authordenballakh <47365157+denballakh@users.noreply.github.com>
Sat, 26 Aug 2023 09:54:16 +0000 (14:54 +0500)
committerGitHub <noreply@github.com>
Sat, 26 Aug 2023 09:54:16 +0000 (15:24 +0530)
Doc/library/struct.rst
Lib/test/test_struct.py
Misc/NEWS.d/next/Library/2023-07-29-02-01-24.gh-issue-107406.ze6sQP.rst [new file with mode: 0644]
Modules/_struct.c

index c94dfde4d55763f2774d3bde333c66a8af59f5d9..e2e6fc542e3e6710bd34625b1dcc382457d514d4 100644 (file)
@@ -1,6 +1,10 @@
 :mod:`struct` --- Interpret bytes as packed binary data
 =======================================================
 
+.. testsetup:: *
+
+   from struct import *
+
 .. module:: struct
    :synopsis: Interpret bytes as packed binary data.
 
@@ -597,6 +601,11 @@ The :mod:`struct` module also defines the following type:
       The calculated size of the struct (and hence of the bytes object produced
       by the :meth:`pack` method) corresponding to :attr:`format`.
 
+   .. versionchanged:: 3.13 The *repr()* of structs has changed.  It
+      is now:
+
+         >>> Struct('i')
+         Struct('i')
 
 .. _half precision format: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
 
index 6b1f22f66fd15758a574e327b36be7b4c4387446..c76649cdcd9cce2bf19559140242ed09757d0fc3 100644 (file)
@@ -774,6 +774,10 @@ class StructTest(unittest.TestCase):
         test_error_propagation('N')
         test_error_propagation('n')
 
+    def test_repr(self):
+        s = struct.Struct('=i2H')
+        self.assertEqual(repr(s), f'Struct({s.format!r})')
+
 class UnpackIteratorTest(unittest.TestCase):
     """
     Tests for iterative unpacking (struct.Struct.iter_unpack).
diff --git a/Misc/NEWS.d/next/Library/2023-07-29-02-01-24.gh-issue-107406.ze6sQP.rst b/Misc/NEWS.d/next/Library/2023-07-29-02-01-24.gh-issue-107406.ze6sQP.rst
new file mode 100644 (file)
index 0000000..b78e57f
--- /dev/null
@@ -0,0 +1,2 @@
+Implement new :meth:`__repr__` method for :class:`struct.Struct`.
+Now it returns ``Struct(<format repr>)``.
index 606ae5e4e199642bd728ed88d33bdbaeb90c8adf..4da654231cdf0022afa42c6bcc2246956919de0c 100644 (file)
@@ -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},