Menu

__builtins__ different in debug vs. run

2007-08-15
2013-03-15
  • Ryan O'Rourke

    Ryan O'Rourke - 2007-08-15

    The Python 2.5 documentation has this mysterious note about __builtins__:

    "As an implementation detail, most modules have the name __builtins__ (note the "s") made available as part of their globals. The value of __builtins__ is normally either this module or the value of this modules's __dict__ attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python."

    My code wants to add a global variable to __builtins__ (yes, a hack, but bear with me).  I'm finding that if I run the module with the run command (or just from a command line), __builtins__ shows up as a module.  However, if I try the PyDev debugger, __builtins__ shows up as a dictionary.

    This is causing problems since the syntax needed to add my variable is different depending on whether __builtins__ is a module or a dictionary.  It's work around-able, I suppose, but what is going on here?  Is the debugger using a different implementation of python for some reason?  Did I make a mistake in configuration somewhere?

    (Running PyDev 1.3.8 on Eclipse 3.3.0, Windows, Python 2.5.)

     
    • Fabio Zadrozny

      Fabio Zadrozny - 2007-08-16

      Can you give details on which code you're using (and what's working and what's not working)?

      The debugger basically has its own main file which sets things up and then calls the python 'execfile' function, passing the globals and locals available (so that the things from the debugger don't interfere with the program), so, that might be the reason this behavior is different (but that needs to be checked).

      Cheers,

      Fabio

       
    • Ryan O'Rourke

      Ryan O'Rourke - 2007-08-16

      Well, I have code like this:

      __builtins__.gServer = this;

      Works if it's a module, fails if it's a dictionary (in which case, "__builtins__["gServer"] = this" would work.)

      This code is in an __init__ function.  The class is being created from a bit of code that looks like this:

      if __name__ == "__main__":
          omg.startServer(Serv, Options)  # startServer is going to instance a Serv.

      This call to startServer is in the Serv module, and it's Serv's __init__ that's got the assignment to __builtins__.gServer, above.

      On the call stack in the debugger, I see the execfile call from pydevd's run function, just above the part where Serv's "if __name__ == "__main__" is being executed.

       
      • Fabio Zadrozny

        Fabio Zadrozny - 2007-08-16

        Please report that as a bug... it's probably related to what's passed to the execfile (haven't checked yet).

        Cheers,

        Fabio