Skip to content

Commit b668cdf

Browse files
author
junyixie
authored
bpo-45196: prevent unittest crash on address sanitizer builds (GH-28331)
1 parent 024fda4 commit b668cdf

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
@@ -43,6 +43,17 @@
4343
import random
4444
import inspect
4545
import threading
46+
import sysconfig
47+
_cflags = sysconfig.get_config_var('CFLAGS') or ''
48+
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
49+
MEMORY_SANITIZER = (
50+
'-fsanitize=memory' in _cflags or
51+
'--with-memory-sanitizer' in _config_args
52+
)
53+
54+
ADDRESS_SANITIZER = (
55+
'-fsanitize=address' in _cflags
56+
)
4657

4758

4859
if sys.platform == 'darwin':
@@ -5500,6 +5511,8 @@ def __abs__(self):
55005511
# Issue 41540:
55015512
@unittest.skipIf(sys.platform.startswith("aix"),
55025513
"AIX: default ulimit: test is flaky because of extreme over-allocation")
5514+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
5515+
"instead of returning NULL for malloc failure.")
55035516
def test_maxcontext_exact_arith(self):
55045517

55055518
# 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
@@ -73,6 +73,10 @@ class EmptyStruct(ctypes.Structure):
7373
'--with-memory-sanitizer' in _config_args
7474
)
7575

76+
ADDRESS_SANITIZER = (
77+
'-fsanitize=address' in _cflags
78+
)
79+
7680
# Does io.IOBase finalizer log the exception if the close() method fails?
7781
# The exception is ignored silently by default in release build.
7882
IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
@@ -1546,7 +1550,7 @@ def test_truncate_on_read_only(self):
15461550
class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
15471551
tp = io.BufferedReader
15481552

1549-
@unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
1553+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
15501554
"instead of returning NULL for malloc failure.")
15511555
def test_constructor(self):
15521556
BufferedReaderTest.test_constructor(self)
@@ -1911,7 +1915,7 @@ def test_slow_close_from_thread(self):
19111915
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
19121916
tp = io.BufferedWriter
19131917

1914-
@unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
1918+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
19151919
"instead of returning NULL for malloc failure.")
19161920
def test_constructor(self):
19171921
BufferedWriterTest.test_constructor(self)
@@ -2410,7 +2414,7 @@ def test_interleaved_readline_write(self):
24102414
class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
24112415
tp = io.BufferedRandom
24122416

2413-
@unittest.skipIf(MEMORY_SANITIZER, "MSan defaults to crashing "
2417+
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
24142418
"instead of returning NULL for malloc failure.")
24152419
def test_constructor(self):
24162420
BufferedRandomTest.test_constructor(self)

0 commit comments

Comments
 (0)