Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-76785: Add Interpreter.prepare_main() #113021

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
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
_interpreters.bind() -> _interpreters.set___main___attrs()
  • Loading branch information
ericsnowcurrently committed Dec 12, 2023
commit 0555fb4744e869f8c90a247d953c3a85d1d5ba23
8 changes: 6 additions & 2 deletions 8 Lib/test/support/interpreters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def close(self):
"""
return _interpreters.destroy(self._id)

# XXX setattr?
def bind(self, ns=None, /, **kwargs):
"""Bind the given values into the interpreter's __main__."""
"""Bind the given values into the interpreter's __main__.

The values must be shareable.
"""
ns = dict(ns, **kwargs) if ns is not None else kwargs
_interpreters.bind(self._id, ns)
_interpreters.set___main___attrs(self._id, ns)

def exec_sync(self, code, /, channels=None):
"""Run the given source code in the interpreter.
Expand Down
2 changes: 1 addition & 1 deletion 2 Lib/test/test__xxinterpchannels.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def test_run_string_arg_unresolved(self):
cid = channels.create()
interp = interpreters.create()

interpreters.bind(interp, dict(cid=cid.send))
interpreters.set___main___attrs(interp, dict(cid=cid.send))
out = _run_output(interp, dedent("""
import _xxinterpchannels as _channels
print(cid.end)
Expand Down
14 changes: 7 additions & 7 deletions 14 Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def test_shareable_types(self):
]
for obj in objects:
with self.subTest(obj):
interpreters.bind(interp, dict(obj=obj))
interpreters.set___main___attrs(interp, dict(obj=obj))
interpreters.run_string(
interp,
f'assert(obj == {obj!r})',
Expand Down Expand Up @@ -721,7 +721,7 @@ def test_with_shared(self):
with open({w}, 'wb') as chan:
pickle.dump(ns, chan)
""")
interpreters.bind(self.id, shared)
interpreters.set___main___attrs(self.id, shared)
interpreters.run_string(self.id, script)
with open(r, 'rb') as chan:
ns = pickle.load(chan)
Expand All @@ -743,7 +743,7 @@ def test_shared_overwrites(self):
ns2 = dict(vars())
del ns2['__builtins__']
""")
interpreters.bind(self.id, shared)
interpreters.set___main___attrs(self.id, shared)
interpreters.run_string(self.id, script)

r, w = os.pipe()
Expand Down Expand Up @@ -775,7 +775,7 @@ def test_shared_overwrites_default_vars(self):
with open({w}, 'wb') as chan:
pickle.dump(ns, chan)
""")
interpreters.bind(self.id, shared)
interpreters.set___main___attrs(self.id, shared)
interpreters.run_string(self.id, script)
with open(r, 'rb') as chan:
ns = pickle.load(chan)
Expand Down Expand Up @@ -1039,7 +1039,7 @@ def script():
with open(w, 'w', encoding="utf-8") as spipe:
with contextlib.redirect_stdout(spipe):
print('it worked!', end='')
interpreters.bind(self.id, dict(w=w))
interpreters.set___main___attrs(self.id, dict(w=w))
interpreters.run_func(self.id, script)

with open(r, encoding="utf-8") as outfile:
Expand All @@ -1056,7 +1056,7 @@ def script():
with contextlib.redirect_stdout(spipe):
print('it worked!', end='')
def f():
interpreters.bind(self.id, dict(w=w))
interpreters.set___main___attrs(self.id, dict(w=w))
interpreters.run_func(self.id, script)
t = threading.Thread(target=f)
t.start()
Expand All @@ -1077,7 +1077,7 @@ def script():
with contextlib.redirect_stdout(spipe):
print('it worked!', end='')
code = script.__code__
interpreters.bind(self.id, dict(w=w))
interpreters.set___main___attrs(self.id, dict(w=w))
interpreters.run_func(self.id, code)

with open(r, encoding="utf-8") as outfile:
Expand Down
14 changes: 8 additions & 6 deletions 14 Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,12 @@ PyDoc_STRVAR(get_main_doc,
Return the ID of main interpreter.");

static PyObject *
interp_bind(PyObject *self, PyObject *args)
interp_set___main___attrs(PyObject *self, PyObject *args)
{
PyObject *id, *updates;
if (!PyArg_ParseTuple(args, "OO:" MODULE_NAME ".bind", &id, &updates)) {
if (!PyArg_ParseTuple(args, "OO:" MODULE_NAME ".set___main___attrs",
&id, &updates))
{
return NULL;
}

Expand Down Expand Up @@ -732,8 +734,8 @@ interp_bind(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}

PyDoc_STRVAR(bind_doc,
"bind(id, ns)\n\
PyDoc_STRVAR(set___main___attrs_doc,
"set___main___attrs(id, ns)\n\
\n\
Bind the given attributes in the interpreter's __main__ module.");

Expand Down Expand Up @@ -1085,8 +1087,8 @@ static PyMethodDef module_functions[] = {
{"run_func", _PyCFunction_CAST(interp_run_func),
METH_VARARGS | METH_KEYWORDS, run_func_doc},

{"bind", _PyCFunction_CAST(interp_bind),
METH_VARARGS, bind_doc},
{"set___main___attrs", _PyCFunction_CAST(interp_set___main___attrs),
METH_VARARGS, set___main___attrs_doc},
{"is_shareable", _PyCFunction_CAST(object_is_shareable),
METH_VARARGS | METH_KEYWORDS, is_shareable_doc},

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.