Skip to content

Commit 8f36d93

Browse files
0azjonathanslenders
authored andcommitted
Fix incorrect __main__ on script execution (prompt-toolkit#444)
1 parent 8d1ee21 commit 8f36d93

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

ptpython/entry_points/run_ptipython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def run(user_ns=None):
3131
path = a.args[0]
3232
with open(path, "rb") as f:
3333
code = compile(f.read(), path, "exec")
34-
exec(code, {})
34+
exec(code, {'__name__': '__main__', '__file__': path})
3535
else:
3636
enable_deprecation_warnings()
3737

ptpython/entry_points/run_ptpython.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ def run() -> None:
179179
path = a.args[0]
180180
with open(path, "rb") as f:
181181
code = compile(f.read(), path, "exec")
182-
# NOTE: We have to pass an empty dictionary as namespace. Omitting
183-
# this argument causes imports to not be found. See issue #326.
184-
exec(code, {})
182+
# NOTE: We have to pass a dict as namespace. Omitting this argument
183+
# causes imports to not be found. See issue #326.
184+
# However, an empty dict sets __name__ to 'builtins', which
185+
# breaks `if __name__ == '__main__'` checks. See issue #444.
186+
exec(code, {'__name__': '__main__', '__file__': path})
185187

186188
# Run interactive shell.
187189
else:

0 commit comments

Comments
 (0)