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 22c82be5df)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
Miss Islington (bot) 2018-03-05 11:23:08 -08:00 committed by GitHub
parent 73a43960c7
commit 6935a51167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -635,6 +635,9 @@ class ModifiedInterpreter(InteractiveInterpreter):
if source is None: if source is None:
with tokenize.open(filename) as fp: with tokenize.open(filename) as fp:
source = fp.read() source = fp.read()
if use_subprocess:
source = (f"__file__ = r'''{os.path.abspath(filename)}'''\n"
+ source + "\ndel __file__")
try: try:
code = compile(source, filename, "exec") code = compile(source, filename, "exec")
except (OverflowError, SyntaxError): except (OverflowError, SyntaxError):

View File

@ -0,0 +1,7 @@
Set ``__file__`` while running a startup file. Like Python, IDLE optionally
runs one startup file in the Shell window before presenting the first interactive
input prompt. For IDLE, ``-s`` runs a file named in environmental variable
:envvar:`IDLESTARTUP` or :envvar:`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.