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

[3.12] gh-109894: Fix initialization of static MemoryError in subinterpreter (gh-110911) #111238

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ extern PyTypeObject _PyExc_MemoryError;
}, \
.last_resort_memory_error = { \
_PyObject_HEAD_INIT(&_PyExc_MemoryError) \
.args = (PyObject*)&_Py_SINGLETON(tuple_empty) \
}, \
}, \
}, \
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,20 @@ class TestException(MemoryError):

gc_collect()

def test_memory_error_in_subinterp(self):
# gh-109894: subinterpreters shouldn't count on last resort memory error
# when MemoryError is raised through PyErr_NoMemory() call,
# and should preallocate memory errors as does the main interpreter.
# interp.static_objects.last_resort_memory_error.args
# should be initialized to empty tuple to avoid crash on attempt to print it.
code = f"""if 1:
import _testcapi
_testcapi.run_in_subinterp(\"[0]*{sys.maxsize}\")
exit(0)
"""
rc, _, err = script_helper.assert_python_ok("-c", code)
self.assertIn(b'MemoryError', err)


class NameErrorTests(unittest.TestCase):
def test_name_error_has_name(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed crash due to improperly initialized static :exc:`MemoryError` in subinterpreter.
4 changes: 0 additions & 4 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3701,10 +3701,6 @@ _PyExc_FiniTypes(PyInterpreterState *interp)
PyStatus
_PyExc_InitGlobalObjects(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return _PyStatus_OK();
}

if (preallocate_memerrors() < 0) {
return _PyStatus_NO_MEMORY();
}
Expand Down
Loading