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

Fixes GH-109894

* set `interp.static_objects.last_resort_memory_error.args` to empty tuple to avoid crash on `PyErr_Display()` call
* allow `_PyExc_InitGlobalObjects()` to be called on subinterpreter init

---------

(cherry picked from commit 47d3e2ed93)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2023-11-28 03:41:53 +01:00 committed by GitHub
parent 1e1a30f9f4
commit 0aceac557d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 4 deletions

View file

@ -1785,6 +1785,20 @@ class ExceptionTests(unittest.TestCase):
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):