|
From: Phillip J. E. <pj...@te...> - 2001-07-14 23:04:12
|
At 11:11 PM 7/14/01 +0200, Samuele Pedroni wrote:
> > Being that the only way is to do it from Java, is there anyway to extend
> > PyClass, while still having the meta-hook take effect? I'd like to extend
> > PyClass without having to copy all its code, just to get around the
> > instanceof test.
>My tricky code does that or a java version of it, or something similar.
>Basically you have to pass a prototype obj that have a __class__ attr set
>to a callable that taking the name,bases and dict args returns a constructed
>instance
>of your subclass of PyClass.
The "tricky code" doesn't work for inheritance. Note that in the example
you gave, subclassing from Z will *not* trigger the metaclass
hook. Basically, it's because you're using a phony metaclass to stand-in
for the *real* metaclass, which means it has to figure out which base is
the dummy, and get rid of it. Once it's been gotten rid of, a subclass is
no longer treated as having a metaclass base.
> > Could there perhaps be instead a interface that Java extenders of PyClass
> > could use to signal their desire to be a meta/pseudo-class? Would a patch
> > to the meta-hook that recognizes such an interface be acceptable? Then I
> > could subclass PyClass in Java, "implement" that interface, and everything
> > would, I believe, be just fine.
>The answer is No. Because:
>- there are tricks to achieve the same without patching the internals
Not with the trick you gave, in either Java or Python, due to the
inheritance issue.
>- I don't have time, now, to make up my mind about that, sorry.
The implementation would consist of three lines added to Py.java:
if (bases[i] instanceof PyMetaClass) {
return ((PyMetaClass)bases[i]).newClassObject(name,new
PyTuple(bases),dict);
}
Plus the declaration of the PyMetaClass interface and its single method,
"newClassObject". I have already tested this implementation by creating a
Java PyExtClass which extends PyClass and implements PyMetaClass. It
*does* work with inheritance. My ExtensionClass.java module then exports
an instance of PyExtClass which can then be used as a base class for Python
classes, and the above meta-hook is correctly triggered.
So, with a very small patch, it would be possible to create proper
metaclasses (i.e. inheritable ones) from Java by extending PyClass and
PyInstance.
Nonetheless, I understand that some things just can't or don't make it into
a project, for whatever reasons.
>OTOH very probably the actual shape of PyClass and PyInstance will change
>in order to support the descr changes in jython 2.2. If you have proposals,
>want to help to integrate that changes in jython I'll be happy to hear from
>you.
I'm not yet familiar enough with the 2.2 proposals... I'm still getting my
head wrapped around 2.1, actually. :)
|