Skip to content

Commit b6c8bed

Browse files
committed
revert stuff from #143382
1 parent 54dbea8 commit b6c8bed

File tree

3 files changed

+2
-32
lines changed

3 files changed

+2
-32
lines changed

Lib/test/test_struct.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -822,20 +822,6 @@ def test_endian_table_init_subinterpreters(self):
822822
results = executor.map(exec, [code] * 5)
823823
self.assertListEqual(list(results), [None] * 5)
824824

825-
def test_Struct_object_mutation_via_dunders(self):
826-
S = struct.Struct('?I')
827-
buf = array.array('b', b' '*100)
828-
829-
class Evil():
830-
def __bool__(self):
831-
# This rebuilds format codes during S.pack().
832-
S.__init__('I')
833-
return True
834-
835-
with self.assertWarns(DeprecationWarning):
836-
self.assertRaises(RuntimeError, S.pack, Evil(), 1)
837-
self.assertRaises(RuntimeError, S.pack_into, buf, 0, Evil(), 1)
838-
839825
def test_operations_on_half_initialized_Struct(self):
840826
with self.assertWarns(DeprecationWarning):
841827
S = struct.Struct.__new__(struct.Struct)

Misc/NEWS.d/next/Library/2026-01-03-10-15-32.gh-issue-143379.iz-hU7.rst

Lines changed: 0 additions & 4 deletions
This file was deleted.

Modules/_struct.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ typedef struct {
7070
formatcode *s_codes;
7171
PyObject *s_format;
7272
PyObject *weakreflist; /* List of weak references */
73-
PyMutex mutex; /* to prevent mutation during packing */
7473
bool ready;
7574
} PyStructObject;
7675

@@ -1781,7 +1780,6 @@ s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
17811780
s->s_codes = NULL;
17821781
s->s_size = -1;
17831782
s->s_len = -1;
1784-
s->mutex = (PyMutex){0};
17851783
s->ready = false;
17861784
}
17871785
return self;
@@ -1831,11 +1829,6 @@ Struct___init___impl(PyStructObject *self, PyObject *format)
18311829

18321830
Py_SETREF(self->s_format, format);
18331831

1834-
if (PyMutex_IsLocked(&self->mutex)) {
1835-
PyErr_SetString(PyExc_RuntimeError,
1836-
"Call Struct.__init__() in struct.pack()");
1837-
return -1;
1838-
}
18391832
if (prepare_s(self)) {
18401833
return -1;
18411834
}
@@ -2174,7 +2167,7 @@ Struct_iter_unpack_impl(PyStructObject *self, PyObject *buffer)
21742167
* argument for where to start processing the arguments for packing, and a
21752168
* character buffer for writing the packed string. The caller must insure
21762169
* that the buffer may contain the required length for packing the arguments.
2177-
* 0 is returned on success, -1 is returned if there is an error.
2170+
* 0 is returned on success, 1 is returned if there is an error.
21782171
*
21792172
*/
21802173
static int
@@ -2298,13 +2291,10 @@ s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
22982291
char *buf = PyBytesWriter_GetData(writer);
22992292

23002293
/* Call the guts */
2301-
PyMutex_Lock(&soself->mutex);
23022294
if ( s_pack_internal(soself, args, 0, buf, state) != 0 ) {
2303-
PyMutex_Unlock(&soself->mutex);
23042295
PyBytesWriter_Discard(writer);
23052296
return NULL;
23062297
}
2307-
PyMutex_Unlock(&soself->mutex);
23082298

23092299
return PyBytesWriter_FinishWithSize(writer, soself->s_size);
23102300
}
@@ -2406,13 +2396,11 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
24062396
}
24072397

24082398
/* Call the guts */
2409-
PyMutex_Lock(&soself->mutex);
24102399
if (s_pack_internal(soself, args, 2, (char*)buffer.buf + offset, state) != 0) {
2411-
PyMutex_Unlock(&soself->mutex);
24122400
PyBuffer_Release(&buffer);
24132401
return NULL;
24142402
}
2415-
PyMutex_Unlock(&soself->mutex);
2403+
24162404
PyBuffer_Release(&buffer);
24172405
Py_RETURN_NONE;
24182406
}

0 commit comments

Comments
 (0)