module name versus function name resolution conflict.

Peter Otten __peter__ at
Tue Jul 7 02:33:48 EDT 2009

rocky wrote:

> Someone recently reported a problem in pydb where a function defined
> in his program was conflicting with a module name that pydb uses. I
> think I understand what's wrong, but I don't have any elegant
> solutions to the problem. Suggestions would be appreciated.
> In a nutshell, here's the problem:
> In file fns:
>   def foo(): print "foo"
> In file
>   import fns, sys
>   def trace_dispatch(frame, event, arg):
>       print frame, event, arg
>       return trace_dispatch
>   sys.settrace(trace_dispatch)
>   execfile('')
> Finally file
>   def fns(): print "This is the *other* fns"
> When you run you get:
> $ python
> foo
> <frame object at 0xdd9030> call None
> foo
> <frame object at 0xdd9030> line None
> Traceback (most recent call last):
>   File "", line 7, in <module>
>     execfile('')
>   File "", line 1, in <module>
>     def fns(): print "This is the *other* fns"
>   File "", line 3, in trace_dispatch
> AttributeError: 'function' object has no attribute 'foo'
> Basically inside the trace hook, local functions are visible and take
> precedence over (global) module names. I could move "import fns"
> inside trace_dispatch(), but I'd have to do that in all of the methods
> that module "fns" is used.  Also note that using the form:
>   from fns import foo
> would eliminate the conflict on "fns", but I'd still have potential
> conflicts on "foo". Using more obscure names (e.g. pydb_fns) would
> reduce the chance of a conflict but not eliminate it.
> Suggestions?

Can you run the program in another namespace?

execfile("", {"__name__":"__main__"})

Alternatively move trace_dispatch into yet another module and import it into 

from elsewhere import trace_dispatch


More information about the Python-list mailing list