[Python-Dev] New-style exceptions
Michael Hudson
mwh at python.net
Thu Aug 5 15:27:57 CEST 2004
Michael Hudson <mwh at python.net> writes:
> (I have a hacky patch which makes exceptions new-style which I'll
> post in a moment).
Well, it turns out to be a bit big for attaching, so it's here:
http://starship.python.net/crew/mwh/hacks/new-style-exceptions-hacking.diff
This is very much a first cut; no attempt at subtlety. The procedure
went roughly "Hack until it compiles, hack until it doesn't dump core
immediately, hack until most tests pass."
The good news: all tests but test_pickletools pass (and that's doomed;
just look at it).
The bad news: I've entirely broken raising old-style classes (well,
not quite:
>>> try: raise C
... except types.ClassType, c: print c
...
__main__.C
) so I've had to make sure various classes used in the test suite
inherit from exception.
There was a bunch of shallow breakage -- f'ex str(old-style-class) is
quite different from str(new-style-class), which broke various output
comparison tests (try not to gag when you see how I worked around
this) -- but not much that's deep.
You can get a *different* kind of shallow breakage by essentially
removing old-style classes (changing the default metatype to type),
but then 'types.ClassType is type' and this from copy_reg.py:
def pickle(ob_type, pickle_function, constructor_ob=None):
if type(ob_type) is _ClassType:
raise TypeError("copy_reg is not intended for use with classes")
rather fails to do the right thing. I didn't pursue this one very
far.
Obviously, a better attempt would be to allow raising any old-style
class or instance or any subtype of Exception or any instance of a
subtype of Exception -- but that becomes tedious to spell.
I suspect that it would be quite hard -- or at least prohibitively
tedious -- to write code that worked with both old- and new-style
exceptions, so I'm not sure a -N switch to turn them on would work.
At least not without a small battery of helper functions that noone
would bother to use.
I guess this means making exceptions new-style might have to wait for
a Python 3.0-ish flag day of some kind.
Cheers,
mwh
--
Roll on a game of competetive offence-taking.
-- Dan Sheppard, ucam.chat
More information about the Python-Dev
mailing list