Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,28 @@ def test_call_invalid(self):
with self.assertRaises(interpreters.NotShareableError):
interp.call(func, op, 'eggs!')

def test_call_unpickle_bad_attribute_error(self):
# gh-156121: unpickling in the other interpreter may fail
# with an AttributeError whose message is not a str
# or cannot be encoded as UTF-8.
interp = interpreters.create()
script = dedent("""
x = 1
def f():
return x # forces the pickle fallback
""")
with defined_in___main__('f', script) as func:
for arg in (42, '\ud800'):
with self.subTest(arg):
interp.exec(dedent(f"""
import pickle
def loads(data):
raise AttributeError({arg!r})
pickle.loads = loads
"""))
with self.assertRaises(interpreters.NotShareableError):
interp.call(func)

def test_callable_requires_frame(self):
# There are various functions that require a current frame.
interp = interpreters.create()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in cross-interpreter unpickling
when the :exc:`AttributeError` raised by :func:`pickle.loads`
has a non-string argument or a message that cannot be encoded as UTF-8.
11 changes: 10 additions & 1 deletion Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ check_missing___main___attr(PyObject *exc)
PyObject *args = PyException_GetArgs(exc);
if (args == NULL || args == Py_None || PyObject_Size(args) < 1) {
Py_XDECREF(args);
assert(!PyErr_Occurred());
PyErr_Clear();
return 0;
}
PyObject *msgobj = args;
Expand All @@ -662,8 +662,17 @@ check_missing___main___attr(PyObject *exc)
PyErr_Clear();
return 0;
}
if (!PyUnicode_Check(msgobj)) {
Py_DECREF(msgobj);
return 0;
}
}
const char *err = PyUnicode_AsUTF8(msgobj);
if (err == NULL) {
Py_DECREF(msgobj);
PyErr_Clear();
return 0;
}

// Check if it's a missing __main__ attr.
int cmp = strncmp(err, "module '__main__' has no attribute '", 36);
Expand Down
Loading