From dfe980bb82f52f95d60297e3840a4e6708baa147 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Thu, 5 Apr 2012 21:54:39 +0300 Subject: [PATCH] Issue #8515: Set __file__ when run file in IDLE. --- Lib/idlelib/NEWS.txt | 3 +++ Lib/idlelib/ScriptBinding.py | 12 ++++++------ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index d50abd44d29..b28e58ad332 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -1,6 +1,9 @@ What's New in IDLE 3.3? ========================= +- Issue #8515: Set __file__ when run file in IDLE. + Initial patch by Bruce Frederiksen. + - IDLE can be launched as `python -m idlelib` - Issue #14409: IDLE now properly executes commands in the Shell window diff --git a/Lib/idlelib/ScriptBinding.py b/Lib/idlelib/ScriptBinding.py index 18ce9650aed..528adf67a61 100644 --- a/Lib/idlelib/ScriptBinding.py +++ b/Lib/idlelib/ScriptBinding.py @@ -150,16 +150,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 873e8229291..dad7ebdef4f 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -333,6 +333,7 @@ Doug Fort John Fouhy Andrew Francis Martin Franklin +Bruce Frederiksen Robin Friedrich Ivan Frohne Matthias Fuchs diff --git a/Misc/NEWS b/Misc/NEWS index e2e0a2d5fc5..b4a00912761 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,9 @@ Core and Builtins Library ------- +- Issue #8515: Set __file__ when run file in IDLE. + Initial patch by Bruce Frederiksen. + - Issue #14496: Fix wrong name in idlelib/tabbedpages.py. Patch by Popa Claudiu.