Content-Length: 450820 | pFad | http://github.com/python/cpython/pull/117662/commits/9b7bdc4d096a1d6b7e049f6dc6a772a24caf0aab

CA 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
Sketch out tests.
  • Loading branch information
ericsnowcurrently committed Apr 8, 2024
commit 9b7bdc4d096a1d6b7e049f6dc6a772a24caf0aab
25 changes: 24 additions & 1 deletion Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ def test_idempotent(self):
id2 = id(interp)
self.assertNotEqual(id1, id2)

def test_unmanaged(self):
with self.unmanaged_interpreter() as unmanaged:
print(unmanaged)
err = unmanaged.exec(dedent(f"""
import {interpreters.__name__} as interpreters
err = None
try:
interpreters.get_current()
except ValueError as exc:
err = exc
assert exc is not None
"""))
self.assertEqual(err, '')


class ListAllTests(TestBase):

Expand Down Expand Up @@ -199,6 +213,9 @@ def test_idempotent(self):
for interp1, interp2 in zip(actual, expected):
self.assertIs(interp1, interp2)

def test_unmanaged(self):
...


class InterpreterObjectTests(TestBase):

Expand Down Expand Up @@ -1071,7 +1088,7 @@ def test_get_config(self):
with self.subTest('main'):
expected = _interpreters.new_config('legacy')
expected.gil = 'own'
interpid = _interpreters.get_main()
interpid, _ = _interpreters.get_main()
config = _interpreters.get_config(interpid)
self.assert_ns_equal(config, expected)

Expand Down Expand Up @@ -1138,6 +1155,12 @@ def test_create(self):
with self.assertRaises(ValueError):
_interpreters.create(orig)

def test_current(self):
...

def test_list_all(self):
...


if __name__ == '__main__':
# Test needs to be a package, so we can do relative imports.
Expand Down
143 changes: 143 additions & 0 deletions Lib/test/test_interpreters/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import os
import os.path
import pickle
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -66,6 +67,111 @@ def run():
t.join()


class UnmanagedInterpreter:

@classmethod
def start(cls, create_pipe, *, legacy=True):
r_in, _ = inpipe = create_pipe()
r_out, w_out = outpipe = create_pipe()

script = dedent(f"""
import pickle, os, traceback, _testcapi

# Send the interpreter ID.
interpid = _testcapi.get_current_interpid()
os.write({w_out}, pickle.dumps(interpid))

# Run exec requests until "done".
script = b''
while True:
ch = os.read({r_in}, 1)
if ch == b'\0':
if not script:
# done!
break

# Run the provided script.
try:
exec(script)
except Exception as exc:
traceback.print_exc()
err = traceback.format_exception_only(exc)
os.write(w_out, err.encode('utf-8'))
os.write(w_out, b'\0')
script = b''
else:
script += ch
""")
def run():
try:
if legacy:
rc = support.run_in_subinterp(script)
else:
rc = support.run_in_subinterp_with_config(script)
assert rc == 0, rc
except BaseException:
os.write(w_out, b'\0')
raise # re-raise
t = threading.Thread(target=run)
t.start()
try:
# Get the interpreter ID.
data = os.read(r_out, 10)
assert len(data) < 10, repr(data)
assert data != b'\0'
interpid = pickle.loads(data)

return cls(interpid, t, inpipe, outpipe)
except BaseException:
os.write(w_out, b'\0')
raise # re-raise

def __init__(self, id, thread, inpipe, outpipe):
self._id = id
self._thread = thread
self._r_in, self._w_in = inpipe
self._r_out, self._w_out = outpipe

def __repr__(self):
return f'<{type(self).__name__} {self._id}>'

def __enter__(self):
return self

def __exit__(self, *args):
self.shutdown()

@property
def id(self):
return self._id

def exec(self, code):
if isinstance(code, str):
code = code.encode('utf-8')
elif not isinstance(code, bytes):
raise TypeError(f'expected str/bytes, got {code!r}')
if not code:
raise ValueError('empty code')
os.write(self._w_in, code)
os.write(self._w_in, b'\0')

out = b''
while True:
ch = os.read(self._r_out, 1)
if ch == b'\0':
break
out += ch
return out.decode('utf-8')

def shutdown(self, *, wait=True):
t = self._thread
self._thread = None
if t is not None:
os.write(self._w_in, b'\0')
if wait:
t.join()


class TestBase(unittest.TestCase):

def tearDown(self):
Expand Down Expand Up @@ -175,3 +281,40 @@ def assert_ns_equal(self, ns1, ns2, msg=None):
diff = f'namespace({diff})'
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))

def unmanaged_interpreter(self, *, legacy=True):
return UnmanagedInterpreter.start(self.pipe, legacy=legacy)
# @contextlib.contextmanager
# def unmanaged_interpreter(self, *, legacy=True):
# r_id, w_id = self.pipe()
# r_done, w_done = self.pipe()
# script = dedent(f"""
# import marshal, os, _testcapi
#
# # Send the interpreter ID.
# interpid = _testcapi.get_current_interpid()
# data = marshal.dumps(interpid)
# os.write({w_id}, data)
#
# # Wait for "done".
# os.read({r_done}, 1)
# """)
# rc = None
# def task():
# nonlocal rc
# if legacy:
# rc = support.run_in_subinterp(script)
# else:
# rc = support.run_in_subinterp_with_config(script)
# t = threading.Thread(target=task)
# t.start()
# try:
# # Get the interpreter ID.
# data = os.read(r_id, 10)
# assert len(data) < 10, repr(data)
# yield marshal.loads(data)
# finally:
# # Send "done".
# os.write(w_done, b'\0')
# t.join()
# self.assertEqual(rc, 0)








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/9b7bdc4d096a1d6b7e049f6dc6a772a24caf0aab

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy