[Python-Dev] Deprecating string exceptions

Fred L. Drake, Jr. fdrake@acm.org
Wed, 27 Mar 2002 23:38:05 -0500


Barry A. Warsaw writes:
 > That's kind of scary Skip!  I.e. I belive the following is /not/
 > guaranteed to work in any version of Python that I'm aware of:
 > 
 >     try:
 > 	raise 'foo'
 >     except 'foo':
 > 	print 'foo caught'
 > 
 > because 'foo' is not (necessarily) 'foo'.

Except of course that it *will* work in Python version dating back to
(I suspect) 1.0; that certainly was being done in 1.2, though I don't
think it was ever recommended practice.

That it worked before string interning was an accident of
implementation: all the 'foo' in your example were in a single code
object, and so were ensured by the compiler to be the same object
(they shared an entry in the constants table).  After string interning
was added, it worked more broadly because interning caused a single
'foo' to be shared.

I'd *love* to see a warning issued everywhere a string exception is
raised or caught by a non-bare except clause!


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Zope Corporation