Re: Mixing metaclasses and exceptions
Phillip J. Eby [EMAIL PROTECTED] wrote: Jp Calderone wrote: 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. I believe they work fine: class sic: ... @staticmethod ... def hello(): print Hello world ... sic.hello() 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing metaclasses and exceptions
On Mon, 27 Dec 2004 21:44:23 -0500, Steve Menard [EMAIL PROTECTED] wrote: 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 : Only instances of classic classes can be raised. This is an arbitrary restriction imposed by CPython. There's no particular reason it need be the case, aside from the fact that no one has bothered to do the necessary work to make new-style classes usable as exceptions. One way you could get around this problem is by making your metaclass return an instance of a classic class. 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. Jp -- http://mail.python.org/mailman/listinfo/python-list
Mixing metaclasses and exceptions
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 -- http://mail.python.org/mailman/listinfo/python-list