[Pythonmac-SIG] Re: sitecustomize and pythonstartup

Christopher Smith csmith@blakeschool.org
Sat, 02 Feb 2002 11:22:22 -0600


>On Sat, 2 Feb 2002, Jack Jansen wrote:
>> > Yes, it's possible.  If we add the following fragment to our
>> > sitecustomize.py:
>> >
>> > ###
>> > import math
>> > import __builtin__
>> > __builtin__.math = math
>> > del math
>> > ###
>> 
>> But this will not only make it show up in interactive 
>> interpreters, it will also make it show up globally, in every 
>> module. I'm not sure this is a good idea...
>
>
>Good point!  Yikes..., ok if we want this automatic math import to work on
>interactive sessions without completely munging every other library in
>Python, perhaps we can employ a variation of the tricky code that
>pydoc.help() uses to try detecting if a session is interactive:
>
>
>###
>import inspect
>def isInteractive():
>    """Returns 1 if we're running in an interpreter window, and false
>    otherwise.  At least, it tries to.  This is a variation of what
>    pydoc.help() tries."""
>    return inspect.stack()[-1][1:4] == (None, 1, "?")
>###
>
>The idea of this is to look at the very end of the frame stack, and if the
>following three characteristics hold:
>
>    1.  The "file" part of the frame is None.
>    2.  We're on line number 1.
>    3.  We have no clue what function we're in. ("?")
>
>then we're probably in interactive mode.
>
>
>And speaking of no clue... I have NO clue if this will work on an IDE.  I
>think that other IDE's actually set the "file name" to something other
>than "None", so perhaps this heuristic needs to be relaxed.  Does anyone
>know a better way of testing if we're in interactive mode or not?

Your code is very close to working in the IDE.  It didn't initially,
so I printed out the contents of the stack() and saw that on the Mac
the (None, 1, "?") appears up toward the top after lines telling what 
current function is running.  So to make the code work I had to change
your [-1] to a [2] and then it works fine:  math is automatically 
loaded and available to the interactive display.  Unfortunately, it
also enabled a call to math that occured in the first line of a script!?

The problem was that the (None, 1, "?") tuple appears in the stack of an
untitled
new script which has, as it's first line, "print math.pi" :-)  So, I
looked at the stack again and saw that PyInteractive appeared in the next
line whereas the stack for a script contains PyEdit instead, so there, I
think is the way to know if you are in the Interactive window or not.

The new code for sitecustomize.py is below and will work, I believe
as long as the name of the Interactive script doesn't change from
PyInteractive.  It will also break if some other functions are running 
before the call to _InteractiveMath (e.g. the __getattr__ appears on 
the stack above the line telling that PyInteractive is running and that's
what makes you have to read further down in the stack to [3] to find the
PyInteractive).

####
def isInteractive():
    """Returns 1 if we're running in an interpreter window, and false
    otherwise.  This is a variation of what pydoc.help() tries."""
    
    """
    return inspect.stack()[3][1].find("PyInteractive")<>0
>
>class _InteractiveMath:
>    def __repr__(self):
>        if isInteractive():
>            import math
>            return repr(math)
>        else:
>            raise NameError, 'name "math" is not defined'
>
>    def __getattr__(self, attr):
>        if isInteractive():
>            import math
>            return getattr(math, attr)
>        else:
>            raise NameError, 'name "math" is not defined'
>
>import __builtin__
>__builtin__.math = _InteractiveMath()
>###

Perhaps this could be extended now to look for module aliases in a folder
like "AutoLoad" and automatically load these at startup, too.  (I would
copy the behavior of the present sitecustomize script that ships with the
mac version which appends search paths to sys.path.)

Thanks for the help, all.

(Now, thanks to the point from Just, I will take a look at getting the 
reset to work in the IDE.)

/c