summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2020-09-01 18:39:46 (GMT)
committerGitHub <noreply@github.com>2020-09-01 18:39:46 (GMT)
commit9b648a95ccb4c3b14f1e87158f5c9f5dbb2f62c0 (patch)
treebf8539814364c648b58965d3558014a23d6bcf4f /Lib
parent6844b56176c41f0a0e25fcd4fef5463bcdbc7d7c (diff)
downloadcpython-9b648a95ccb4c3b14f1e87158f5c9f5dbb2f62c0.zip
cpython-9b648a95ccb4c3b14f1e87158f5c9f5dbb2f62c0.tar.gz
cpython-9b648a95ccb4c3b14f1e87158f5c9f5dbb2f62c0.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_exceptions.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 2ffe8ca..1ec4468 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1,6 +1,7 @@
# Python test set -- part 5, built-in exceptions
import copy
+import gc
import os
import sys
import unittest
@@ -1330,6 +1331,36 @@ class ExceptionTests(unittest.TestCase):
del AssertionError
self.fail('Expected exception')
+ def test_memory_error_subclasses(self):
+ # bpo-41654: MemoryError instances use a freelist of objects that are
+ # linked using the 'dict' attribute when they are inactive/dead.
+ # Subclasses of MemoryError should not participate in the freelist
+ # schema. This test creates a MemoryError object and keeps it alive
+ # (therefore advancing the freelist) and then it creates and destroys a
+ # subclass object. Finally, it checks that creating a new MemoryError
+ # succeeds, proving that the freelist is not corrupted.
+
+ class TestException(MemoryError):
+ pass
+
+ try:
+ raise MemoryError
+ except MemoryError as exc:
+ inst = exc
+
+ try:
+ raise TestException
+ except Exception:
+ pass
+
+ for _ in range(10):
+ try:
+ raise MemoryError
+ except MemoryError as exc:
+ pass
+
+ gc_collect()
+
class ImportErrorTests(unittest.TestCase):