When the file named by PYTHONSTARTUP is executed, an
__file__ is left behind in __main__ even though it
doesn't apply to commands run in the interactive
interpreter. This causes confusing output when
warnings are generated. For example, running CVS
Python with -Wall:
>>> raise 'foo'
/home/fdrake/env/python/INIT.py:1:
PendingDeprecationWarning: raising a string exception
is deprecated
# INIT.py
Traceback (most recent call last):
File "<stdin>", line 1, in ?
foo
Clearly, the deprecation warning is not generated
because of anything in the file named.
This patch causes the __file__ entry in the __main__
module to be removed after the PYTHONSTARTUP file has
been run. __file__ was not added to __main__ in Python
2.2; this is caused by a change to
PyRun_SimpleFileExFlags() in 2.3 (in particular, a
change I made so __file__ would be set while running
PYTHONSTARTUP). It does this simply by causing
PyRun_SimpleFileExFlags() to clean up after itself.
Functions defined in PYTHONSTARTUP that need __file__
after running will simply need to cache the value
somewhere (possibly just under a different name in
__main__'s globals).
|