Issue1676293

classification
Title: metaclass __init__ dct reference
Type: Severity: normal
Components: Core Versions:
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: h_eriksson, pedronis, pjenvey
Priority: normal Keywords:

Created on 2007-03-08.07:09:32 by h_eriksson, last changed 2008-06-08.02:42:55 by pjenvey.

Messages
msg1534 (view) Author: Henrik Eriksson (h_eriksson) Date: 2007-03-08.07:09:32
The dct object that gets passed to a metaclass' __init__ method seems to point to the actual __dict__ of the class beeing created. I guess it should be a copy?

The following code breaks with a KeyError since delattr removes the attribute from dct.

def __init__(cls, name, bases, dct):
  # ...snip
  dctnames = dct.keys()
  if name.endswith("__doc"):
    if hasattr(cls, name):
      delattr(cls, name)
                
  val = dct[name]
  # ...snip
msg1535 (view) Author: Samuele Pedroni (pedronis) Date: 2007-03-08.09:47:53
why guessing when you can check?  I still don't understand what's the problem, too little information

Python 2.4.3 (#1, Apr  7 2006, 10:54:33) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class T(type): 
...    def __init__(self, name, bases, dct):
...        global d 
...        d = dct
... 
>>> class X(object):
...    __metaclass__ = T
...    global d1
...    d1 = locals()
... 
>>> d1 is d
True
msg1536 (view) Author: Samuele Pedroni (pedronis) Date: 2007-03-08.10:21:16
now I see, there is indeed a copy involved in CPython, but is what is stored on the type which is a copy.

This is honestly an obscure implementation detail, and not copying is faster. Easy enough to do the CPython thing tough.

Still whoever wrote that sort of code that depends on this should feel dirty :) .
msg1537 (view) Author: Henrik Eriksson (h_eriksson) Date: 2007-03-08.10:46:32
Sorry for the confusing description. I should have referred to the mail-thread about "metaclasses in jython" on jython-dev (Charlie asked me to post the bug). 

Hehe... the code is from CherryPy 3.
msg1538 (view) Author: Henrik Eriksson (h_eriksson) Date: 2007-03-08.10:59:29
As you (Samule) say this seems like an obscure implementation detail and I'm not sure it should be fixed... I could try reasoning with the CP folks and see if they can fix it in their code instead.
msg3244 (view) Author: Philip Jenvey (pjenvey) Date: 2008-06-08.02:42:53
I ended up fixing this in r4336. As Samuele points out it'd be 
preferable if we didn't have to mimic this implementation detail, but 
the fact that it can cause such subtle bugs that both I and Henrik ran 
into in different codebases warrants it, I think

Pylons or some related dependency was also relying on this behavior, 
though I can't recall what exactly it was now
History
Date User Action Args
2008-06-08 02:42:55pjenveysetstatus: open -> closed
nosy: + pjenvey
resolution: fixed
messages: + msg3244
2007-03-08 07:09:32h_erikssoncreate