Skip to content

Commit 45d767c

Browse files
Use _PyCode_GetScriptXIData().
1 parent 2f4be4e commit 45d767c

File tree

5 files changed

+109
-236
lines changed

5 files changed

+109
-236
lines changed

Lib/test/support/interpreters/channels.py

+1-1
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/support/interpreters/queues.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def list_all():
8888
if not hasattr(self, '_unbound'):
8989
self._set_unbound(unboundop)
9090
else:
91-
assert self._unbound[0] == op
91+
assert self._unbound[0] == unboundop
9292
queues.append(self)
9393
return queues
9494

Lib/test/test__interpreters.py

+14-7
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,12 +1063,9 @@ def test_closure(self):
10531063
spam = True
10541064
def script():
10551065
assert spam
1056-
10571066
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'

Lib/test/test_interpreters/test_api.py

+21-8
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,8 @@ 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, (), {}),
1020+
#(call_func_return_shareable, (), {}),
1021+
#(call_func_return_not_shareable, (), {}),
10151022
(Spam.noop, (), {}),
10161023
]):
10171024
with self.subTest(f'success case #{i+1}'):
@@ -1036,6 +1043,8 @@ def test_call(self):
10361043
(call_func_complex, ('custom', 'spam!'), {}),
10371044
(call_func_complex, ('custom-inner', 'eggs!'), {}),
10381045
(call_func_complex, ('???',), {'exc': ValueError('spam')}),
1046+
(call_func_return_shareable, (), {}),
1047+
(call_func_return_not_shareable, (), {}),
10391048
]):
10401049
with self.subTest(f'invalid case #{i+1}'):
10411050
with self.assertRaises(Exception):
@@ -1051,8 +1060,8 @@ def test_call_in_thread(self):
10511060

10521061
for i, (callable, args, kwargs) in enumerate([
10531062
(call_func_noop, (), {}),
1054-
(call_func_return_shareable, (), {}),
1055-
(call_func_return_not_shareable, (), {}),
1063+
#(call_func_return_shareable, (), {}),
1064+
#(call_func_return_not_shareable, (), {}),
10561065
(Spam.noop, (), {}),
10571066
]):
10581067
with self.subTest(f'success case #{i+1}'):
@@ -1079,6 +1088,8 @@ def test_call_in_thread(self):
10791088
(call_func_complex, ('custom', 'spam!'), {}),
10801089
(call_func_complex, ('custom-inner', 'eggs!'), {}),
10811090
(call_func_complex, ('???',), {'exc': ValueError('spam')}),
1091+
(call_func_return_shareable, (), {}),
1092+
(call_func_return_not_shareable, (), {}),
10821093
]):
10831094
with self.subTest(f'invalid case #{i+1}'):
10841095
if args or kwargs:
@@ -1610,8 +1621,10 @@ def test_exec(self):
16101621
def test_call(self):
16111622
with self.subTest('no args'):
16121623
interpid = _interpreters.create()
1613-
exc = _interpreters.call(interpid, call_func_return_shareable)
1614-
self.assertIs(exc, None)
1624+
with self.assertRaises(ValueError):
1625+
_interpreters.call(interpid, call_func_return_shareable)
1626+
# exc = _interpreters.call(interpid, call_func_return_shareable)
1627+
# self.assertIs(exc, None)
16151628

16161629
with self.subTest('uncaught exception'):
16171630
interpid = _interpreters.create()

0 commit comments

Comments
 (0)