Skip to content

gh-76785: Add PyInterpreterConfig Helpers #117170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
bdaef6b
Fix a comment.
ericsnowcurrently Mar 7, 2024
6342635
Add PyInterpreterConfig helpers.
ericsnowcurrently Mar 21, 2024
c48dd00
Add config helpers to _testinternalcapi.
ericsnowcurrently Mar 21, 2024
0796fe9
Use the new helpers in run_in_subinterp_with_config().
ericsnowcurrently Mar 22, 2024
8993b41
Move the PyInterpreterConfig utils to their own file.
ericsnowcurrently Mar 22, 2024
a113017
_PyInterpreterState_ResolveConfig() -> _PyInterpreterConfig_InitFromS…
ericsnowcurrently Mar 22, 2024
fce72b8
_testinternalcapi.new_interpreter_config() -> _xxsubinterpreters.new_…
ericsnowcurrently Mar 22, 2024
c52e484
_testinternalcapi.get_interpreter_config() -> _xxsubinterpreters.get_…
ericsnowcurrently Mar 22, 2024
a2983ce
Call _PyInterpreterState_RequireIDRef() in _interpreters._incref().
ericsnowcurrently Mar 22, 2024
05a081e
_testinternalcapi.interpreter_incref() -> _interpreters._incref()
ericsnowcurrently Mar 23, 2024
8a39bbc
Supporting passing a config to _xxsubinterpreters.create().
ericsnowcurrently Mar 22, 2024
1173cd1
Factor out new_interpreter().
ericsnowcurrently Mar 22, 2024
92c11d3
Fix test_import.
ericsnowcurrently Mar 25, 2024
edda48d
Fix an outdent.
ericsnowcurrently Mar 25, 2024
5f617ed
Call _PyInterpreterState_RequireIDRef() in the right places.
ericsnowcurrently Apr 1, 2024
c504c79
Drop an unnecessary _PyInterpreterState_IDInitref() call.
ericsnowcurrently Apr 1, 2024
8a75c90
Reduce to just the new internal C-API.
ericsnowcurrently Apr 2, 2024
a38cda7
Adjust test_get_config.
ericsnowcurrently Apr 2, 2024
cae0482
Remove trailing whitespace.
ericsnowcurrently Apr 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ PyAPI_FUNC(char*) _Py_SetLocaleFromEnv(int category);
// Export for special main.c string compiling with source tracebacks
int _PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompilerFlags *flags);


/* interpreter config */

// Export for _testinternalcapi shared extension
PyAPI_FUNC(int) _PyInterpreterConfig_InitFromState(
PyInterpreterConfig *,
PyInterpreterState *);
PyAPI_FUNC(PyObject *) _PyInterpreterConfig_AsDict(PyInterpreterConfig *);
PyAPI_FUNC(int) _PyInterpreterConfig_InitFromDict(
PyInterpreterConfig *,
PyObject *);
PyAPI_FUNC(int) _PyInterpreterConfig_UpdateFromDict(
PyInterpreterConfig *,
PyObject *);


#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 13 additions & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,8 +1728,19 @@ def run_in_subinterp_with_config(code, *, own_gil=None, **config):
import _testinternalcapi
if own_gil is not None:
assert 'gil' not in config, (own_gil, config)
config['gil'] = 2 if own_gil else 1
return _testinternalcapi.run_in_subinterp_with_config(code, **config)
config['gil'] = 'own' if own_gil else 'shared'
else:
gil = config['gil']
if gil == 0:
config['gil'] = 'default'
elif gil == 1:
config['gil'] = 'shared'
elif gil == 2:
config['gil'] = 'own'
else:
raise NotImplementedError(gil)
config = types.SimpleNamespace(**config)
return _testinternalcapi.run_in_subinterp_with_config(code, config)


def _check_tracemalloc():
Expand Down
251 changes: 251 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,257 @@ def test_module_state_shared_in_global(self):
self.assertEqual(main_attr_id, subinterp_attr_id)


class InterpreterConfigTests(unittest.TestCase):

supported = {
'isolated': types.SimpleNamespace(
use_main_obmalloc=False,
allow_fork=False,
allow_exec=False,
allow_threads=True,
allow_daemon_threads=False,
check_multi_interp_extensions=True,
gil='own',
),
'legacy': types.SimpleNamespace(
use_main_obmalloc=True,
allow_fork=True,
allow_exec=True,
allow_threads=True,
allow_daemon_threads=True,
check_multi_interp_extensions=False,
gil='shared',
),
'empty': types.SimpleNamespace(
use_main_obmalloc=False,
allow_fork=False,
allow_exec=False,
allow_threads=False,
allow_daemon_threads=False,
check_multi_interp_extensions=False,
gil='default',
),
}
gil_supported = ['default', 'shared', 'own']

def iter_all_configs(self):
for use_main_obmalloc in (True, False):
for allow_fork in (True, False):
for allow_exec in (True, False):
for allow_threads in (True, False):
for allow_daemon in (True, False):
for checkext in (True, False):
for gil in ('shared', 'own', 'default'):
yield types.SimpleNamespace(
use_main_obmalloc=use_main_obmalloc,
allow_fork=allow_fork,
allow_exec=allow_exec,
allow_threads=allow_threads,
allow_daemon_threads=allow_daemon,
check_multi_interp_extensions=checkext,
gil=gil,
)

def assert_ns_equal(self, ns1, ns2, msg=None):
# This is mostly copied from TestCase.assertDictEqual.
self.assertEqual(type(ns1), type(ns2))
if ns1 == ns2:
return

import difflib
import pprint
from unittest.util import _common_shorten_repr
standardMsg = '%s != %s' % _common_shorten_repr(ns1, ns2)
diff = ('\n' + '\n'.join(difflib.ndiff(
pprint.pformat(vars(ns1)).splitlines(),
pprint.pformat(vars(ns2)).splitlines())))
diff = f'namespace({diff})'
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))

def test_predefined_config(self):
def check(name, expected):
expected = self.supported[expected]
args = (name,) if name else ()

config1 = _testinternalcapi.new_interp_config(*args)
self.assert_ns_equal(config1, expected)
self.assertIsNot(config1, expected)

config2 = _testinternalcapi.new_interp_config(*args)
self.assert_ns_equal(config2, expected)
self.assertIsNot(config2, expected)
self.assertIsNot(config2, config1)

with self.subTest('default'):
check(None, 'isolated')

for name in self.supported:
with self.subTest(name):
check(name, name)

def test_update_from_dict(self):
for name, vanilla in self.supported.items():
with self.subTest(f'noop ({name})'):
expected = vanilla
overrides = vars(vanilla)
config = _testinternalcapi.new_interp_config(name, **overrides)
self.assert_ns_equal(config, expected)

with self.subTest(f'change all ({name})'):
overrides = {k: not v for k, v in vars(vanilla).items()}
for gil in self.gil_supported:
if vanilla.gil == gil:
continue
overrides['gil'] = gil
expected = types.SimpleNamespace(**overrides)
config = _testinternalcapi.new_interp_config(
name, **overrides)
self.assert_ns_equal(config, expected)

# Override individual fields.
for field, old in vars(vanilla).items():
if field == 'gil':
values = [v for v in self.gil_supported if v != old]
else:
values = [not old]
for val in values:
with self.subTest(f'{name}.{field} ({old!r} -> {val!r})'):
overrides = {field: val}
expected = types.SimpleNamespace(
**dict(vars(vanilla), **overrides),
)
config = _testinternalcapi.new_interp_config(
name, **overrides)
self.assert_ns_equal(config, expected)

with self.subTest('unsupported field'):
for name in self.supported:
with self.assertRaises(ValueError):
_testinternalcapi.new_interp_config(name, spam=True)

# Bad values for bool fields.
for field, value in vars(self.supported['empty']).items():
if field == 'gil':
continue
assert isinstance(value, bool)
for value in [1, '', 'spam', 1.0, None, object()]:
with self.subTest(f'unsupported value ({field}={value!r})'):
with self.assertRaises(TypeError):
_testinternalcapi.new_interp_config(**{field: value})

# Bad values for .gil.
for value in [True, 1, 1.0, None, object()]:
with self.subTest(f'unsupported value(gil={value!r})'):
with self.assertRaises(TypeError):
_testinternalcapi.new_interp_config(gil=value)
for value in ['', 'spam']:
with self.subTest(f'unsupported value (gil={value!r})'):
with self.assertRaises(ValueError):
_testinternalcapi.new_interp_config(gil=value)

@requires_subinterpreters
def test_interp_init(self):
questionable = [
# strange
dict(
allow_fork=True,
allow_exec=False,
),
dict(
gil='shared',
use_main_obmalloc=False,
),
# risky
dict(
allow_fork=True,
allow_threads=True,
),
# ought to be invalid?
dict(
allow_threads=False,
allow_daemon_threads=True,
),
dict(
gil='own',
use_main_obmalloc=True,
),
]
invalid = [
dict(
use_main_obmalloc=False,
check_multi_interp_extensions=False
),
]
def match(config, override_cases):
ns = vars(config)
for overrides in override_cases:
if dict(ns, **overrides) == ns:
return True
return False

def check(config):
script = 'pass'
rc = _testinternalcapi.run_in_subinterp_with_config(script, config)
self.assertEqual(rc, 0)

for config in self.iter_all_configs():
if config.gil == 'default':
continue
if match(config, invalid):
with self.subTest(f'invalid: {config}'):
with self.assertRaises(RuntimeError):
check(config)
elif match(config, questionable):
with self.subTest(f'questionable: {config}'):
check(config)
else:
with self.subTest(f'valid: {config}'):
check(config)

@requires_subinterpreters
def test_get_config(self):
@contextlib.contextmanager
def new_interp(config):
interpid = _testinternalcapi.new_interpreter(config)
try:
yield interpid
finally:
try:
_interpreters.destroy(interpid)
except _interpreters.InterpreterNotFoundError:
pass

with self.subTest('main'):
expected = _testinternalcapi.new_interp_config('legacy')
expected.gil = 'own'
interpid = _interpreters.get_main()
config = _testinternalcapi.get_interp_config(interpid)
self.assert_ns_equal(config, expected)

with self.subTest('isolated'):
expected = _testinternalcapi.new_interp_config('isolated')
with new_interp('isolated') as interpid:
config = _testinternalcapi.get_interp_config(interpid)
self.assert_ns_equal(config, expected)

with self.subTest('legacy'):
expected = _testinternalcapi.new_interp_config('legacy')
with new_interp('legacy') as interpid:
config = _testinternalcapi.get_interp_config(interpid)
self.assert_ns_equal(config, expected)

with self.subTest('custom'):
orig = _testinternalcapi.new_interp_config(
'empty',
use_main_obmalloc=True,
gil='shared',
)
with new_interp(orig) as interpid:
config = _testinternalcapi.get_interp_config(interpid)
self.assert_ns_equal(config, orig)


@requires_subinterpreters
class InterpreterIDTests(unittest.TestCase):

Expand Down
12 changes: 10 additions & 2 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,15 +1823,19 @@ def check_compatible_fresh(self, name, *, strict=False, isolated=False):
**(self.ISOLATED if isolated else self.NOT_ISOLATED),
check_multi_interp_extensions=strict,
)
gil = kwargs['gil']
kwargs['gil'] = 'default' if gil == 0 else (
'shared' if gil == 1 else 'own' if gil == 2 else gil)
_, out, err = script_helper.assert_python_ok('-c', textwrap.dedent(f'''
import _testinternalcapi, sys
assert (
{name!r} in sys.builtin_module_names or
{name!r} not in sys.modules
), repr({name!r})
config = type(sys.implementation)(**{kwargs})
ret = _testinternalcapi.run_in_subinterp_with_config(
{self.import_script(name, "sys.stdout.fileno()")!r},
**{kwargs},
config,
)
assert ret == 0, ret
'''))
Expand All @@ -1847,12 +1851,16 @@ def check_incompatible_fresh(self, name, *, isolated=False):
**(self.ISOLATED if isolated else self.NOT_ISOLATED),
check_multi_interp_extensions=True,
)
gil = kwargs['gil']
kwargs['gil'] = 'default' if gil == 0 else (
'shared' if gil == 1 else 'own' if gil == 2 else gil)
_, out, err = script_helper.assert_python_ok('-c', textwrap.dedent(f'''
import _testinternalcapi, sys
assert {name!r} not in sys.modules, {name!r}
config = type(sys.implementation)(**{kwargs})
ret = _testinternalcapi.run_in_subinterp_with_config(
{self.import_script(name, "sys.stdout.fileno()")!r},
**{kwargs},
config,
)
assert ret == 0, ret
'''))
Expand Down
5 changes: 5 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ PYTHON_OBJS= \
Python/import.o \
Python/importdl.o \
Python/initconfig.o \
Python/interpconfig.o \
Python/instrumentation.o \
Python/intrinsics.o \
Python/jit.o \
Expand Down Expand Up @@ -1680,6 +1681,10 @@ Modules/_xxinterpchannelsmodule.o: $(srcdir)/Modules/_xxinterpchannelsmodule.c $

Python/crossinterp.o: $(srcdir)/Python/crossinterp.c $(srcdir)/Python/crossinterp_data_lookup.h $(srcdir)/Python/crossinterp_exceptions.h

Python/initconfig.o: $(srcdir)/Python/initconfig.c $(srcdir)/Python/config_common.h

Python/interpconfig.o: $(srcdir)/Python/interpconfig.c $(srcdir)/Python/config_common.h

Python/dynload_shlib.o: $(srcdir)/Python/dynload_shlib.c Makefile
$(CC) -c $(PY_CORE_CFLAGS) \
-DSOABI='"$(SOABI)"' \
Expand Down
Loading