summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_descr.py26
-rw-r--r--Python/ceval.c5
2 files changed, 30 insertions, 1 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 308ed44cf0..353d0f28b4 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -2974,6 +2974,31 @@ def docdescriptor():
vereq(NewClass.__doc__, 'object=None; type=NewClass')
vereq(NewClass().__doc__, 'object=NewClass instance; type=NewClass')
+def string_exceptions():
+ if verbose:
+ print "Testing string exceptions ..."
+
+ # Ensure builtin strings work OK as exceptions.
+ astring = "An exception string."
+ try:
+ raise astring
+ except astring:
+ pass
+ else:
+ raise TestFailed, "builtin string not usable as exception"
+
+ # Ensure string subclass instances do not.
+ class MyStr(str):
+ pass
+
+ newstring = MyStr("oops -- shouldn't work")
+ try:
+ raise newstring
+ except TypeError:
+ pass
+ except:
+ raise TestFailed, "string subclass allowed as exception"
+
def test_main():
class_docstrings()
lists()
@@ -3039,6 +3064,7 @@ def test_main():
funnynew()
imulbug()
docdescriptor()
+ string_exceptions()
if verbose: print "All OK"
if __name__ == "__main__":
diff --git a/Python/ceval.c b/Python/ceval.c
index a93ceea128..4e08a2af0a 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2743,7 +2743,10 @@ do_raise(PyObject *type, PyObject *value, PyObject *tb)
Py_DECREF(tmp);
}
- if (PyString_Check(type))
+ if (PyString_CheckExact(type))
+ /* Raising builtin string is deprecated but still allowed --
+ * do nothing. Raising an instance of a new-style str
+ * subclass is right out. */
;
else if (PyClass_Check(type))