[Python-bugs-list] [ python-Bugs-610610 ] Exception.__str__ error problems

noreply@sourceforge.net noreply@sourceforge.net
Tue, 17 Sep 2002 21:07:14 -0700


Bugs item #610610, was opened at 2002-09-17 11:23
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=610610&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Martijn Pieters (mjpieters)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Exception.__str__ error problems

Initial Comment:
Exception.__str__ seems to contain a bug that causes it
to not handle errors correctly. To reproduce the
problem, type the following lines into a Python
interpreter:

  ex = Exception()
  ex.args = None
  str(ex)
  d = {}

Note that we set Exception.args to None, while the
__str__ method expects args to be a sequence. The
interpreter, however, prints "'None'", and no error is
shown. But when trying to excecute the perfectly
harmless 'd = {}' assignment, an error, with *no
traceback* is printed an 'd' is never defined.

I have not been able to recreate this bug in a simple
script yet, though it was found through a large Zope
application where a client had created a custom
Exception with self.args set to None. The above
behaviour has been observed in Python versions 2.1.3,
2.2.1 and current CVS, but not in Python 1.5.2.

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-09-18 00:07

Message:
Logged In: YES 
user_id=6380

Thanks, both! Fixed in the CVS trunk, labeled as a backport
candidate.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-09-17 18:38

Message:
Logged In: YES 
user_id=33168

The problem is in Python/exceptions.c at the switch
statement, line 278 or so.  If PySequence_Size() returns -1
(since args is None), the default case is handled, but an
exception was set.  The string is returned and printed as
'None', but an exception still exists.  The next instruction
(d = {}) show's the exception.

I would tend to say it's best to ignore the error (treat as
empty args).  If so, adding case -1: which calls
PyErr_Clear() and then falls through to case 0: seems to
work for me.

Note:  there are 2 other simlar swiches in
Python/exceptions.c.  SystemExit__init__ &
EnvironmentError__init__

Guido, assign back to me if you want me to fix it.  And let
me know how this problem should be handled.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=610610&group_id=5470