Skip to content

_testinternalcapi is imported without guards in some test modules #109721

@sobolevn

Description

@sobolevn

Bug report

I found at least three cases where _testinternalcapi is just imported, we cannot do that, because other python implementation which reuse our test cases will fail on this.

  1. def check_pythonmalloc(self, env_var, name):
    code = 'import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())'
    env = dict(os.environ)
    env.pop('PYTHONDEVMODE', None)
    if env_var is not None:
    env['PYTHONMALLOC'] = env_var
    else:
    env.pop('PYTHONMALLOC', None)
    args = (sys.executable, '-c', code)
    proc = subprocess.run(args,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    universal_newlines=True,
    env=env)
    self.assertEqual(proc.stdout.rstrip(), name)
    self.assertEqual(proc.returncode, 0)
    def test_pythonmalloc(self):
    # Test the PYTHONMALLOC environment variable
    pymalloc = support.with_pymalloc()
    if pymalloc:
    default_name = 'pymalloc_debug' if support.Py_DEBUG else 'pymalloc'
    default_name_debug = 'pymalloc_debug'
    else:
    default_name = 'malloc_debug' if support.Py_DEBUG else 'malloc'
    default_name_debug = 'malloc_debug'
    tests = [
    (None, default_name),
    ('debug', default_name_debug),
    ('malloc', 'malloc'),
    ('malloc_debug', 'malloc_debug'),
    ]
    if pymalloc:
    tests.extend((
    ('pymalloc', 'pymalloc'),
    ('pymalloc_debug', 'pymalloc_debug'),
    ))
    for env_var, name in tests:
    with self.subTest(env_var=env_var, name=name):
    self.check_pythonmalloc(env_var, name)

  2. import _testinternalcapi

  3. import _testinternalcapi

There can be several options to solve this:

  1. Use import _testinternalcapi with except ImportError, when some places might need it
  2. Use import_helper.import_module to skip some tests where this module is required
  3. Use @cpython_only decorator

Linked PRs

Metadata

Metadata

Assignees

Labels

testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions