-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
bpo-34722: Consistent serialization of sets in bytecode #9472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Ensures that sets / frozensets marshal in a consistent order by sorting the | ||
serialised items before writing them. This will result in somewhat slower | ||
serialisation but bytecode files using set literals in certain contexts will | ||
now serialise deterministically where it didn't before. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -496,7 +496,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p) | |
w_object((PyObject *)NULL, p); | ||
} | ||
else if (PyAnySet_CheckExact(v)) { | ||
PyObject *value, *it; | ||
PyObject *value, *l; | ||
|
||
if (PyObject_TypeCheck(v, &PySet_Type)) | ||
W_TYPE(TYPE_SET, p); | ||
|
@@ -509,17 +509,27 @@ w_complex_object(PyObject *v, char flag, WFILE *p) | |
return; | ||
} | ||
W_SIZE(n, p); | ||
it = PyObject_GetIter(v); | ||
if (it == NULL) { | ||
l = PySequence_List(v); | ||
if (l == NULL) { | ||
p->depth--; | ||
p->error = WFERR_UNMARSHALLABLE; | ||
return; | ||
} | ||
while ((value = PyIter_Next(it)) != NULL) { | ||
w_object(value, p); | ||
Py_DECREF(value); | ||
for (i = 0; i < n; i++) { | ||
value = PyList_GetItem(l, i); | ||
value = PyMarshal_WriteObjectToString(value, Py_MARSHAL_VERSION); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will lost the identity of objects. For example, in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I can get the same objects back at present?
Maybe I'm misunderstanding what you mean, or that's not a good test case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additional tests were added in #13736. Rebase your PR and make the tests be success. |
||
PyList_SetItem(l, i, value); | ||
} | ||
if (PyList_Sort(l) == -1) { | ||
Py_DECREF(l); | ||
p->depth--; | ||
p->error = WFERR_UNMARSHALLABLE; | ||
return; | ||
} | ||
for (i = 0; i < n; i++) { | ||
w_object(PyList_GetItem(l, i), p); | ||
} | ||
Py_DECREF(it); | ||
Py_DECREF(l); | ||
if (PyErr_Occurred()) { | ||
p->depth--; | ||
p->error = WFERR_UNMARSHALLABLE; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
PyList_GetItem()
andPyList_SetItem()
calls added in this patch should be replaced with thePyList_GET_ITEM()
andPyList_SET_ITEM()
macros.Also,
PyMarshal_WriteObjectToString()
should be checked for failure.