On Thu, Apr 09, 2009 at 09:12:49AM -0700, [email protected] wrote:
> > > According to the EnvironmentError exception's description, it's possible
> > > that errno won't be set.
> > >
> > > http://www.python.org/doc/2.4.4/lib/module-exceptions.html
> >
> > I read that as the member will always be there, just sometimes set to None,
> > in which case
> >
> > if isinstance(__e, EnvironmentError) and __e.errno != errno.ENOMEM:
> > raise
> >
> > ...
> >
> > should work, no?
>
> Yes, this looks like it should work. I double-checked the __init__
> method for EnvironmentError, and it always has args, errno, strerror,
> and filename as attributes. Sometimes, though, those attributes are set
> to none.
>
> Python/exceptions.c:469 in Python 2.4.4 for the gory details.
Argh. I forgot that classes that inherit from EnvrionmentError aren't
required to have the same attributes as the base class. This gets
enforced in other OO languages, I think. However, in Python it's
totally legit to have EnvironmentError be in your class heirarchy, yet
have a totally different set of attributes.
I discovered this while looking over bug 2136 that you just
re-categorized. In urllib2.py, there's the following code:
class URLError(IOError):
# URLError is a sub-type of IOError, but it doesn't share any of
# the implementation. need to override __init__ and __str__.
# It sets self.args for compatibility with other EnvironmentError
# subclasses, but args doesn't have the typical format with errno in
# slot 0 and strerror in slot 1. This may be better than nothing.
def __init__(self, reason):
self.args = reason,
self.reason = reason
def __str__(self):
return '<urlopen error %s>' % self.reason
This is an abhorrent programming practice, IMO, since it means that we
get the following useless result in Python:
>>> import urllib2
>>> e = urllib2.URLError("I'm totally broken!")
>>> print isinstance(e, EnvironmentError)
True
>>> print hasattr(e, "errno")
False
This means that it's entirely possible for an EnvironmentError to pass
through this code, not have errno, and cause an AttributeError
exception.
I'm baffled as to why this is considered OK in Python, but not other
languages. It looks like my instinct to assume brokenness and test for
both the class and attribute wasn't so far off after all.
*sigh*
-j
_______________________________________________
pkg-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/pkg-discuss