sys.settrace closure interaction bug

Jeremy Hylton jeremy at alum.mit.edu
Wed Oct 4 17:33:37 CEST 2006


Can you file a bug report for this?  I'm guessing that the trace code
has some bad interaction with LOAD_LOCALS, such that a free variable
passed through the class gets treated as local instead.  I can
reproduce this problem in Python 2.4, so it's a long-standing bug.

Also, as a matter of terminology, there is no currying going on.  It's
just a problem with closures.

Jeremy

On 10/1/06, Scott Marks <scott.c.marks at gmail.com> wrote:
> The code below exhibits different behavior depending on whether it invokes
> sys.settrace ('-t' option) or not.  This means that (in a more complicated
> case) debugging the code (which uses sys.settrace) makes it fail.  Any
> ideas?
>
> """ Demonstrace that tracing messes up curried class definitions """
>
>  # Some simple command line parsing: -t or --trace means trace, nothing
> means don't trace
>  import sys
>
> def usage( ):
>      print 'Usage:', sys.argv[ 0 ], '[-t | --trace]'
>     sys.exit( 1 )
>
> if 1 == len( sys.argv ):
>      pass
> elif 2 == len( sys.argv ):
>      if sys.argv[ 1 ]=='-t' or sys.argv[ 1 ]=='--trace':
>         def trace ( frame, event, arg ):
>             # print frame, event, arg
>              return trace
>         sys.settrace( trace )
>      else:
>         usage( )
>  else:
>     usage( )
>
>
>
> # The test: define a class factory with a curried member function
>
> def the_factory( parm1 ):
>      class the_class( object ):
>         def curried( self ): return parm1
>      return the_class
>
>  x = the_factory( 42 )
>
> y = x( )
>
> try:
>     x.parm1
>     print "Failure: x (the manufactured class) has attribute parm1?!"
>  except AttributeError:
>     print "Success with the manufactured class!"
>
> try:
>      y.parm1
>     print "Failure: y (the instance) has attribute parm1?!"
>  except AttributeError:
>     print "Success with the instance!"
>
> assert y.curried( ) == 42, "Currying didn't work?!"
>
>
> _______________________________________________
> Python-bugs-list mailing list
> Unsubscribe:
> http://mail.python.org/mailman/options/python-bugs-list/jeremy%40alum.mit.edu
>
>
>
>


More information about the Python-bugs-list mailing list