This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dangyogi
Recipients dangyogi, taleinat
Date 2010-06-11.20:21:52
SpamBayes Score 1.0935344e-06
Marked as misclassified No
Message-id <AANLkTikb8c9ikffDj59clvwcuP1_V-69Eco8cmWAFezu@mail.gmail.com>
In-reply-to <1276269089.89.0.608423270885.issue8515@psf.upfronthosting.co.za>
Content
No, IDLE compiles the module (with the 'compile' built-in using the 'exec'
option) and then does an 'exec' on the code (in PyShell.py).  It has several
lines of code that it runs before this exec to prepare the environment that
the code is run in.  It appears to be an oversight that the __file__
variable is not being set as a part of this preparation code to match the
behavior of the python CLI.  The patch that I included only changes one line
of this preparation code to also set the __file__ variable and that fixes
the problem.  If you examine the IDLE code in the immediate vicinity of my
patch you will see this.

I have several use cases where I'm relying on the __file__ variable in my
module so that it can find other non .py files that it needs in the same
directory that it's in.  This works under all combinations of uses from the
CLI, but fails in IDLE using Run Module.

The language reference
manual<http://docs.python.org/reference/datamodel.html?highlight=__file__>states
under "Module":

Predefined (writable) attributes: __name__ is the module’s name; __doc__ is
> the module’s documentation string, or None if unavailable; __file__ is the
> pathname of the file from which the module was loaded, if it was loaded from
> a file. The __file__ attribute is not present for C modules that are
> statically linked into the interpreter; for extension modules loaded
> dynamically from a shared library, it is the pathname of the shared library
> file.
>

The python CLI honors this definition in all cases, but IDLE/Run Module does
not.

On Fri, Jun 11, 2010 at 11:11 AM, Tal Einat <report@bugs.python.org> wrote:

>
> Tal Einat <taleinat@users.sourceforge.net> added the comment:
>
> I believe IDLE runs modules via execfile(), so I would expect the behavior
> to be similar, and execfile() does not set __file__. Doing "Run Module" is
> also IMO equivalent to doing execfile(), so this behavior retains
> consistency.
>
> However, I would expect __file__ to be set when running IDLE -r <script>,
> but I get "name '__file__' is not defined" (with Python 2.6.2). This is
> inconsistent and should be fixed.
>
> ----------
> nosy: +taleinat
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue8515>
> _______________________________________
>
Files
File name Uploaded
unnamed dangyogi, 2010-06-11.20:21:51
History
Date User Action Args
2010-06-11 20:21:55dangyogisetrecipients: + dangyogi, taleinat
2010-06-11 20:21:53dangyogilinkissue8515 messages
2010-06-11 20:21:52dangyogicreate