[Python-Dev] When to signal an error
Jason Orendorff
jason@jorendorff.com
Sat, 19 Jan 2002 17:16:42 -0600
Neal Norwitz:
> Guido van Rossum:
> > But these warnings will always have a different status than purely
> > syntactical error: there are often cases where the user knows better
> > (for example, sometimes an attribute reference can have a desirable
> > side effect).
>
> I agree.
Here's what Pychecker finds in the standard library (as of 2.2).
In each case, the expression is intended to raise an exception if
the named variable or attribute doesn't exist.
Each one could be rewritten (I'm curious as to the prevailing
stylistic opinions on this):
=== code.py (lines 217 and 221)
try:
sys.ps1
except AttributeError:
sys.ps1 = ">>> "
try:
sys.ps2
except AttributeError:
sys.ps2 = "... "
Could be rewritten:
if not hasattr(sys, 'ps1'):
sys.ps1 = ">>> "
if not hasattr(sys, 'ps2'):
sys.ps2 = "... "
=== locale.py (line 721)
try:
LC_MESSAGES
except:
pass
else:
__all__.append("LC_MESSAGES")
Could be rewritten:
if globals().has_key("LC_MESSAGES"):
__all__.append("LC_MESSAGES")
=== pickle.py (line 58)
try:
UnicodeType
except NameError:
UnicodeType = None
Could be rewritten:
globals().setdefault('UnicodeType', None)
## Jason Orendorff http://www.jorendorff.com/