Skip to content

Commit 6935a51

Browse files
bpo-32984: IDLE - set __file__ for startup files (GH-5981)
Like Python, IDLE optionally runs one startup file in the Shell window before presenting the first interactive input prompt. For IDLE, option -s runs a file named in environmental variable IDLESTARTUP or PYTHONSTARTUP; -r file runs file. Python sets __file__ to the startup file name before running the file and unsets it before the first prompt. IDLE now does the same when run normally, without the -n option. (cherry picked from commit 22c82be) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent 73a4396 commit 6935a51

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Lib/idlelib/pyshell.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ def execfile(self, filename, source=None):
635635
if source is None:
636636
with tokenize.open(filename) as fp:
637637
source = fp.read()
638+
if use_subprocess:
639+
source = (f"__file__ = r'''{os.path.abspath(filename)}'''\n"
640+
+ source + "\ndel __file__")
638641
try:
639642
code = compile(source, filename, "exec")
640643
except (OverflowError, SyntaxError):
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Set ``__file__`` while running a startup file. Like Python, IDLE optionally
2+
runs one startup file in the Shell window before presenting the first interactive
3+
input prompt. For IDLE, ``-s`` runs a file named in environmental variable
4+
:envvar:`IDLESTARTUP` or :envvar:`PYTHONSTARTUP`; ``-r file`` runs
5+
``file``. Python sets ``__file__`` to the startup file name before running the
6+
file and unsets it before the first prompt. IDLE now does the same when run
7+
normally, without the ``-n`` option.

0 commit comments

Comments
 (0)