[Python-Dev] New/Old class exception pitfall

Guido van Rossum guido at python.org
Mon Mar 17 23:49:14 CET 2008


On Mon, Mar 17, 2008 at 5:35 PM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> While discussing issue2291, I presented the following argument:
>
>  """
>  Consider the following code:
>
>  class x:
>      pass
>  class y(x):
>      pass
>  try:
>      raise y
>  except y:
>      print "a"
>  except:
>      print "b"
>
>  It prints 'b'.

Really? Under which version exactly? On which platform? I cannot
reproduce this with either 2.4, 2.5 or 2.6 on OS X.

> Now, suppose in preparation for 3.0 transition someone
>  adds "__metaclass__ = type" to the module with that code. The result:
>  it prints 'a'.

Which one would expect regardless of the metaclass, right?

>  Since the difference in behavior is in error handling
>  code, which in my experience is often not thoroughly tested, the bug
>  introduced by a seemingly innocuous move from old to new style classes
>  is likely to trigger in the worst possible moment. (For example a wrong
>  roll-back logic is applied after a failed database commit.)
>  """ http://bugs.python.org/msg63584
>
>  This issue is only partially alleviated by the -3 warning because the warning
>  is not issued unless the error condition raising a new style class not deriving
>  from BaseException is actually tested for.
>
>  It is my understanding that subclass check is skipped for new style classes
>  not derived from BaseException in order to enable the identity check when a
>  string exception is caught.

I have no idea what you are talking about. Can you quote a file,
revision and line number where this is done?

>  With the deprecation of string exceptions, this logic is hard to justify.
>
>  In any case, I believe this issue is either code or documentation bug:
>
>  """
>  Exceptions are identified by class instances. The except clause is
>  selected depending on the class of the instance: it must reference the class of
>  the instance or a base class thereof.
>  """ http://docs.python.org/dev/reference/executionmodel.html#id2
>
>  I don't see anything in the documentation that would suggest that old and new
>  class instances should behave differently.

Me neither.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list