Content-Length: 526196 | pFad | http://github.com/python/cpython/pull/117662/commits/175080c83444dc3515602b2a9bddce788a213442

FF gh-76785: Add More Tests to test_interpreters.test_api by ericsnowcurrently · Pull Request #117662 · python/cpython · GitHub
Skip to content

gh-76785: Add More Tests to test_interpreters.test_api #117662

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 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bdd8d70
InterpreterError inherits from Exception.
ericsnowcurrently Apr 8, 2024
940f24d
Add _PyInterpreterState_GetIDObject().
ericsnowcurrently Apr 5, 2024
528c6e3
Add _PyXI_NewInterpreter() and _PyXI_EndInterpreter().
ericsnowcurrently Apr 5, 2024
0bd2e6b
Add _testinternalcapi.next_interpreter_id().
ericsnowcurrently Apr 1, 2024
76e32cd
Add _testinternalcapi.exec_interpreter().
ericsnowcurrently Apr 8, 2024
9b7bdc4
Sketch out tests.
ericsnowcurrently Mar 22, 2024
fa28f9b
Flesh out the tests.
ericsnowcurrently Mar 27, 2024
5e31f6e
Add PipeEnd.
ericsnowcurrently Mar 28, 2024
9111a83
Refactor _captured_script().
ericsnowcurrently Mar 28, 2024
0c24e5c
Finish the tests.
ericsnowcurrently Mar 28, 2024
611fa31
Add missing tests.
ericsnowcurrently Apr 1, 2024
bdc09f9
Add more capture/exec helpers.
ericsnowcurrently Apr 8, 2024
be85ff5
Fill out the tests.
ericsnowcurrently Apr 8, 2024
51f18f8
Adjust the tests.
ericsnowcurrently Apr 8, 2024
b1f96d2
Fix clean_up_interpreters().
ericsnowcurrently Apr 9, 2024
cd61643
Fix test_capi.test_misc.
ericsnowcurrently Apr 9, 2024
7de523d
external/unmanaged -> from_capi
ericsnowcurrently Apr 9, 2024
11d38ab
Add a missing decorator.
ericsnowcurrently Apr 9, 2024
1cf31b6
Fix other tests.
ericsnowcurrently Apr 9, 2024
4421169
Fix test_capi.
ericsnowcurrently Apr 9, 2024
c75a115
Add _interpreters.capture_exception().
ericsnowcurrently Apr 9, 2024
b00476d
Raise ExecutionFailed when possible.
ericsnowcurrently Apr 9, 2024
6a82a33
Handle OSError in the _running() script.
ericsnowcurrently Apr 9, 2024
11dae3d
Add PyInterpreterState._whence.
ericsnowcurrently Apr 10, 2024
7c9a2b9
Fix up _testinternalcapi.
ericsnowcurrently Apr 10, 2024
e68654c
Add PyInterpreterState.initialized.
ericsnowcurrently Apr 10, 2024
175080c
Add tests for whence.
ericsnowcurrently Apr 10, 2024
9db5a2b
Set interp-_ready on the main interpreter.
ericsnowcurrently Apr 10, 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
Prev Previous commit
Next Next commit
Add tests for whence.
  • Loading branch information
ericsnowcurrently committed Apr 10, 2024
commit 175080c83444dc3515602b2a9bddce788a213442
52 changes: 48 additions & 4 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from .utils import (
_captured_script, _run_output, _running, TestBase,
requires__testinternalcapi, _testinternalcapi,
requires_test_modules, _testinternalcapi,
)


Expand Down Expand Up @@ -163,7 +163,7 @@ def test_idempotent(self):
id2 = id(interp)
self.assertNotEqual(id1, id2)

@requires__testinternalcapi
@requires_test_modules
def test_created_with_capi(self):
last = 0
for id, *_ in _interpreters.list_all():
Expand Down Expand Up @@ -668,7 +668,7 @@ def test_running(self):
interp.prepare_main({'spam': False})
interp.exec('assert spam is True')

@requires__testinternalcapi
@requires_test_modules
def test_created_with_capi(self):
with self.interpreter_from_capi() as interpid:
interp = interpreters.Interpreter(interpid)
Expand Down Expand Up @@ -1378,7 +1378,7 @@ def test_create(self):
with self.assertRaises(ValueError):
_interpreters.create(orig)

@requires__testinternalcapi
@requires_test_modules
def test_destroy(self):
with self.subTest('from _interpreters'):
interpid = _interpreters.create()
Expand Down Expand Up @@ -1439,6 +1439,50 @@ def test_get_config(self):
config = _interpreters.get_config(interpid)
self.assert_ns_equal(config, orig)

@requires_test_modules
def test_whence(self):
with self.subTest('main'):
interpid, *_ = _interpreters.get_main()
whence = _interpreters.whence(interpid)
self.assertEqual(whence, _interpreters.WHENCE_RUNTIME)

with self.subTest('stdlib'):
interpid = _interpreters.create()
whence = _interpreters.whence(interpid)
self.assertEqual(whence, _interpreters.WHENCE_XI)

for orig, name in {
# XXX Also check WHENCE_UNKNOWN.
_interpreters.WHENCE_LEGACY_CAPI: 'legacy C-API',
_interpreters.WHENCE_CAPI: 'C-API',
_interpreters.WHENCE_XI: 'cross-interpreter C-API',
}.items():
with self.subTest(f'from C-API ({orig}: {name})'):
with self.interpreter_from_capi(whence=orig) as interpid:
whence = _interpreters.whence(interpid)
self.assertEqual(whence, orig)

with self.subTest('from C-API, running'):
text = self.run_temp_from_capi(dedent(f"""
import {_interpreters.__name__} as _interpreters
interpid, *_ = _interpreters.get_current()
print(_interpreters.whence(interpid))
"""),
config=True)
whence = eval(text)
self.assertEqual(whence, _interpreters.WHENCE_CAPI)

with self.subTest('from legacy C-API, running'):
...
text = self.run_temp_from_capi(dedent(f"""
import {_interpreters.__name__} as _interpreters
interpid, *_ = _interpreters.get_current()
print(_interpreters.whence(interpid))
"""),
config=False)
whence = eval(text)
self.assertEqual(whence, _interpreters.WHENCE_LEGACY_CAPI)

def test_is_running(self):
with self.subTest('main'):
interpid, *_ = _interpreters.get_main()
Expand Down
53 changes: 40 additions & 13 deletions Lib/test/test_interpreters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

try:
import _testinternalcapi
import _testcapi
except ImportError:
_testinternalcapi = None
else:
run_in_interpreter = _testinternalcapi.run_in_subinterp_with_config
_testcapi = None

def requires__testinternalcapi(func):
def requires_test_modules(func):
return unittest.skipIf(_testinternalcapi is None, "test requires _testinternalcapi module")(func)


Expand Down Expand Up @@ -509,12 +509,31 @@ def run_and_capture(self, interp, script):
else:
return text

@requires__testinternalcapi
@requires_test_modules
@contextlib.contextmanager
def interpreter_from_capi(self, config='legacy'):
if isinstance(config, str):
def interpreter_from_capi(self, config=None, whence=None):
if config is False:
if whence is None:
whence = _interpreters.WHENCE_LEGACY_CAPI
else:
assert whence in (_interpreters.WHENCE_LEGACY_CAPI,
_interpreters.WHENCE_UNKNOWN), repr(whence)
config = None
elif config is True:
config = _interpreters.new_config('default')
elif config is None:
if whence not in (
_interpreters.WHENCE_LEGACY_CAPI,
_interpreters.WHENCE_UNKNOWN,
):
config = _interpreters.new_config('legacy')
elif isinstance(config, str):
config = _interpreters.new_config(config)
interpid = _testinternalcapi.create_interpreter()

if whence is None:
whence = _interpreters.WHENCE_XI

interpid = _testinternalcapi.create_interpreter(config, whence=whence)
try:
yield interpid
finally:
Expand All @@ -535,7 +554,7 @@ def capturing(self, script):
with capturing:
yield wrapped, capturing.final(force=True)

@requires__testinternalcapi
@requires_test_modules
def run_from_capi(self, interpid, script, *, main=False):
with self.capturing(script) as (wrapped, results):
rc = _testinternalcapi.exec_interpreter(interpid, wrapped, main=main)
Expand Down Expand Up @@ -622,7 +641,7 @@ def exec_interp(script):
with self._running(run_interp, exec_interp):
yield

@requires__testinternalcapi
@requires_test_modules
@contextlib.contextmanager
def running_from_capi(self, interpid, *, main=False):
def run_interp(script):
Expand All @@ -634,12 +653,20 @@ def exec_interp(script):
with self._running(run_interp, exec_interp):
yield

@requires__testinternalcapi
@requires_test_modules
def run_temp_from_capi(self, script, config='legacy'):
if isinstance(config, str):
config = _interpreters.new_config(config)
if config is False:
# Force using Py_NewInterpreter().
run_in_interp = (lambda s, c: _testcapi.run_in_subinterp(s))
config = None
else:
run_in_interp = _testinternalcapi.run_in_subinterp_with_config
if config is True:
config = 'default'
if isinstance(config, str):
config = _interpreters.new_config(config)
with self.capturing(script) as (wrapped, results):
rc = run_in_interpreter(wrapped, config)
rc = run_in_interp(wrapped, config)
assert rc == 0, rc
results.raise_if_failed()
return results.stdout
7 changes: 5 additions & 2 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1443,17 +1443,20 @@ create_interpreter(PyObject *self, PyObject *args, PyObject *kwargs)
PyObject *configobj = NULL;
long whence = _PyInterpreterState_WHENCE_XI;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"|O$p:create_interpreter", kwlist,
"|O$l:create_interpreter", kwlist,
&configobj, &whence))
{
return NULL;
}
if (configobj == Py_None) {
configobj = NULL;
}

// Resolve the config.
PyInterpreterConfig *config = NULL;
PyInterpreterConfig _config;
if (whence == _PyInterpreterState_WHENCE_UNKNOWN
|| whence == _PyInterpreterState_WHENCE_UNKNOWN)
|| whence == _PyInterpreterState_WHENCE_LEGACY_CAPI)
{
if (configobj != NULL) {
PyErr_SetString(PyExc_ValueError, "got unexpected config");
Expand Down
15 changes: 13 additions & 2 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1868,8 +1868,8 @@ void
_PyXI_EndInterpreter(PyInterpreterState *interp,
PyThreadState *tstate, PyThreadState **p_save_tstate)
{
PyThreadState *cur_tstate = PyThreadState_GET();
PyThreadState *save_tstate = NULL;
PyThreadState *cur_tstate = PyThreadState_GET();
if (tstate == NULL) {
if (PyThreadState_GetInterpreter(cur_tstate) == interp) {
tstate = cur_tstate;
Expand All @@ -1889,7 +1889,18 @@ _PyXI_EndInterpreter(PyInterpreterState *interp,
}
}

Py_EndInterpreter(tstate);
long whence = _PyInterpreterState_GetWhence(interp);
assert(whence != _PyInterpreterState_WHENCE_RUNTIME);
if (whence == _PyInterpreterState_WHENCE_UNKNOWN) {
assert(!interp->_ready);
PyThreadState *tstate = PyThreadState_New(interp);
save_tstate = PyThreadState_Swap(tstate);
_PyInterpreterState_Clear(tstate);
PyInterpreterState_Delete(interp);
}
else {
Py_EndInterpreter(tstate);
}

if (p_save_tstate != NULL) {
save_tstate = *p_save_tstate;
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/117662/commits/175080c83444dc3515602b2a9bddce788a213442

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy