[Python-Dev] Metaclass problem in the "with" statement semantics in PEP 343

Nick Coghlan ncoghlan at iinet.net.au
Mon Nov 28 12:26:53 CET 2005


Given the current semantics of PEP 343 and the following class:

   class null_context(object):
     def __context__(self):
         return self
     def __enter__(self):
         return self
     def __exit__(self, *exc_info):
         pass

Mistakenly writing:

    with null_context:
        # Oops, passed the class instead of an instance

Would give a less than meaningful error message:

     TypeError: unbound method __context__() must be called with null_context 
instance as first argument (got nothing instead)

It's the usual metaclass problem with invoking a slot (or slot equivalent) via 
"obj.__slot__()" rather than via "type(obj).__slot__(obj)" the way the 
underlying C code does.

I think we need to fix the proposed semantics so that they access the slots 
via the type, rather than directly through the instance. Otherwise the slots 
for the with statement will behave strangely when compared to the slots for 
other magic methods.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list