Skip to content

Commit e961611

Browse files
gh-132775: Do Not Set __name__ to __main__ With _PyPickle_GetXIData() (gh-133472)
This is a follow-up to gh-133107. I realized that we could end up with an infinite recursion if we try to run a function from __main__ in a subinterpreter.
1 parent 60cdd80 commit e961611

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Python/crossinterp.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,12 @@ _unpickle_context_set_module(struct _unpickle_context *ctx,
512512
struct sync_module_result res = {0};
513513
struct sync_module_result *cached = NULL;
514514
const char *filename = NULL;
515+
const char *run_modname = modname;
515516
if (strcmp(modname, "__main__") == 0) {
516517
cached = &ctx->main.cached;
517518
filename = ctx->main.filename;
519+
// We don't want to trigger "if __name__ == '__main__':".
520+
run_modname = "<fake __main__>";
518521
}
519522
else {
520523
res.failed = PyExc_NotImplementedError;
@@ -533,7 +536,7 @@ _unpickle_context_set_module(struct _unpickle_context *ctx,
533536
res.failed = PyExc_NotImplementedError;
534537
goto finally;
535538
}
536-
res.loaded = runpy_run_path(filename, modname);
539+
res.loaded = runpy_run_path(filename, run_modname);
537540
if (res.loaded == NULL) {
538541
Py_CLEAR(res.module);
539542
res.failed = _PyErr_GetRaisedException(ctx->tstate);

0 commit comments

Comments
 (0)