Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jan 2, 2024

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/

Passing allow_code=False prevents serialization and de-serialization of
code objects which is incompatible between Python versions.
Doc/library/marshal.rst Outdated Show resolved Hide resolved
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Comment on lines +145 to +147
with self.assertRaises(ValueError):
marshal.dumps(data, allow_code=False)
self.assertEqual(marshal.loads(dump, allow_code=True), data)
Copy link
Member

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");
Copy link
Member

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":

Suggested change
"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;
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
code objects which is incompatible between Python versions.
code objects which are incompatible between Python versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants