Skip to content

Commit 9ccdc90

Browse files
miss-islingtonjunyixie
andauthored
bpo-45196: prevent unittest crash on address sanitizer builds (GH-28331)
(cherry picked from commit b668cdf) Co-authored-by: junyixie <[email protected]>
1 parent 13257d9 commit 9ccdc90

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Lib/test/test_decimal.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
import random
4141
import inspect
4242
import threading
43+
import sysconfig
44+
_cflags = sysconfig.get_config_var('CFLAGS') or ''
45+
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
46+
MEMORY_SANITIZER = (
47+
'-fsanitize=memory' in _cflags or
48+
'--with-memory-sanitizer' in _config_args
49+
)
50+
51+
ADDRESS_SANITIZER = (
52+
'-fsanitize=address' in _cflags
53+
)
4354

4455

4556
if sys.platform == 'darwin':
@@ -5486,6 +5497,8 @@ def __abs__(self):
54865497
# Issue 41540:
54875498
@unittest.skipIf(sys.platform.startswith("aix"),
54885499
"AIX: default ulimit: test is flaky because of extreme over-allocation")
5500+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
5501+
"instead of returning NULL for malloc failure.")
54895502
def test_maxcontext_exact_arith(self):
54905503

54915504
# Make sure that exact operations do not raise MemoryError due

Lib/test/test_io.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class EmptyStruct(ctypes.Structure):
6969
'--with-memory-sanitizer' in _config_args
7070
)
7171

72+
ADDRESS_SANITIZER = (
73+
'-fsanitize=address' in _cflags
74+
)
75+
7276
# Does io.IOBase finalizer log the exception if the close() method fails?
7377
# The exception is ignored silently by default in release build.
7478
IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
@@ -1539,7 +1543,7 @@ def test_truncate_on_read_only(self):
15391543
class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
15401544
tp = io.BufferedReader
15411545

1542-
@unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
1546+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
15431547
"instead of returning NULL for malloc failure.")
15441548
def test_constructor(self):
15451549
BufferedReaderTest.test_constructor(self)
@@ -1888,7 +1892,7 @@ def test_slow_close_from_thread(self):
18881892
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
18891893
tp = io.BufferedWriter
18901894

1891-
@unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
1895+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
18921896
"instead of returning NULL for malloc failure.")
18931897
def test_constructor(self):
18941898
BufferedWriterTest.test_constructor(self)
@@ -2387,7 +2391,7 @@ def test_interleaved_readline_write(self):
23872391
class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
23882392
tp = io.BufferedRandom
23892393

2390-
@unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
2394+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
23912395
"instead of returning NULL for malloc failure.")
23922396
def test_constructor(self):
23932397
BufferedRandomTest.test_constructor(self)

0 commit comments

Comments
 (0)