mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
bpo-37596: Clean up the set/frozenset marshalling code (GH-28068)
This commit is contained in:
parent
4300352000
commit
51999c960e
1 changed files with 11 additions and 8 deletions
|
@ -507,36 +507,39 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
|
||||||
// to have their elements serialized in a consistent order (even when
|
// to have their elements serialized in a consistent order (even when
|
||||||
// they have been scrambled by hash randomization). To ensure this, we
|
// they have been scrambled by hash randomization). To ensure this, we
|
||||||
// use an order equivalent to sorted(v, key=marshal.dumps):
|
// use an order equivalent to sorted(v, key=marshal.dumps):
|
||||||
PyObject *pairs = PyList_New(0);
|
PyObject *pairs = PyList_New(n);
|
||||||
if (pairs == NULL) {
|
if (pairs == NULL) {
|
||||||
p->error = WFERR_NOMEMORY;
|
p->error = WFERR_NOMEMORY;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Py_ssize_t i = 0;
|
||||||
while (_PySet_NextEntry(v, &pos, &value, &hash)) {
|
while (_PySet_NextEntry(v, &pos, &value, &hash)) {
|
||||||
PyObject *dump = PyMarshal_WriteObjectToString(value, p->version);
|
PyObject *dump = PyMarshal_WriteObjectToString(value, p->version);
|
||||||
if (dump == NULL) {
|
if (dump == NULL) {
|
||||||
p->error = WFERR_UNMARSHALLABLE;
|
p->error = WFERR_UNMARSHALLABLE;
|
||||||
goto anyset_done;
|
Py_DECREF(pairs);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
PyObject *pair = PyTuple_Pack(2, dump, value);
|
PyObject *pair = PyTuple_Pack(2, dump, value);
|
||||||
Py_DECREF(dump);
|
Py_DECREF(dump);
|
||||||
if (pair == NULL || PyList_Append(pairs, pair)) {
|
if (pair == NULL) {
|
||||||
p->error = WFERR_NOMEMORY;
|
p->error = WFERR_NOMEMORY;
|
||||||
Py_XDECREF(pair);
|
Py_DECREF(pairs);
|
||||||
goto anyset_done;
|
return;
|
||||||
}
|
}
|
||||||
Py_DECREF(pair);
|
PyList_SET_ITEM(pairs, i++, pair);
|
||||||
}
|
}
|
||||||
|
assert(i == n);
|
||||||
if (PyList_Sort(pairs)) {
|
if (PyList_Sort(pairs)) {
|
||||||
p->error = WFERR_NOMEMORY;
|
p->error = WFERR_NOMEMORY;
|
||||||
goto anyset_done;
|
Py_DECREF(pairs);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
for (Py_ssize_t i = 0; i < n; i++) {
|
for (Py_ssize_t i = 0; i < n; i++) {
|
||||||
PyObject *pair = PyList_GET_ITEM(pairs, i);
|
PyObject *pair = PyList_GET_ITEM(pairs, i);
|
||||||
value = PyTuple_GET_ITEM(pair, 1);
|
value = PyTuple_GET_ITEM(pair, 1);
|
||||||
w_object(value, p);
|
w_object(value, p);
|
||||||
}
|
}
|
||||||
anyset_done:
|
|
||||||
Py_DECREF(pairs);
|
Py_DECREF(pairs);
|
||||||
}
|
}
|
||||||
else if (PyCode_Check(v)) {
|
else if (PyCode_Check(v)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue