Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,8 @@ def test_operations_on_half_initialized_Struct(self):
self.assertRaises(RuntimeError, S.unpack, spam)
self.assertRaises(RuntimeError, S.unpack_from, spam)
self.assertRaises(RuntimeError, getattr, S, 'format')
self.assertRaises(RuntimeError, S.__sizeof__)
self.assertRaises(RuntimeError, repr, S)
self.assertEqual(S.size, -1)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix crash in :mod:`struct` when calling :func:`repr` or
``__sizeof__()`` on an uninitialized :class:`struct.Struct`
object created via ``Struct.__new__()`` without calling ``__init__()``.
2 changes: 2 additions & 0 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,7 @@ static PyObject *
Struct___sizeof___impl(PyStructObject *self)
/*[clinic end generated code: output=2d0d78900b4cdb4e input=faca5925c1f1ffd0]*/
{
ENSURE_STRUCT_IS_READY(self);
size_t size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
for (formatcode *code = self->s_codes; code->fmtdef != NULL; code++) {
size += sizeof(formatcode);
Expand All @@ -2393,6 +2394,7 @@ static PyObject *
s_repr(PyObject *op)
{
PyStructObject *self = PyStructObject_CAST(op);
ENSURE_STRUCT_IS_READY(self);
PyObject* fmt = PyUnicode_FromStringAndSize(
PyBytes_AS_STRING(self->s_format), PyBytes_GET_SIZE(self->s_format));
if (fmt == NULL) {
Expand Down
Loading