8000 bpo-41654: Fix deallocator of MemoryError to account for subclasses (… · python/cpython@9b648a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b648a9

Browse files
authored
bpo-41654: Fix deallocator of MemoryError to account for subclasses (GH-22020)
When allocating MemoryError classes, there is some logic to use pre-allocated instances in a freelist only if the type that is being allocated is not a subclass of MemoryError. Unfortunately in the destructor this logic is not present so the freelist is altered even with subclasses of MemoryError.
1 parent 6844b56 commit 9b648a9

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Lib/test/test_exceptions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Python test set -- part 5, built-in exceptions
22

33
import copy
4+
import gc
45
import os
56
import sys
67
import unittest
@@ -1330,6 +1331,36 @@ def test_assert_shadowing(self):
13301331
del AssertionError
13311332
self.fail('Expected exception')
13321333

1334+
def test_memory_error_subclasses(self):
1335+
# bpo-41654: MemoryError instances use a freelist of objects that are
1336+
# linked using the 'dict' attribute when they are inactive/dead.
1337+
# Subclasses of MemoryError should not participate in the freelist
1338+
# schema. This test creates a MemoryError object and keeps it alive
1339+
# (therefore advancing the freelist) and then it creates and destroys a
1340+
# subclass object. Finally, it checks that creating a new MemoryError
1341+
# succeeds, proving that the freelist is not corrupted.
1342+
1343+
class TestException(MemoryError):
1344+
pass
1345+
1346+
try:
1347+
raise MemoryError
1348+
except MemoryError as exc:
1349+
inst = exc
1350+
1351+
try:
1352+
raise TestException
1353+
except Exception:
1354+
pass
1355+
1356+
for _ in range(10):
1357+
try:
1358+
raise MemoryError
1359+
except MemoryError as exc:
1360+
pass
1361+
1362+
gc_collect()
1363+
13331364

13341365
class ImportErrorTests(unittest.TestCase):
13351366

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash that occurred when destroying subclasses of
2+
:class:`MemoryError`. Patch by Pablo Galindo.

Objects/exceptions.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,8 +2286,11 @@ MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22862286
{
22872287
PyBaseExceptionObject *self;
22882288

2289-
if (type != (PyTypeObject *) PyExc_MemoryError)
2289+
/* If this is a subclass of MemoryError, don't use the freelist
2290+
* and just return a fresh object */
2291+
if (type != (PyTypeObject *) PyExc_MemoryError) {
22902292
return BaseException_new(type, args, kwds);
2293+
}
22912294

22922295
struct _Py_exc_state *state = get_exc_state();
22932296
if (state->memerrors_freelist == NULL) {
@@ -2313,9 +2316,16 @@ MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23132316
static void
23142317
MemoryError_dealloc(PyBaseExceptionObject *self)
23152318
{
2316-
_PyObject_GC_UNTRACK(self);
23172319
BaseException_clear(self);
23182320

2321+
/* If this is a subclass of MemoryError, we don't need to
2322+
* do anything in the free-list*/
2323+
if (!Py_IS_TYPE(self, (PyTypeObject *) PyExc_MemoryError)) {
2324+
return Py_TYPE(self)->tp_free((PyObject *)self);
2325+
}
2326+
2327+
_PyObject_GC_UNTRACK(self);
2328+
23192329
struct _Py_exc_state *state = get_exc_state();
23202330
if (state->memerrors_numfree >= MEMERRORS_SAVE) {
23212331
Py_TYPE(self)->tp_free((PyObject *)self);

0 commit comments

Comments
 (0)
0