Re: Strange metaclass behaviour

2006-03-24 Thread Michele Simionato
Ziga Seilnacht wrote: > >>> def in_class_statement2(): > ... frame = sys._getframe(1) > ... return '__module__' in frame.f_locals and not \ > ...'__module__' in frame.f_code.co_varnames On second thought, to break this check is less easy than I expected, so maybe it is reliable

Re: Strange metaclass behaviour

2006-03-24 Thread Michele Simionato
Well, I would not call it a bug, I would call it to cheat ;) The assert is there I just wanted to prevent accidents, not to *really* ensure that 'thisclass' is called inside a class statement. Do you know of any reliable method to enforce that restriction? Michele Simionato -- http:/

Re: Strange metaclass behaviour

2006-03-24 Thread Ziga Seilnacht
Michele Simionato wrote: There is a minor bug in your code: > def thisclass(proc, *args, **kw): >""" Example: >>>> def register(cls): print 'registered' >... >>>> class C: >...thisclass(register) >... >registered >""" ># basic idea stolen from zope.interf

Re: Strange metaclass behaviour

2006-03-24 Thread Michele Simionato
> After 5 years of Python, I still find it impressive how much > vodoo and mojo one can do here :-) True ;) However, I should point out that I never use this stuff in production code. I have found out that for my typical usages metaclasses are too much: a class decorator would be enough and much

Re: Strange metaclass behaviour

2006-03-23 Thread Christian Eder
Michele Simionato wrote: > Still, it is an interesting exercise if you are willing to risk the > melting of your brain, > so here is the code ;) I tried your code and it fixes the double execution, but the __new__ is executed in context of M_A instead of M_B which would be the more specific typ

Re: Strange metaclass behaviour

2006-03-23 Thread Christian Eder
Ziga Seilnacht wrote: > I hope that above explanation helps. > Thanks for your support. I now understand what happens here, but I'm not really happy with the situation. Your solution is a nice workaround, but in a quite huge and complex class framework with a lot a custom metaclasses you don't

Re: Strange metaclass behaviour

2006-03-23 Thread Michele Simionato
Christian Eder wrote: > Hi, > > I think I have discovered a problem in context of > metaclasses and multiple inheritance in python 2.4, > which I could finally reduce to a simple example: > > Look at following code: > > class M_A (type) : > > def __new__ (meta, name, bases, dict) : > p

Re: Strange metaclass behaviour

2006-03-23 Thread Michele Simionato
Ziga Seilnacht wrote: > - Since D does not have a __metaclass__ attribute, >its type is determined from its bases. > - Since A is the first base, its type (M_A) is called; >unfortunately this is not the way metaclasses are >supposed to work; the most derived metaclass should >be s

Re: Strange metaclass behaviour

2006-03-23 Thread Ziga Seilnacht
Christian Eder wrote: > Hi, > > I think I have discovered a problem in context of > metaclasses and multiple inheritance in python 2.4, > which I could finally reduce to a simple example: I don't know if this is a bug; but I will try to expain what is happening; here is an example similar to yours