Skip to content

bpo-32984: IDLE - set __file__ for startup files#5981

Merged
terryjreedy merged 3 commits into
python:masterfrom
terryjreedy:file-32984
Mar 5, 2018
Merged

bpo-32984: IDLE - set __file__ for startup files#5981
terryjreedy merged 3 commits into
python:masterfrom
terryjreedy:file-32984

Conversation

@terryjreedy

@terryjreedy terryjreedy commented Mar 5, 2018

Copy link
Copy Markdown
Member

Comment thread Lib/idlelib/pyshell.py Outdated
source = fp.read()
if use_subprocess:
source = (f"__file__ = r'{os.path.abspath(filename)}'\n" +
source + "\ndel __file__")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives a SyntaxError if there's an unescaped quote in the path or file name (such as /home/cheryl's/tests/).

Using triple quotes works:
f"__file__ = r'''{os.path.abspath(filename)}'''\n"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Changed.

input prompt. For IDLE, -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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. by 'editing', I could see and copy the markup (and maybe learn better). I changed one clause to -r file runs file, which I can preview here ;-).

@terryjreedy terryjreedy merged commit 22c82be into python:master Mar 5, 2018
@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @terryjreedy for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

@terryjreedy terryjreedy deleted the file-32984 branch March 5, 2018 16:02
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 5, 2018
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 <tjreedy@udel.edu>
@bedevere-bot

Copy link
Copy Markdown

GH-5986 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 5, 2018
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 <tjreedy@udel.edu>
@bedevere-bot

Copy link
Copy Markdown

GH-5987 is a backport of this pull request to the 3.6 branch.

miss-islington added a commit that referenced this pull request Mar 5, 2018
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 <tjreedy@udel.edu>
@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @terryjreedy for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 5, 2018
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 <tjreedy@udel.edu>
@bedevere-bot

Copy link
Copy Markdown

GH-5993 is a backport of this pull request to the 3.6 branch.

miss-islington added a commit that referenced this pull request Mar 5, 2018
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 <tjreedy@udel.edu>
jo2y pushed a commit to jo2y/cpython that referenced this pull request Mar 23, 2018
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants