From b6eeef4165a616443b01a948c0997ca7695d6a4a Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 30 Jun 2013 19:07:49 -0400 Subject: [PATCH] Issue #8515: Set __file__ when run file in IDLE. Backport 2c276d0553ff by Andrew Svetlov, based on initial patch by Bruce Frederiksen. --- Lib/idlelib/ScriptBinding.py | 12 ++++++------ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Lib/idlelib/ScriptBinding.py b/Lib/idlelib/ScriptBinding.py index a1d5aaf7f74c..665b3b241ccd 100644 --- a/Lib/idlelib/ScriptBinding.py +++ b/Lib/idlelib/ScriptBinding.py @@ -152,16 +152,16 @@ class ScriptBinding: dirname = os.path.dirname(filename) # XXX Too often this discards arguments the user just set... interp.runcommand("""if 1: - _filename = %r + __file__ = {filename!r} import sys as _sys from os.path import basename as _basename if (not _sys.argv or - _basename(_sys.argv[0]) != _basename(_filename)): - _sys.argv = [_filename] + _basename(_sys.argv[0]) != _basename(__file__)): + _sys.argv = [__file__] import os as _os - _os.chdir(%r) - del _filename, _sys, _basename, _os - \n""" % (filename, dirname)) + _os.chdir({dirname!r}) + del _sys, _basename, _os + \n""".format(filename=filename, dirname=dirname)) interp.prepend_syspath(filename) # XXX KBK 03Jul04 When run w/o subprocess, runtime warnings still # go to __stderr__. With subprocess, they go to the shell. diff --git a/Misc/ACKS b/Misc/ACKS index 67f0c97eafa9..82e49566f07d 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -317,6 +317,7 @@ Doug Fort John Fouhy Stefan Franke Martin Franklin +Bruce Frederiksen Robin Friedrich Bradley Froehle Ivan Frohne diff --git a/Misc/NEWS b/Misc/NEWS index 175fa9569b29..9048a9862a22 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -73,6 +73,9 @@ Build IDLE ---- +- Issue #8515: Set __file__ when run file in IDLE. + Initial patch by Bruce Frederiksen. + - Issue #5492: Avoid traceback when exiting IDLE caused by a race condition. - Issue #17511: Keep IDLE find dialog open after clicking "Find Next". -- 2.47.3