diff options
author | Brandt Bucher <brandt@python.org> | 2021-08-31 16:18:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-31 16:18:33 (GMT) |
commit | 51999c960e7fc45feebd629421dec6524a5fc803 (patch) | |
tree | 5de4ce8197791cfbcbb0334fb28991a767310a2c /Python/marshal.c | |
parent | 4300352000beed22fb525ec45fd331918d206528 (diff) | |
download | cpython-51999c960e7fc45feebd629421dec6524a5fc803.zip cpython-51999c960e7fc45feebd629421dec6524a5fc803.tar.gz cpython-51999c960e7fc45feebd629421dec6524a5fc803.tar.bz2 |
bpo-37596: Clean up the set/frozenset marshalling code (GH-28068)
Diffstat (limited to 'Python/marshal.c')
-rw-r--r-- | Python/marshal.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/Python/marshal.c b/Python/marshal.c index b69c4d0..60b818f 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -507,36 +507,39 @@ w_complex_object(PyObject *v, char flag, WFILE *p) // to have their elements serialized in a consistent order (even when // they have been scrambled by hash randomization). To ensure this, we // use an order equivalent to sorted(v, key=marshal.dumps): - PyObject *pairs = PyList_New(0); + PyObject *pairs = PyList_New(n); if (pairs == NULL) { p->error = WFERR_NOMEMORY; return; } + Py_ssize_t i = 0; while (_PySet_NextEntry(v, &pos, &value, &hash)) { PyObject *dump = PyMarshal_WriteObjectToString(value, p->version); if (dump == NULL) { p->error = WFERR_UNMARSHALLABLE; - goto anyset_done; + Py_DECREF(pairs); + return; } PyObject *pair = PyTuple_Pack(2, dump, value); Py_DECREF(dump); - if (pair == NULL || PyList_Append(pairs, pair)) { + if (pair == NULL) { p->error = WFERR_NOMEMORY; - Py_XDECREF(pair); - goto anyset_done; + Py_DECREF(pairs); + return; } - Py_DECREF(pair); + PyList_SET_ITEM(pairs, i++, pair); } + assert(i == n); if (PyList_Sort(pairs)) { p->error = WFERR_NOMEMORY; - goto anyset_done; + Py_DECREF(pairs); + return; } for (Py_ssize_t i = 0; i < n; i++) { PyObject *pair = PyList_GET_ITEM(pairs, i); value = PyTuple_GET_ITEM(pair, 1); w_object(value, p); } - anyset_done: Py_DECREF(pairs); } else if (PyCode_Check(v)) { |