Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address review comments.
  • Loading branch information
serhiy-storchaka committed Jan 12, 2024
commit 7c6c2b665e6a29c73891fa2e3d5ef3558794796c
4 changes: 2 additions & 2 deletions Lib/test/test_marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ def test_no_allow_code(self):
co = ExceptionTestCase.test_exceptions.__code__
data = {'a': [({co, 0},)]}
dump = marshal.dumps(data, allow_code=True)
self.assertEqual(marshal.loads(dump, allow_code=True), data)
with self.assertRaises(ValueError):
marshal.dumps(data, allow_code=False)
self.assertEqual(marshal.loads(dump, allow_code=True), data)
with self.assertRaises(ValueError):
marshal.loads(dump, allow_code=False)

marshal.dump(data, io.BytesIO(), allow_code=True)
self.assertEqual(marshal.load(io.BytesIO(dump), allow_code=True), data)
with self.assertRaises(ValueError):
marshal.dump(data, io.BytesIO(), allow_code=False)
self.assertEqual(marshal.load(io.BytesIO(dump), allow_code=True), data)
with self.assertRaises(ValueError):
marshal.load(io.BytesIO(dump), allow_code=False)

Expand Down
25 changes: 19 additions & 6 deletions Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module marshal
#define WFERR_UNMARSHALLABLE 1
#define WFERR_NESTEDTOODEEP 2
#define WFERR_NOMEMORY 3
#define WFERR_CODE_NOT_ALLOWED 4

typedef struct {
FILE *fp;
Expand Down Expand Up @@ -555,7 +556,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
}
else if (PyCode_Check(v)) {
if (!p->allow_code) {
p->error = WFERR_UNMARSHALLABLE;
p->error = WFERR_CODE_NOT_ALLOWED;
return;
}
PyCodeObject *co = (PyCodeObject *)v;
Expand Down Expand Up @@ -1377,7 +1378,7 @@ r_object(RFILE *p)

if (!p->allow_code) {
PyErr_SetString(PyExc_ValueError,
"loading of code objects is disallowed");
"unmarshalling code objects is disallowed");
break;
}
idx = r_ref_reserve(flag, p);
Expand Down Expand Up @@ -1693,12 +1694,24 @@ _PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
}
if (wf.error != WFERR_OK) {
Py_XDECREF(wf.str);
if (wf.error == WFERR_NOMEMORY)
switch (wf.error) {
case WFERR_NOMEMORY:
PyErr_NoMemory();
else
break;
case WFERR_NESTEDTOODEEP:
PyErr_SetString(PyExc_ValueError,
"object too deeply nested to marshal");
break;
case WFERR_CODE_NOT_ALLOWED:
PyErr_SetString(PyExc_ValueError,
(wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"
:"object too deeply nested to marshal");
"marshalling code objects is disallowed");
break;
default:
case WFERR_UNMARSHALLABLE:
PyErr_SetString(PyExc_ValueError,
"unmarshallable object");
break;
}
return NULL;
}
return wf.str;
Expand Down