Skip to content

Commit 51999c9

Browse files
authored
bpo-37596: Clean up the set/frozenset marshalling code (GH-28068)
1 parent 4300352 commit 51999c9

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Python/marshal.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,36 +507,39 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
507507
// to have their elements serialized in a consistent order (even when
508508
// they have been scrambled by hash randomization). To ensure this, we
509509
// use an order equivalent to sorted(v, key=marshal.dumps):
510-
PyObject *pairs = PyList_New(0);
510+
PyObject *pairs = PyList_New(n);
511511
if (pairs == NULL) {
512512
p->error = WFERR_NOMEMORY;
513513
return;
514514
}
515+
Py_ssize_t i = 0;
515516
while (_PySet_NextEntry(v, &pos, &value, &hash)) {
516517
PyObject *dump = PyMarshal_WriteObjectToString(value, p->version);
517518
if (dump == NULL) {
518519
p->error = WFERR_UNMARSHALLABLE;
519-
goto anyset_done;
520+
Py_DECREF(pairs);
521+
return;
520522
}
521523
PyObject *pair = PyTuple_Pack(2, dump, value);
522524
Py_DECREF(dump);
523-
if (pair == NULL || PyList_Append(pairs, pair)) {
525+
if (pair == NULL) {
524526
p->error = WFERR_NOMEMORY;
525-
Py_XDECREF(pair);
526-
goto anyset_done;
527+
Py_DECREF(pairs);
528+
return;
527529
}
528-
Py_DECREF(pair);
530+
PyList_SET_ITEM(pairs, i++, pair);
529531
}
532+
assert(i == n);
530533
if (PyList_Sort(pairs)) {
531534
p->error = WFERR_NOMEMORY;
532-
goto anyset_done;
535+
Py_DECREF(pairs);
536+
return;
533537
}
534538
for (Py_ssize_t i = 0; i < n; i++) {
535539
PyObject *pair = PyList_GET_ITEM(pairs, i);
536540
value = PyTuple_GET_ITEM(pair, 1);
537541
w_object(value, p);
538542
}
539-
anyset_done:
540543
Py_DECREF(pairs);
541544
}
542545
else if (PyCode_Check(v)) {

0 commit comments

Comments
 (0)