[Python-checkins] bpo-32984: IDLE - set __file__ for startup files (GH-5981)

Miss Islington (bot) webhook-mailer at python.org
Mon Mar 5 14:23:11 EST 2018


https://github.com/python/cpython/commit/6935a511670797a3aaebdf96aad3dcff66baa76e
commit: 6935a511670797a3aaebdf96aad3dcff66baa76e
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-03-05T11:23:08-08:00
summary:

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

Co-authored-by: Terry Jan Reedy <tjreedy at udel.edu>

files:
A Misc/NEWS.d/next/IDLE/2018-03-05-01-29-05.bpo-32984.NGjgT4.rst
M Lib/idlelib/pyshell.py

diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py
index 8b07d52cc487..ee1313161da3 100755
--- a/Lib/idlelib/pyshell.py
+++ b/Lib/idlelib/pyshell.py
@@ -635,6 +635,9 @@ def execfile(self, filename, source=None):
         if source is None:
             with tokenize.open(filename) as fp:
                 source = fp.read()
+                if use_subprocess:
+                    source = (f"__file__ = r'''{os.path.abspath(filename)}'''\n"
+                              + source + "\ndel __file__")
         try:
             code = compile(source, filename, "exec")
         except (OverflowError, SyntaxError):
diff --git a/Misc/NEWS.d/next/IDLE/2018-03-05-01-29-05.bpo-32984.NGjgT4.rst b/Misc/NEWS.d/next/IDLE/2018-03-05-01-29-05.bpo-32984.NGjgT4.rst
new file mode 100644
index 000000000000..15d40b72caaf
--- /dev/null
+++ b/Misc/NEWS.d/next/IDLE/2018-03-05-01-29-05.bpo-32984.NGjgT4.rst
@@ -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.



More information about the Python-checkins mailing list