summaryrefslogtreecommitdiff
path: root/Lib/test/support/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/support/__init__.py')
-rw-r--r--Lib/test/support/__init__.py68
1 files changed, 67 insertions, 1 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index e124fab610..04e8629134 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -26,6 +26,7 @@ import sys
import sysconfig
import tempfile
import time
+import types
import unittest
import urllib.error
import warnings
@@ -89,6 +90,7 @@ __all__ = [
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
"requires_IEEE_754", "skip_unless_xattr", "requires_zlib",
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
+ "check__all__",
# sys
"is_jython", "check_impl_detail",
# network
@@ -900,7 +902,7 @@ def temp_dir(path=None, quiet=False):
yield path
finally:
if dir_created:
- shutil.rmtree(path)
+ rmtree(path)
@contextlib.contextmanager
def change_cwd(path, quiet=False):
@@ -2073,6 +2075,11 @@ def args_from_interpreter_flags():
settings in sys.flags and sys.warnoptions."""
return subprocess._args_from_interpreter_flags()
+def optim_args_from_interpreter_flags():
+ """Return a list of command-line arguments reproducing the current
+ optimization settings in sys.flags."""
+ return subprocess._optim_args_from_interpreter_flags()
+
#============================================================
# Support for assertions about logging.
#============================================================
@@ -2224,6 +2231,65 @@ def detect_api_mismatch(ref_api, other_api, *, ignore=()):
return missing_items
+def check__all__(test_case, module, name_of_module=None, extra=(),
+ blacklist=()):
+ """Assert that the __all__ variable of 'module' contains all public names.
+
+ The module's public names (its API) are detected automatically based on
+ whether they match the public name convention and were defined in
+ 'module'.
+
+ The 'name_of_module' argument can specify (as a string or tuple thereof)
+ what module(s) an API could be defined in in order to be detected as a
+ public API. One case for this is when 'module' imports part of its public
+ API from other modules, possibly a C backend (like 'csv' and its '_csv').
+
+ The 'extra' argument can be a set of names that wouldn't otherwise be
+ automatically detected as "public", like objects without a proper
+ '__module__' attriubute. If provided, it will be added to the
+ automatically detected ones.
+
+ The 'blacklist' argument can be a set of names that must not be treated
+ as part of the public API even though their names indicate otherwise.
+
+ Usage:
+ import bar
+ import foo
+ import unittest
+ from test import support
+
+ class MiscTestCase(unittest.TestCase):
+ def test__all__(self):
+ support.check__all__(self, foo)
+
+ class OtherTestCase(unittest.TestCase):
+ def test__all__(self):
+ extra = {'BAR_CONST', 'FOO_CONST'}
+ blacklist = {'baz'} # Undocumented name.
+ # bar imports part of its API from _bar.
+ support.check__all__(self, bar, ('bar', '_bar'),
+ extra=extra, blacklist=blacklist)
+
+ """
+
+ if name_of_module is None:
+ name_of_module = (module.__name__, )
+ elif isinstance(name_of_module, str):
+ name_of_module = (name_of_module, )
+
+ expected = set(extra)
+
+ for name in dir(module):
+ if name.startswith('_') or name in blacklist:
+ continue
+ obj = getattr(module, name)
+ if (getattr(obj, '__module__', None) in name_of_module or
+ (not hasattr(obj, '__module__') and
+ not isinstance(obj, types.ModuleType))):
+ expected.add(name)
+ test_case.assertCountEqual(module.__all__, expected)
+
+
class SuppressCrashReport:
"""Try to prevent a crash report from popping up.