Skip to content

Commit 6a5fb7b

Browse files
Fix the Interpreter.run() signature.
1 parent b8ea359 commit 6a5fb7b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Lib/test/support/interpreters.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,19 @@ def close(self):
9292
return _interpreters.destroy(self._id)
9393

9494
# XXX Rename "run" to "exec"?
95-
def run(self, src_str, /, channels=None):
95+
# XXX Do not allow init to overwrite (by default)?
96+
def run(self, src_str, /, *, init=None):
9697
"""Run the given source code in the interpreter.
9798
9899
This is essentially the same as calling the builtin "exec"
99100
with this interpreter, using the __dict__ of its __main__
100101
module as both globals and locals.
101102
103+
If "init" is provided, it must be a dict mapping attribute names
104+
to "shareable" objects, including channels. These are set as
105+
attributes on the __main__ module before the given code is
106+
executed. If a name is already bound then it is overwritten.
107+
102108
There is no return value.
103109
104110
If the code raises an unhandled exception then a RunFailedError
@@ -110,7 +116,7 @@ def run(self, src_str, /, channels=None):
110116
that time, the previous interpreter is allowed to run
111117
in other threads.
112118
"""
113-
_interpreters.exec(self._id, src_str, channels)
119+
_interpreters.exec(self._id, src_str, init)
114120

115121

116122
def create_channel():

Lib/test/test_interpreters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def clean_up_interpreters():
3939
pass # already destroyed
4040

4141

42-
def _run_output(interp, request, channels=None):
42+
def _run_output(interp, request, init=None):
4343
script, rpipe = _captured_script(request)
4444
with rpipe:
45-
interp.run(script, channels=channels)
45+
interp.run(script, init=init)
4646
return rpipe.read()
4747

4848

@@ -953,7 +953,7 @@ def test_send_recv_different_interpreters(self):
953953
print(id(orig2))
954954
s.send_nowait(orig2)
955955
"""),
956-
channels=dict(r=r1, s=s2),
956+
init=dict(r=r1, s=s2),
957957
)
958958
obj2 = r2.recv()
959959

@@ -1027,7 +1027,7 @@ def test_send_recv_nowait_different_interpreters(self):
10271027
print(id(orig2))
10281028
s.send_nowait(orig2)
10291029
"""),
1030-
channels=dict(r=r1, s=s2),
1030+
init=dict(r=r1, s=s2),
10311031
)
10321032
obj2 = r2.recv_nowait()
10331033

0 commit comments

Comments
 (0)