[New-bugs-announce] [issue2651] Strings passed to KeyError do not round trip

Rick Harris report at bugs.python.org
Thu Apr 17 19:24:41 CEST 2008

New submission from Rick Harris <rconradharris at gmail.com>:

Here is a bug in Python 2.5 which would be nice to fix for Py3k (since
we are already breaking compatibility):

Take a string:
s = "Hello"

Create a KeyError exception with that string:
e = KeyError(s)

Counterintuitively, casting the exception to a string doesn't return the
same string:
str(e) != s

Instead, when KeyError is cast to a string it affixes single-quotes
around the string.

I have create a test which shows that the other built-in exceptions
(except for 3 Unicode Errors which seem to be unusual in that they don't
accept just a string), do indeed round-trip the string unaltered.

This actually caused a bug (in an old version of zope.DocumentTemplate).

I am including the test case I wrote for now; I will begin looking into
a solution shortly and hopefully whip up a patch.

components: Interpreter Core
files: testExceptionStringRoundTrip.py
messages: 65586
nosy: rharris
severity: normal
status: open
title: Strings passed to KeyError do not round trip
type: behavior
versions: Python 2.5
Added file: http://bugs.python.org/file10048/testExceptionStringRoundTrip.py

Tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list