Skip to content

Commit bbf8048

Browse files
[3.14] gh-132775: Use _PyCode GetScriptXIData() (gh-134515)
(cherry picked from commit 09e72cf, AKA gh-134511) Co-authored-by: Eric Snow <[email protected]>
1 parent c31b25c commit bbf8048

File tree

4 files changed

+104
-240
lines changed

4 files changed

+104
-240
lines changed

Lib/test/support/interpreters/channels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def list_all():
6969
if not hasattr(send, '_unboundop'):
7070
send._set_unbound(unboundop)
7171
else:
72-
assert send._unbound[0] == op
72+
assert send._unbound[0] == unboundop
7373
channels.append(chan)
7474
return channels
7575

Lib/test/test__interpreters.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,15 @@ def setUp(self):
474474

475475
def test_signatures(self):
476476
# See https://github.com/python/cpython/issues/126654
477-
msg = "expected 'shared' to be a dict"
477+
msg = r'_interpreters.exec\(\) argument 3 must be dict, not int'
478478
with self.assertRaisesRegex(TypeError, msg):
479479
_interpreters.exec(self.id, 'a', 1)
480480
with self.assertRaisesRegex(TypeError, msg):
481481
_interpreters.exec(self.id, 'a', shared=1)
482+
msg = r'_interpreters.run_string\(\) argument 3 must be dict, not int'
482483
with self.assertRaisesRegex(TypeError, msg):
483484
_interpreters.run_string(self.id, 'a', shared=1)
485+
msg = r'_interpreters.run_func\(\) argument 3 must be dict, not int'
484486
with self.assertRaisesRegex(TypeError, msg):
485487
_interpreters.run_func(self.id, lambda: None, shared=1)
486488

@@ -952,7 +954,8 @@ def test_invalid_syntax(self):
952954
""")
953955

954956
with self.subTest('script'):
955-
self.assert_run_failed(SyntaxError, script)
957+
with self.assertRaises(SyntaxError):
958+
_interpreters.run_string(self.id, script)
956959

957960
with self.subTest('module'):
958961
modname = 'spam_spam_spam'
@@ -1019,12 +1022,19 @@ def script():
10191022
with open(w, 'w', encoding="utf-8") as spipe:
10201023
with contextlib.redirect_stdout(spipe):
10211024
print('it worked!', end='')
1025+
failed = None
10221026
def f():
1023-
_interpreters.set___main___attrs(self.id, dict(w=w))
1024-
_interpreters.run_func(self.id, script)
1027+
nonlocal failed
1028+
try:
1029+
_interpreters.set___main___attrs(self.id, dict(w=w))
1030+
_interpreters.run_func(self.id, script)
1031+
except Exception as exc:
1032+
failed = exc
10251033
t = threading.Thread(target=f)
10261034
t.start()
10271035
t.join()
1036+
if failed:
1037+
raise Exception from failed
10281038

10291039
with open(r, encoding="utf-8") as outfile:
10301040
out = outfile.read()
@@ -1053,19 +1063,16 @@ def test_closure(self):
10531063
spam = True
10541064
def script():
10551065
assert spam
1056-
1057-
with self.assertRaises(TypeError):
1066+
with self.assertRaises(ValueError):
10581067
_interpreters.run_func(self.id, script)
10591068

1060-
# XXX This hasn't been fixed yet.
1061-
@unittest.expectedFailure
10621069
def test_return_value(self):
10631070
def script():
10641071
return 'spam'
10651072
with self.assertRaises(ValueError):
10661073
_interpreters.run_func(self.id, script)
10671074

1068-
@unittest.skip("we're not quite there yet")
1075+
# @unittest.skip("we're not quite there yet")
10691076
def test_args(self):
10701077
with self.subTest('args'):
10711078
def script(a, b=0):

Lib/test/test_interpreters/test_api.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,16 @@ def test_bad_script(self):
839839
interp.exec(10)
840840

841841
def test_bytes_for_script(self):
842+
r, w = self.pipe()
843+
RAN = b'R'
844+
DONE = b'D'
842845
interp = interpreters.create()
843-
with self.assertRaises(TypeError):
844-
interp.exec(b'print("spam")')
846+
interp.exec(f"""if True:
847+
import os
848+
os.write({w}, {RAN!r})
849+
""")
850+
os.write(w, DONE)
851+
self.assertEqual(os.read(r, 1), RAN)
845852

846853
def test_with_background_threads_still_running(self):
847854
r_interp, w_interp = self.pipe()
@@ -1010,8 +1017,6 @@ def test_call(self):
10101017

10111018
for i, (callable, args, kwargs) in enumerate([
10121019
(call_func_noop, (), {}),
1013-
(call_func_return_shareable, (), {}),
1014-
(call_func_return_not_shareable, (), {}),
10151020
(Spam.noop, (), {}),
10161021
]):
10171022
with self.subTest(f'success case #{i+1}'):
@@ -1036,6 +1041,8 @@ def test_call(self):
10361041
(call_func_complex, ('custom', 'spam!'), {}),
10371042
(call_func_complex, ('custom-inner', 'eggs!'), {}),
10381043
(call_func_complex, ('???',), {'exc': ValueError('spam')}),
1044+
(call_func_return_shareable, (), {}),
1045+
(call_func_return_not_shareable, (), {}),
10391046
]):
10401047
with self.subTest(f'invalid case #{i+1}'):
10411048
with self.assertRaises(Exception):
@@ -1051,8 +1058,6 @@ def test_call_in_thread(self):
10511058

10521059
for i, (callable, args, kwargs) in enumerate([
10531060
(call_func_noop, (), {}),
1054-
(call_func_return_shareable, (), {}),
1055-
(call_func_return_not_shareable, (), {}),
10561061
(Spam.noop, (), {}),
10571062
]):
10581063
with self.subTest(f'success case #{i+1}'):
@@ -1079,6 +1084,8 @@ def test_call_in_thread(self):
10791084
(call_func_complex, ('custom', 'spam!'), {}),
10801085
(call_func_complex, ('custom-inner', 'eggs!'), {}),
10811086
(call_func_complex, ('???',), {'exc': ValueError('spam')}),
1087+
(call_func_return_shareable, (), {}),
1088+
(call_func_return_not_shareable, (), {}),
10821089
]):
10831090
with self.subTest(f'invalid case #{i+1}'):
10841091
if args or kwargs:
@@ -1618,8 +1625,8 @@ def test_exec(self):
16181625
def test_call(self):
16191626
with self.subTest('no args'):
16201627
interpid = _interpreters.create()
1621-
exc = _interpreters.call(interpid, call_func_return_shareable)
1622-
self.assertIs(exc, None)
1628+
with self.assertRaises(ValueError):
1629+
_interpreters.call(interpid, call_func_return_shareable)
16231630

16241631
with self.subTest('uncaught exception'):
16251632
interpid = _interpreters.create()

0 commit comments

Comments
 (0)