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
gh-113626: Add allow_code parameter in marshal functions #113648
base: main
Are you sure you want to change the base?
Conversation
Passing allow_code=False prevents serialization and de-serialization of code objects which is incompatible between Python versions.
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
with self.assertRaises(ValueError): | ||
marshal.dumps(data, allow_code=False) | ||
self.assertEqual(marshal.loads(dump, allow_code=True), data) |
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 suggest to move the allow_code=True test before the allow_code=False test (exchange the 2 tests). Same below.
@@ -1364,6 +1375,11 @@ r_object(RFILE *p) | |||
PyObject* linetable = NULL; | |||
PyObject *exceptiontable = NULL; | |||
|
|||
if (!p->allow_code) { | |||
PyErr_SetString(PyExc_ValueError, | |||
"loading of code objects is prohibited"); |
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 suggest "disallowed" to be closer to "allow":
"loading of code objects is prohibited"); | |
"loading of code objects is disallowed"); |
@@ -549,6 +554,10 @@ w_complex_object(PyObject *v, char flag, WFILE *p) | |||
Py_DECREF(pairs); | |||
} | |||
else if (PyCode_Check(v)) { | |||
if (!p->allow_code) { | |||
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.
Maybe it would be worth it to have a more specific error just for code, to have a more specific error message in PyMarshal_WriteObjectToString().
|
||
* Add the *allow_code* parameter in module functions. | ||
Passing ``allow_code=False`` prevents serialization and de-serialization of | ||
code objects which is incompatible between Python versions. |
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.
code objects which is incompatible between Python versions. | |
code objects which are incompatible between Python versions. |
Passing allow_code=False prevents serialization and de-serialization of code objects which is incompatible between Python versions.
📚 Documentation preview 📚: https://cpython-previews--113648.org.readthedocs.build/