mirror of
https://github.com/python/cpython.git
synced 2025-10-26 11:14:33 +00:00
ceval.c/do_raise(): Tighten the test to disallow raising an instance of
a str subclass. test_descr.py/string_exceptions(): New sub-test. For 2.3 only. Guido doesn't want this backported.
This commit is contained in:
parent
16af557ae9
commit
afb2c80b29
2 changed files with 30 additions and 1 deletions
|
|
@ -2974,6 +2974,31 @@ class NewClass(object):
|
||||||
vereq(NewClass.__doc__, 'object=None; type=NewClass')
|
vereq(NewClass.__doc__, 'object=None; type=NewClass')
|
||||||
vereq(NewClass().__doc__, 'object=NewClass instance; 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():
|
def test_main():
|
||||||
class_docstrings()
|
class_docstrings()
|
||||||
lists()
|
lists()
|
||||||
|
|
@ -3039,6 +3064,7 @@ def test_main():
|
||||||
funnynew()
|
funnynew()
|
||||||
imulbug()
|
imulbug()
|
||||||
docdescriptor()
|
docdescriptor()
|
||||||
|
string_exceptions()
|
||||||
if verbose: print "All OK"
|
if verbose: print "All OK"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -2743,7 +2743,10 @@ do_raise(PyObject *type, PyObject *value, PyObject *tb)
|
||||||
Py_DECREF(tmp);
|
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))
|
else if (PyClass_Check(type))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue