test_exceptions.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. # Python test set -- part 5, built-in exceptions
  2. import os
  3. import sys
  4. import unittest
  5. import pickle, cPickle
  6. from test.test_support import (TESTFN, unlink, run_unittest, captured_stderr,
  7. check_warnings, cpython_only)
  8. from test.test_pep352 import ignore_deprecation_warnings
  9. class BrokenStrException(Exception):
  10. def __str__(self):
  11. raise Exception("str() is broken")
  12. __repr__ = __str__ # Python 2's PyErr_WriteUnraisable() uses repr()
  13. # XXX This is not really enough, each *operation* should be tested!
  14. class ExceptionTests(unittest.TestCase):
  15. def testReload(self):
  16. # Reloading the built-in exceptions module failed prior to Py2.2, while it
  17. # should act the same as reloading built-in sys.
  18. try:
  19. from imp import reload
  20. import exceptions
  21. reload(exceptions)
  22. except ImportError, e:
  23. self.fail("reloading exceptions: %s" % e)
  24. def raise_catch(self, exc, excname):
  25. try:
  26. raise exc, "spam"
  27. except exc, err:
  28. buf1 = str(err)
  29. try:
  30. raise exc("spam")
  31. except exc, err:
  32. buf2 = str(err)
  33. self.assertEqual(buf1, buf2)
  34. self.assertEqual(exc.__name__, excname)
  35. def testRaising(self):
  36. self.raise_catch(AttributeError, "AttributeError")
  37. self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")
  38. self.raise_catch(EOFError, "EOFError")
  39. fp = open(TESTFN, 'w')
  40. fp.close()
  41. fp = open(TESTFN, 'r')
  42. savestdin = sys.stdin
  43. try:
  44. try:
  45. sys.stdin = fp
  46. x = raw_input()
  47. except EOFError:
  48. pass
  49. finally:
  50. sys.stdin = savestdin
  51. fp.close()
  52. unlink(TESTFN)
  53. self.raise_catch(IOError, "IOError")
  54. self.assertRaises(IOError, open, 'this file does not exist', 'r')
  55. self.raise_catch(ImportError, "ImportError")
  56. self.assertRaises(ImportError, __import__, "undefined_module")
  57. self.raise_catch(IndexError, "IndexError")
  58. x = []
  59. self.assertRaises(IndexError, x.__getitem__, 10)
  60. self.raise_catch(KeyError, "KeyError")
  61. x = {}
  62. self.assertRaises(KeyError, x.__getitem__, 'key')
  63. self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt")
  64. self.raise_catch(MemoryError, "MemoryError")
  65. self.raise_catch(NameError, "NameError")
  66. try: x = undefined_variable
  67. except NameError: pass
  68. self.raise_catch(OverflowError, "OverflowError")
  69. x = 1
  70. for dummy in range(128):
  71. x += x # this simply shouldn't blow up
  72. self.raise_catch(RuntimeError, "RuntimeError")
  73. self.raise_catch(SyntaxError, "SyntaxError")
  74. try: exec '/\n'
  75. except SyntaxError: pass
  76. self.raise_catch(IndentationError, "IndentationError")
  77. self.raise_catch(TabError, "TabError")
  78. # can only be tested under -tt, and is the only test for -tt
  79. #try: compile("try:\n\t1.0/0.0\n \t1.0/0.0\nfinally:\n pass\n", '<string>', 'exec')
  80. #except TabError: pass
  81. #else: self.fail("TabError not raised")
  82. self.raise_catch(SystemError, "SystemError")
  83. self.raise_catch(SystemExit, "SystemExit")
  84. self.assertRaises(SystemExit, sys.exit, 0)
  85. self.raise_catch(TypeError, "TypeError")
  86. try: [] + ()
  87. except TypeError: pass
  88. self.raise_catch(ValueError, "ValueError")
  89. self.assertRaises(ValueError, chr, 10000)
  90. self.raise_catch(ZeroDivisionError, "ZeroDivisionError")
  91. try: x = 1 // 0
  92. except ZeroDivisionError: pass
  93. self.raise_catch(Exception, "Exception")
  94. try: x = 1 // 0
  95. except Exception, e: pass
  96. def testSyntaxErrorMessage(self):
  97. # make sure the right exception message is raised for each of
  98. # these code fragments
  99. def ckmsg(src, msg):
  100. try:
  101. compile(src, '<fragment>', 'exec')
  102. except SyntaxError, e:
  103. if e.msg != msg:
  104. self.fail("expected %s, got %s" % (msg, e.msg))
  105. else:
  106. self.fail("failed to get expected SyntaxError")
  107. s = '''while 1:
  108. try:
  109. pass
  110. finally:
  111. continue'''
  112. if not sys.platform.startswith('java'):
  113. ckmsg(s, "'continue' not supported inside 'finally' clause")
  114. s = '''if 1:
  115. try:
  116. continue
  117. except:
  118. pass'''
  119. ckmsg(s, "'continue' not properly in loop")
  120. ckmsg("continue\n", "'continue' not properly in loop")
  121. @cpython_only
  122. def testSettingException(self):
  123. # test that setting an exception at the C level works even if the
  124. # exception object can't be constructed.
  125. class BadException:
  126. def __init__(self_):
  127. raise RuntimeError, "can't instantiate BadException"
  128. def test_capi1():
  129. import _testcapi
  130. try:
  131. _testcapi.raise_exception(BadException, 1)
  132. except TypeError, err:
  133. exc, err, tb = sys.exc_info()
  134. co = tb.tb_frame.f_code
  135. self.assertEqual(co.co_name, "test_capi1")
  136. self.assertTrue(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
  137. else:
  138. self.fail("Expected exception")
  139. def test_capi2():
  140. import _testcapi
  141. try:
  142. _testcapi.raise_exception(BadException, 0)
  143. except RuntimeError, err:
  144. exc, err, tb = sys.exc_info()
  145. co = tb.tb_frame.f_code
  146. self.assertEqual(co.co_name, "__init__")
  147. self.assertTrue(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
  148. co2 = tb.tb_frame.f_back.f_code
  149. self.assertEqual(co2.co_name, "test_capi2")
  150. else:
  151. self.fail("Expected exception")
  152. if not sys.platform.startswith('java'):
  153. test_capi1()
  154. test_capi2()
  155. def test_WindowsError(self):
  156. try:
  157. WindowsError
  158. except NameError:
  159. pass
  160. else:
  161. self.assertEqual(str(WindowsError(1001)),
  162. "1001")
  163. self.assertEqual(str(WindowsError(1001, "message")),
  164. "[Error 1001] message")
  165. self.assertEqual(WindowsError(1001, "message").errno, 22)
  166. self.assertEqual(WindowsError(1001, "message").winerror, 1001)
  167. @ignore_deprecation_warnings
  168. def testAttributes(self):
  169. # test that exception attributes are happy
  170. exceptionList = [
  171. (BaseException, (), {'message' : '', 'args' : ()}),
  172. (BaseException, (1, ), {'message' : 1, 'args' : (1,)}),
  173. (BaseException, ('foo',),
  174. {'message' : 'foo', 'args' : ('foo',)}),
  175. (BaseException, ('foo', 1),
  176. {'message' : '', 'args' : ('foo', 1)}),
  177. (SystemExit, ('foo',),
  178. {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}),
  179. (IOError, ('foo',),
  180. {'message' : 'foo', 'args' : ('foo',), 'filename' : None,
  181. 'errno' : None, 'strerror' : None}),
  182. (IOError, ('foo', 'bar'),
  183. {'message' : '', 'args' : ('foo', 'bar'), 'filename' : None,
  184. 'errno' : 'foo', 'strerror' : 'bar'}),
  185. (IOError, ('foo', 'bar', 'baz'),
  186. {'message' : '', 'args' : ('foo', 'bar'), 'filename' : 'baz',
  187. 'errno' : 'foo', 'strerror' : 'bar'}),
  188. (IOError, ('foo', 'bar', 'baz', 'quux'),
  189. {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}),
  190. (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'),
  191. {'message' : '', 'args' : ('errnoStr', 'strErrorStr'),
  192. 'strerror' : 'strErrorStr', 'errno' : 'errnoStr',
  193. 'filename' : 'filenameStr'}),
  194. (EnvironmentError, (1, 'strErrorStr', 'filenameStr'),
  195. {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1,
  196. 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
  197. (SyntaxError, (), {'message' : '', 'msg' : None, 'text' : None,
  198. 'filename' : None, 'lineno' : None, 'offset' : None,
  199. 'print_file_and_line' : None}),
  200. (SyntaxError, ('msgStr',),
  201. {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None,
  202. 'print_file_and_line' : None, 'msg' : 'msgStr',
  203. 'filename' : None, 'lineno' : None, 'offset' : None}),
  204. (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr',
  205. 'textStr')),
  206. {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr',
  207. 'args' : ('msgStr', ('filenameStr', 'linenoStr',
  208. 'offsetStr', 'textStr')),
  209. 'print_file_and_line' : None, 'msg' : 'msgStr',
  210. 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}),
  211. (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
  212. 'textStr', 'print_file_and_lineStr'),
  213. {'message' : '', 'text' : None,
  214. 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
  215. 'textStr', 'print_file_and_lineStr'),
  216. 'print_file_and_line' : None, 'msg' : 'msgStr',
  217. 'filename' : None, 'lineno' : None, 'offset' : None}),
  218. (UnicodeError, (), {'message' : '', 'args' : (),}),
  219. (UnicodeEncodeError, ('ascii', u'a', 0, 1, 'ordinal not in range'),
  220. {'message' : '', 'args' : ('ascii', u'a', 0, 1,
  221. 'ordinal not in range'),
  222. 'encoding' : 'ascii', 'object' : u'a',
  223. 'start' : 0, 'reason' : 'ordinal not in range'}),
  224. (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'),
  225. {'message' : '', 'args' : ('ascii', '\xff', 0, 1,
  226. 'ordinal not in range'),
  227. 'encoding' : 'ascii', 'object' : '\xff',
  228. 'start' : 0, 'reason' : 'ordinal not in range'}),
  229. (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"),
  230. {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'),
  231. 'object' : u'\u3042', 'reason' : 'ouch',
  232. 'start' : 0, 'end' : 1}),
  233. ]
  234. try:
  235. exceptionList.append(
  236. (WindowsError, (1, 'strErrorStr', 'filenameStr'),
  237. {'message' : '', 'args' : (1, 'strErrorStr'),
  238. 'strerror' : 'strErrorStr', 'winerror' : 1,
  239. 'errno' : 22, 'filename' : 'filenameStr'})
  240. )
  241. except NameError:
  242. pass
  243. for exc, args, expected in exceptionList:
  244. try:
  245. raise exc(*args)
  246. except BaseException, e:
  247. if type(e) is not exc:
  248. raise
  249. # Verify module name
  250. self.assertEqual(type(e).__module__, 'exceptions')
  251. # Verify no ref leaks in Exc_str()
  252. s = str(e)
  253. for checkArgName in expected:
  254. self.assertEqual(repr(getattr(e, checkArgName)),
  255. repr(expected[checkArgName]),
  256. 'exception "%s", attribute "%s"' %
  257. (repr(e), checkArgName))
  258. # test for pickling support
  259. for p in pickle, cPickle:
  260. for protocol in range(p.HIGHEST_PROTOCOL + 1):
  261. new = p.loads(p.dumps(e, protocol))
  262. for checkArgName in expected:
  263. got = repr(getattr(new, checkArgName))
  264. want = repr(expected[checkArgName])
  265. self.assertEqual(got, want,
  266. 'pickled "%r", attribute "%s"' %
  267. (e, checkArgName))
  268. def testDeprecatedMessageAttribute(self):
  269. # Accessing BaseException.message and relying on its value set by
  270. # BaseException.__init__ triggers a deprecation warning.
  271. exc = BaseException("foo")
  272. with check_warnings(("BaseException.message has been deprecated "
  273. "as of Python 2.6", DeprecationWarning)) as w:
  274. self.assertEqual(exc.message, "foo")
  275. self.assertEqual(len(w.warnings), 1)
  276. def testRegularMessageAttribute(self):
  277. # Accessing BaseException.message after explicitly setting a value
  278. # for it does not trigger a deprecation warning.
  279. exc = BaseException("foo")
  280. exc.message = "bar"
  281. with check_warnings(quiet=True) as w:
  282. self.assertEqual(exc.message, "bar")
  283. self.assertEqual(len(w.warnings), 0)
  284. # Deleting the message is supported, too.
  285. del exc.message
  286. with self.assertRaises(AttributeError):
  287. exc.message
  288. @ignore_deprecation_warnings
  289. def testPickleMessageAttribute(self):
  290. # Pickling with message attribute must work, as well.
  291. e = Exception("foo")
  292. f = Exception("foo")
  293. f.message = "bar"
  294. for p in pickle, cPickle:
  295. ep = p.loads(p.dumps(e))
  296. self.assertEqual(ep.message, "foo")
  297. fp = p.loads(p.dumps(f))
  298. self.assertEqual(fp.message, "bar")
  299. @ignore_deprecation_warnings
  300. def testSlicing(self):
  301. # Test that you can slice an exception directly instead of requiring
  302. # going through the 'args' attribute.
  303. args = (1, 2, 3)
  304. exc = BaseException(*args)
  305. self.assertEqual(exc[:], args)
  306. self.assertEqual(exc.args[:], args)
  307. def testKeywordArgs(self):
  308. # test that builtin exception don't take keyword args,
  309. # but user-defined subclasses can if they want
  310. self.assertRaises(TypeError, BaseException, a=1)
  311. class DerivedException(BaseException):
  312. def __init__(self, fancy_arg):
  313. BaseException.__init__(self)
  314. self.fancy_arg = fancy_arg
  315. x = DerivedException(fancy_arg=42)
  316. self.assertEqual(x.fancy_arg, 42)
  317. def testInfiniteRecursion(self):
  318. def f():
  319. return f()
  320. self.assertRaises(RuntimeError, f)
  321. def g():
  322. try:
  323. return g()
  324. except ValueError:
  325. return -1
  326. # The test prints an unraisable recursion error when
  327. # doing "except ValueError", this is because subclass
  328. # checking has recursion checking too.
  329. with captured_stderr():
  330. try:
  331. g()
  332. except RuntimeError:
  333. pass
  334. except:
  335. self.fail("Should have raised KeyError")
  336. else:
  337. self.fail("Should have raised KeyError")
  338. def testUnicodeStrUsage(self):
  339. # Make sure both instances and classes have a str and unicode
  340. # representation.
  341. self.assertTrue(str(Exception))
  342. self.assertTrue(unicode(Exception))
  343. self.assertTrue(str(Exception('a')))
  344. self.assertTrue(unicode(Exception(u'a')))
  345. self.assertTrue(unicode(Exception(u'\xe1')))
  346. def testUnicodeChangeAttributes(self):
  347. # See issue 7309. This was a crasher.
  348. u = UnicodeEncodeError('baz', u'xxxxx', 1, 5, 'foo')
  349. self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: foo")
  350. u.end = 2
  351. self.assertEqual(str(u), "'baz' codec can't encode character u'\\x78' in position 1: foo")
  352. u.end = 5
  353. u.reason = 0x345345345345345345
  354. self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: 965230951443685724997")
  355. u.encoding = 4000
  356. self.assertEqual(str(u), "'4000' codec can't encode characters in position 1-4: 965230951443685724997")
  357. u.start = 1000
  358. self.assertEqual(str(u), "'4000' codec can't encode characters in position 1000-4: 965230951443685724997")
  359. u = UnicodeDecodeError('baz', 'xxxxx', 1, 5, 'foo')
  360. self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: foo")
  361. u.end = 2
  362. self.assertEqual(str(u), "'baz' codec can't decode byte 0x78 in position 1: foo")
  363. u.end = 5
  364. u.reason = 0x345345345345345345
  365. self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: 965230951443685724997")
  366. u.encoding = 4000
  367. self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1-4: 965230951443685724997")
  368. u.start = 1000
  369. self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1000-4: 965230951443685724997")
  370. u = UnicodeTranslateError(u'xxxx', 1, 5, 'foo')
  371. self.assertEqual(str(u), "can't translate characters in position 1-4: foo")
  372. u.end = 2
  373. self.assertEqual(str(u), "can't translate character u'\\x78' in position 1: foo")
  374. u.end = 5
  375. u.reason = 0x345345345345345345
  376. self.assertEqual(str(u), "can't translate characters in position 1-4: 965230951443685724997")
  377. u.start = 1000
  378. self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997")
  379. def test_unicode_errors_no_object(self):
  380. # See issue #21134.
  381. klasses = UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError
  382. for klass in klasses:
  383. self.assertEqual(str(klass.__new__(klass)), "")
  384. def test_badisinstance(self):
  385. # Bug #2542: if issubclass(e, MyException) raises an exception,
  386. # it should be ignored
  387. class Meta(type):
  388. def __subclasscheck__(cls, subclass):
  389. raise ValueError()
  390. class MyException(Exception):
  391. __metaclass__ = Meta
  392. pass
  393. with captured_stderr() as stderr:
  394. try:
  395. raise KeyError()
  396. except MyException, e:
  397. self.fail("exception should not be a MyException")
  398. except KeyError:
  399. pass
  400. except:
  401. self.fail("Should have raised KeyError")
  402. else:
  403. self.fail("Should have raised KeyError")
  404. with captured_stderr() as stderr:
  405. def g():
  406. try:
  407. return g()
  408. except RuntimeError:
  409. return sys.exc_info()
  410. e, v, tb = g()
  411. self.assertTrue(e is RuntimeError, e)
  412. self.assertIn("maximum recursion depth exceeded", str(v))
  413. def test_new_returns_invalid_instance(self):
  414. # See issue #11627.
  415. class MyException(Exception):
  416. def __new__(cls, *args):
  417. return object()
  418. with self.assertRaises(TypeError):
  419. raise MyException
  420. def test_assert_with_tuple_arg(self):
  421. try:
  422. assert False, (3,)
  423. except AssertionError as e:
  424. self.assertEqual(str(e), "(3,)")
  425. def test_bad_exception_clearing(self):
  426. # See issue 16445: use of Py_XDECREF instead of Py_CLEAR in
  427. # BaseException_set_message gave a possible way to segfault the
  428. # interpreter.
  429. class Nasty(str):
  430. def __del__(message):
  431. del e.message
  432. e = ValueError(Nasty("msg"))
  433. e.args = ()
  434. del e.message
  435. # Helper class used by TestSameStrAndUnicodeMsg
  436. class ExcWithOverriddenStr(Exception):
  437. """Subclass of Exception that accepts a keyword 'msg' arg that is
  438. returned by __str__. 'msg' won't be included in self.args"""
  439. def __init__(self, *args, **kwargs):
  440. self.msg = kwargs.pop('msg') # msg should always be present
  441. super(ExcWithOverriddenStr, self).__init__(*args, **kwargs)
  442. def __str__(self):
  443. return self.msg
  444. class TestSameStrAndUnicodeMsg(unittest.TestCase):
  445. """unicode(err) should return the same message of str(err). See #6108"""
  446. def check_same_msg(self, exc, msg):
  447. """Helper function that checks if str(exc) == unicode(exc) == msg"""
  448. self.assertEqual(str(exc), msg)
  449. self.assertEqual(str(exc), unicode(exc))
  450. def test_builtin_exceptions(self):
  451. """Check same msg for built-in exceptions"""
  452. # These exceptions implement a __str__ method that uses the args
  453. # to create a better error message. unicode(e) should return the same
  454. # message.
  455. exceptions = [
  456. SyntaxError('invalid syntax', ('<string>', 1, 3, '2+*3')),
  457. IOError(2, 'No such file or directory'),
  458. KeyError('both should have the same quotes'),
  459. UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1,
  460. 'ordinal not in range(128)'),
  461. UnicodeEncodeError('ascii', u'\u1234', 0, 1,
  462. 'ordinal not in range(128)')
  463. ]
  464. for exception in exceptions:
  465. self.assertEqual(str(exception), unicode(exception))
  466. def test_0_args(self):
  467. """Check same msg for Exception with 0 args"""
  468. # str() and unicode() on an Exception with no args should return an
  469. # empty string
  470. self.check_same_msg(Exception(), '')
  471. def test_0_args_with_overridden___str__(self):
  472. """Check same msg for exceptions with 0 args and overridden __str__"""
  473. # str() and unicode() on an exception with overridden __str__ that
  474. # returns an ascii-only string should return the same string
  475. for msg in ('foo', u'foo'):
  476. self.check_same_msg(ExcWithOverriddenStr(msg=msg), msg)
  477. # if __str__ returns a non-ascii unicode string str() should fail
  478. # but unicode() should return the unicode string
  479. e = ExcWithOverriddenStr(msg=u'f\xf6\xf6') # no args
  480. self.assertRaises(UnicodeEncodeError, str, e)
  481. self.assertEqual(unicode(e), u'f\xf6\xf6')
  482. def test_1_arg(self):
  483. """Check same msg for Exceptions with 1 arg"""
  484. for arg in ('foo', u'foo'):
  485. self.check_same_msg(Exception(arg), arg)
  486. # if __str__ is not overridden and self.args[0] is a non-ascii unicode
  487. # string, str() should try to return str(self.args[0]) and fail.
  488. # unicode() should return unicode(self.args[0]) and succeed.
  489. e = Exception(u'f\xf6\xf6')
  490. self.assertRaises(UnicodeEncodeError, str, e)
  491. self.assertEqual(unicode(e), u'f\xf6\xf6')
  492. def test_1_arg_with_overridden___str__(self):
  493. """Check same msg for exceptions with overridden __str__ and 1 arg"""
  494. # when __str__ is overridden and __unicode__ is not implemented
  495. # unicode(e) returns the same as unicode(e.__str__()).
  496. for msg in ('foo', u'foo'):
  497. self.check_same_msg(ExcWithOverriddenStr('arg', msg=msg), msg)
  498. # if __str__ returns a non-ascii unicode string, str() should fail
  499. # but unicode() should succeed.
  500. e = ExcWithOverriddenStr('arg', msg=u'f\xf6\xf6') # 1 arg
  501. self.assertRaises(UnicodeEncodeError, str, e)
  502. self.assertEqual(unicode(e), u'f\xf6\xf6')
  503. def test_many_args(self):
  504. """Check same msg for Exceptions with many args"""
  505. argslist = [
  506. (3, 'foo'),
  507. (1, u'foo', 'bar'),
  508. (4, u'f\xf6\xf6', u'bar', 'baz')
  509. ]
  510. # both str() and unicode() should return a repr() of the args
  511. for args in argslist:
  512. self.check_same_msg(Exception(*args), repr(args))
  513. def test_many_args_with_overridden___str__(self):
  514. """Check same msg for exceptions with overridden __str__ and many args"""
  515. # if __str__ returns an ascii string / ascii unicode string
  516. # both str() and unicode() should succeed
  517. for msg in ('foo', u'foo'):
  518. e = ExcWithOverriddenStr('arg1', u'arg2', u'f\xf6\xf6', msg=msg)
  519. self.check_same_msg(e, msg)
  520. # if __str__ returns a non-ascii unicode string, str() should fail
  521. # but unicode() should succeed
  522. e = ExcWithOverriddenStr('arg1', u'f\xf6\xf6', u'arg3', # 3 args
  523. msg=u'f\xf6\xf6')
  524. self.assertRaises(UnicodeEncodeError, str, e)
  525. self.assertEqual(unicode(e), u'f\xf6\xf6')
  526. @cpython_only
  527. def test_exception_with_doc(self):
  528. import _testcapi
  529. doc2 = "This is a test docstring."
  530. doc4 = "This is another test docstring."
  531. self.assertRaises(SystemError, _testcapi.make_exception_with_doc,
  532. "error1")
  533. # test basic usage of PyErr_NewException
  534. error1 = _testcapi.make_exception_with_doc("_testcapi.error1")
  535. self.assertIs(type(error1), type)
  536. self.assertTrue(issubclass(error1, Exception))
  537. self.assertIsNone(error1.__doc__)
  538. # test with given docstring
  539. error2 = _testcapi.make_exception_with_doc("_testcapi.error2", doc2)
  540. self.assertEqual(error2.__doc__, doc2)
  541. # test with explicit base (without docstring)
  542. error3 = _testcapi.make_exception_with_doc("_testcapi.error3",
  543. base=error2)
  544. self.assertTrue(issubclass(error3, error2))
  545. # test with explicit base tuple
  546. class C(object):
  547. pass
  548. error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4,
  549. (error3, C))
  550. self.assertTrue(issubclass(error4, error3))
  551. self.assertTrue(issubclass(error4, C))
  552. self.assertEqual(error4.__doc__, doc4)
  553. # test with explicit dictionary
  554. error5 = _testcapi.make_exception_with_doc("_testcapi.error5", "",
  555. error4, {'a': 1})
  556. self.assertTrue(issubclass(error5, error4))
  557. self.assertEqual(error5.a, 1)
  558. self.assertEqual(error5.__doc__, "")
  559. def test_unraisable(self):
  560. # Issue #22836: PyErr_WriteUnraisable() should give sensible reports
  561. class BrokenDel:
  562. def __del__(self):
  563. exc = ValueError("del is broken")
  564. # In Python 3, the following line would be in the report:
  565. raise exc
  566. class BrokenRepr(BrokenDel):
  567. def __repr__(self):
  568. raise AttributeError("repr() is broken")
  569. class BrokenExceptionDel:
  570. def __del__(self):
  571. exc = BrokenStrException()
  572. # In Python 3, the following line would be in the report:
  573. raise exc
  574. for test_class in (BrokenDel, BrokenRepr, BrokenExceptionDel):
  575. obj = test_class()
  576. with captured_stderr() as stderr:
  577. del obj
  578. report = stderr.getvalue()
  579. self.assertRegexpMatches(report, "Exception.* ignored")
  580. if test_class is BrokenRepr:
  581. self.assertIn("<object repr() failed>", report)
  582. else:
  583. self.assertIn("__del__", report)
  584. if test_class is BrokenExceptionDel:
  585. self.assertIn("BrokenStrException", report)
  586. self.assertIn("<exception repr() failed>", report)
  587. else:
  588. self.assertIn("ValueError", report)
  589. self.assertIn("del is broken", report)
  590. self.assertTrue(report.endswith("\n"))
  591. def test_unhandled(self):
  592. # Check for sensible reporting of unhandled exceptions
  593. for exc_type in (ValueError, BrokenStrException):
  594. try:
  595. exc = exc_type("test message")
  596. # The following line is included in the traceback report:
  597. raise exc
  598. except exc_type:
  599. with captured_stderr() as stderr:
  600. sys.__excepthook__(*sys.exc_info())
  601. report = stderr.getvalue()
  602. self.assertIn("test_exceptions.py", report)
  603. self.assertIn("raise exc", report)
  604. self.assertIn(exc_type.__name__, report)
  605. if exc_type is BrokenStrException:
  606. self.assertIn("<exception str() failed>", report)
  607. else:
  608. self.assertIn("test message", report)
  609. self.assertTrue(report.endswith("\n"))
  610. def test_main():
  611. run_unittest(ExceptionTests, TestSameStrAndUnicodeMsg)
  612. if __name__ == '__main__':
  613. test_main()