Refactor sanitiser skip tests into test.support (GH-30889)

* Refactor sanitizer skip tests into test.support

* fixup! Refactor sanitizer skip tests into test.support

* fixup! fixup! Refactor sanitizer skip tests into test.support
This commit is contained in:
Pablo Galindo Salgado 2022-01-25 22:01:10 +00:00 committed by GitHub
parent cef0a5458f
commit b1cb843050
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 34 deletions

View File

@ -43,7 +43,7 @@ __all__ = [
"has_subprocess_support", "requires_subprocess",
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
"check__all__", "skip_if_buggy_ucrt_strfptime",
"check_disallow_instantiation",
"check_disallow_instantiation", "skip_if_sanitizer",
# sys
"is_jython", "is_android", "is_emscripten", "is_wasi",
"check_impl_detail", "unix_shell", "setswitchinterval",
@ -384,6 +384,35 @@ def skip_if_buildbot(reason=None):
isbuildbot = os.environ.get('USER') == 'buildbot'
return unittest.skipIf(isbuildbot, reason)
def skip_if_sanitizer(reason=None, *, address=False, memory=False, ub=False):
"""Decorator raising SkipTest if running with a sanitizer active."""
if not (address or memory or ub):
raise ValueError('At least one of address, memory, or ub must be True')
if not reason:
reason = 'not working with sanitizers active'
_cflags = sysconfig.get_config_var('CFLAGS') or ''
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
memory_sanitizer = (
'-fsanitize=memory' in _cflags or
'--with-memory-sanitizer' in _config_args
)
address_sanitizer = (
'-fsanitize=address' in _cflags or
'--with-memory-sanitizer' in _config_args
)
ub_sanitizer = (
'-fsanitize=undefined' in _cflags or
'--with-undefined-behavior-sanitizer' in _config_args
)
skip = (
(memory and memory_sanitizer) or
(address and address_sanitizer) or
(ub and ub_sanitizer)
)
return unittest.skipIf(skip, reason)
def system_must_validate_cert(f):
"""Skip the test on TLS certificate validation failures."""

View File

@ -6,10 +6,10 @@ import re
import signal
import subprocess
import sys
import sysconfig
from test import support
from test.support import os_helper
from test.support import script_helper, is_android
from test.support import skip_if_sanitizer
import tempfile
import unittest
from textwrap import dedent
@ -21,16 +21,6 @@ except ImportError:
TIMEOUT = 0.5
MS_WINDOWS = (os.name == 'nt')
_cflags = sysconfig.get_config_var('CFLAGS') or ''
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
UB_SANITIZER = (
'-fsanitize=undefined' in _cflags or
'--with-undefined-behavior-sanitizer' in _config_args
)
MEMORY_SANITIZER = (
'-fsanitize=memory' in _cflags or
'--with-memory-sanitizer' in _config_args
)
def expected_traceback(lineno1, lineno2, header, min_count=1):
@ -311,8 +301,8 @@ class FaultHandlerTests(unittest.TestCase):
3,
'Segmentation fault')
@unittest.skipIf(UB_SANITIZER or MEMORY_SANITIZER,
"sanitizer builds change crashing process output.")
@skip_if_sanitizer(memory=True, ub=True, reason="sanitizer "
"builds change crashing process output.")
@skip_segfault_on_android
def test_enable_file(self):
with temporary_filename() as filename:
@ -328,8 +318,8 @@ class FaultHandlerTests(unittest.TestCase):
@unittest.skipIf(sys.platform == "win32",
"subprocess doesn't support pass_fds on Windows")
@unittest.skipIf(UB_SANITIZER or MEMORY_SANITIZER,
"sanitizer builds change crashing process output.")
@skip_if_sanitizer(memory=True, ub=True, reason="sanitizer "
"builds change crashing process output.")
@skip_segfault_on_android
def test_enable_fd(self):
with tempfile.TemporaryFile('wb+') as fp:

View File

@ -28,7 +28,6 @@ import pickle
import random
import signal
import sys
import sysconfig
import textwrap
import threading
import time
@ -44,6 +43,7 @@ from test.support import import_helper
from test.support import os_helper
from test.support import threading_helper
from test.support import warnings_helper
from test.support import skip_if_sanitizer
from test.support.os_helper import FakePath
import codecs
@ -66,17 +66,6 @@ else:
class EmptyStruct(ctypes.Structure):
pass
_cflags = sysconfig.get_config_var('CFLAGS') or ''
_config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
MEMORY_SANITIZER = (
'-fsanitize=memory' in _cflags or
'--with-memory-sanitizer' in _config_args
)
ADDRESS_SANITIZER = (
'-fsanitize=address' in _cflags
)
# Does io.IOBase finalizer log the exception if the close() method fails?
# The exception is ignored silently by default in release build.
IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
@ -1550,7 +1539,7 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
tp = io.BufferedReader
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
@skip_if_sanitizer(memory=True, address=True, reason= "sanitizer defaults to crashing "
"instead of returning NULL for malloc failure.")
def test_constructor(self):
BufferedReaderTest.test_constructor(self)
@ -1915,7 +1904,7 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
tp = io.BufferedWriter
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
@skip_if_sanitizer(memory=True, address=True, reason= "sanitizer defaults to crashing "
"instead of returning NULL for malloc failure.")
def test_constructor(self):
BufferedWriterTest.test_constructor(self)
@ -2414,7 +2403,7 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
tp = io.BufferedRandom
@unittest.skipIf(MEMORY_SANITIZER or ADDRESS_SANITIZER, "sanitizer defaults to crashing "
@skip_if_sanitizer(memory=True, address=True, reason= "sanitizer defaults to crashing "
"instead of returning NULL for malloc failure.")
def test_constructor(self):
BufferedRandomTest.test_constructor(self)