Mixing metaclasses and exceptions

S

Steve Menard

In writing the next version of Jpype (Python to Java bridge), I have hot
a rather unpleasant wall ... Hopefully it is I who is doing something
wrong and i can be fixed ...

Since I am bridging Java classes and presenting them as Python classes,
I decided to try to create a corresponding python class for every Jva
classes accessed inside the python program. So far, everything is great
... until we get to exception handling.

Since I am creating classes, on the fly, I thought the easiest and most
pythonic way to do it would be to create a metaclass. This is true also
for the Java exception classes.

The problem arises when I try to raise one of those exception classes
.... it sems Python will not allow classes that have a custom metaclass
to be raised as exceptions! Even though those classes derive from
Exception! To illustrate, try the following snippet :

class mc(type) :
pass

class foo(Exception, object) :
__metaclass__ = mc
pass

try :
raise foo, 'ex'
except Exception, ex :
print ex.__class__, ex


Note the above code has nothing to do with JPype. When I try to run the
above, I get the following error :

exceptions.TypeError exceptions must be classes, instances, or strings
(deprecated), not mc

Is there anything I can do to raise exception that have metaclasses?
Maybe I can make my metaclass acceptable to "raise" somehow?

Thanks for any help anyone can provide.


Steve
 
P

Phillip J. Eby

Jp said:
I'd skip that, though. Your problem doesn't sound "Metaclass!" at me.
I wonder if you could elaborate on your usage? Perhaps there's a better
solution which doesn't involve metaclasses at all.

I suspect he could *maybe* get by without the metaclass, but not
without custom descriptors, which don't always work for classic
classes. In particular, if a Java exception has a static method, he'll
want to map that to a Python classmethod or staticmethod, and as far as
I recall, that stuff just doesn't work with classic classes.

In particular, if a Java class has both a static method and a
non-static method of the same name, there's no way that I know of to
map it into Python using a classic class; you *have* to have a
metaclass with a data descriptor in order to prevent a __dict__ lookup
on the class itself.

The only solution I can think of for this is to only use metaclasses if
you have the static/non-static naming conflicts, or other features of
the mapped class that require using a metaclass or other newstyle-only
features. Or, alternatively, force all Throwable subclasses to be
implemented as classic classes. :(
 
A

Alex Martelli

Phillip J. Eby said:
I suspect he could *maybe* get by without the metaclass, but not
without custom descriptors, which don't always work for classic
classes. In particular, if a Java exception has a static method, he'll
want to map that to a Python classmethod or staticmethod, and as far as
I recall, that stuff just doesn't work with classic classes.

I believe they work fine:
.... @staticmethod
.... def hello(): print "Hello world"
.... Hello world


Class-sick classes have many little niggling problems, but I think
staticmethod and classmethod aren't among them.

In particular, if a Java class has both a static method and a
non-static method of the same name, there's no way that I know of to
map it into Python using a classic class; you *have* to have a
metaclass with a data descriptor in order to prevent a __dict__ lookup
on the class itself.

Well, that's another ball of wax. Does Java support that kind of
overloading...?! Eeek. I believe C++ doesn't and for once is simpler
thereby.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top