Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Mar 5, 2018

Conversation

terryjreedy
Copy link
Member

@terryjreedy terryjreedy commented Mar 5, 2018

@@ -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__")
Copy link
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
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
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
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
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

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

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
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

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