From 23d6c10cd7aa2fd5637abe6420e580f39ea8e256 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 20 Mar 2024 05:51:32 -0400 Subject: [PATCH] Add compatibility shims for working with cpython fixtures. --- jaraco/test/cpython.py | 35 +++++++++++++++++++++++++++++++++++ setup.cfg | 1 + 2 files changed, 36 insertions(+) create mode 100644 jaraco/test/cpython.py diff --git a/jaraco/test/cpython.py b/jaraco/test/cpython.py new file mode 100644 index 0000000..47a1ffc --- /dev/null +++ b/jaraco/test/cpython.py @@ -0,0 +1,35 @@ +""" +Compatibility shims for getting stuff from test.support across +Python versions (for compatibility with Python 3.9 and earlier). + +>>> os_helper = try_import('os_helper') or from_test_support('temp_dir') +>>> os_helper.temp_dir + +""" + +import importlib +import types + +from jaraco.context import suppress +from jaraco.collections import Projection + + +def from_test_support(*names): + """ + Return a SimpleNamespace of names from test.support. + + >>> support = from_test_support('swap_item') + >>> support.swap_item + + """ + import test.support + + return types.SimpleNamespace(**Projection(names, vars(test.support))) + + +@suppress(ImportError) +def try_import(name): + """ + Attempt to import a submodule of test.support; return None if missing. + """ + return importlib.import_module(f'test.support.{name}') diff --git a/setup.cfg b/setup.cfg index d34927a..3802fb2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,6 +19,7 @@ python_requires = >=3.8 install_requires = jaraco.functools jaraco.context + jaraco.collections [options.extras_require] testing =