469,923 Members | 1,314 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

metaclasses and Exceptions

From previous discussion [1] it seems that it's not possible to raise
new-style objects. Does this effectively mean that it's impossible to
use anything with a metaclass as an exception? i.e., am I missing some
nifty trick that would allow me to do this in the meantime?

If not, does anyone anticipate this changing anytime soon?

Cheers and thanks,

1. http://aspn.activestate.com/ASPN/Mai...n-list/1095990
Jul 18 '05 #1
4 1608
aa**@pythoncraft.com (Aahz) wrote in message news:<ce**********@panix3.panix.com>...
In article <fc*************************@posting.google.com> ,
Mark Nottingham <mn******@gmail.com> wrote:

From previous discussion [1] it seems that it's not possible to raise
new-style objects. Does this effectively mean that it's impossible to
use anything with a metaclass as an exception? i.e., am I missing some
nifty trick that would allow me to do this in the meantime?


Why do you want to do this?


I have to define a fairly large number (~40) of exceptions, and need
to keep a dictionary keyed on an error code for each, so that the
appropriate exception can be looked up. While I can manage it
manually, it would be much cleaner and more reliable to use a
metaclass to automatically create the dictionary from class
attributes.

E.g.,

class MyExceptionNumberOne:
code = "1"

class MyExceptionNumberTwo:
code = "2"

lookup = {
1: MyExceptionNumberOne,
2: MyExceptionNumberTwo,
}

Cheers,
Jul 18 '05 #2
[Mark Nottingham]
I have to define a fairly large number (~40) of exceptions, and
need to keep a dictionary keyed on an error code for each, so that
the appropriate exception can be looked up. While I can manage
it manually, it would be much cleaner and more reliable to use
a metaclass to automatically create the dictionary from class
attributes.


Yes, it is a bit annoying that exceptions are not types. However, what
you could do with metaclasses for building a registry of exceptions,
you could achieve with module introspection. Here is some example code:
---------------------------------------------------------------------->
class Erreur(Exception):
diagnostic = "Erreur Pyam"

def __str__(self):
if self.args:
return self.diagnostic + ': ' + ', '.join(map(str, self.args))
return self.diagnostic

class ErreurDenotation(Erreur):
diagnostic = "Dénotation invalide"

# [etc.]

def fabriquer_erreur_selon_nom():
# J'aurais bien voulu donner une méta-classe à Erreur pour fabriquer le
# registre au vol, mais une classe dérivée de Exception _doit_ être une
# ancienne classe, pour des raisons techniques internes à Python.
from types import ClassType
for nom, valeur in globals().iteritems():
if isinstance(valeur, ClassType) and issubclass(valeur, Erreur):
erreur_selon_nom[nom] = valeur

erreur_selon_nom = {}
fabriquer_erreur_selon_nom()
----------------------------------------------------------------------<
--
François Pinard http://www.iro.umontreal.ca/~pinard
Jul 18 '05 #3
Michael Hudson <mw*@python.net> wrote > One of the areas where PyPy is
different from CPython is that there
are no old-style classes or instances, so exceptions are necessarily
new-style. This has caused no real problems on any Python program
we've tried it on yet...


How does PyPy distinguish between instance and class arguments to
raise?

Given: raise X
Does it pass X or X()?
Raymond Hettinger
Jul 18 '05 #4
py****@rcn.com (Raymond Hettinger) writes:
Michael Hudson <mw*@python.net> wrote > One of the areas where PyPy is
different from CPython is that there
are no old-style classes or instances, so exceptions are necessarily
new-style. This has caused no real problems on any Python program
we've tried it on yet...
How does PyPy distinguish between instance and class arguments to
raise?


"isinstance(x, type)", iirc.

The code is in this file:

http://codespeak.net/svn/pypy/trunk/...ter/pyframe.py

in the function app_normalize_exception(). There's a kind of horrible
issue if you do things like:

class Nasty(type, Exception):
pass

raise Nasty()

I can't remember what PyPy does here (the answer is quite possibly
"break").
Given: raise X
Does it pass X or X()?


Depends on X! Has to, really, or we certainly would have had more
problems from this than we've had.

Cheers,
mwh

--
"declare"? my bogometer indicates that you're really programming
in some other language and trying to force Common Lisp into your
mindset. this won't work. -- Erik Naggum, comp.lang.lisp
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Stephan Diehl | last post: by
3 posts views Thread by Mike C. Fletcher | last post: by
2 posts views Thread by Santiago Aguiar | last post: by
27 posts views Thread by Michele Simionato | last post: by
4 posts views Thread by Michael Sparks | last post: by
13 posts views Thread by Jean-François Doyon | last post: by
1 post views Thread by Hartmut Goebel | last post: by
reply views Thread by Jan-Ole Esleben | last post: by
1 post views Thread by Alan Isaac | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.