run-exceptions.test 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. # Test cases for exceptions (compile and run)
  2. [case testException]
  3. from typing import List
  4. def f(x: List[int]) -> None:
  5. g(x)
  6. def g(x: List[int]) -> bool:
  7. x[5] = 2
  8. return True
  9. def r1() -> None:
  10. q1()
  11. def q1() -> None:
  12. raise Exception("test")
  13. def r2() -> None:
  14. q2()
  15. def q2() -> None:
  16. raise Exception
  17. class A:
  18. def __init__(self) -> None:
  19. raise Exception
  20. def hey() -> None:
  21. A()
  22. [file driver.py]
  23. from native import f, r1, r2, hey
  24. import traceback
  25. try:
  26. f([])
  27. except IndexError:
  28. traceback.print_exc()
  29. try:
  30. r1()
  31. except Exception:
  32. traceback.print_exc()
  33. try:
  34. r2()
  35. except Exception:
  36. traceback.print_exc()
  37. try:
  38. hey()
  39. except Exception:
  40. traceback.print_exc()
  41. [out]
  42. Traceback (most recent call last):
  43. File "driver.py", line 4, in <module>
  44. f([])
  45. File "native.py", line 3, in f
  46. g(x)
  47. File "native.py", line 6, in g
  48. x[5] = 2
  49. IndexError: list assignment index out of range
  50. Traceback (most recent call last):
  51. File "driver.py", line 8, in <module>
  52. r1()
  53. File "native.py", line 10, in r1
  54. q1()
  55. File "native.py", line 13, in q1
  56. raise Exception("test")
  57. Exception: test
  58. Traceback (most recent call last):
  59. File "driver.py", line 12, in <module>
  60. r2()
  61. File "native.py", line 16, in r2
  62. q2()
  63. File "native.py", line 19, in q2
  64. raise Exception
  65. Exception
  66. Traceback (most recent call last):
  67. File "driver.py", line 16, in <module>
  68. hey()
  69. File "native.py", line 26, in hey
  70. A()
  71. File "native.py", line 23, in __init__
  72. raise Exception
  73. Exception
  74. [case testTryExcept]
  75. from typing import Any, Iterator
  76. import wrapsys
  77. def g(b: bool) -> None:
  78. try:
  79. if b:
  80. x = [0]
  81. x[1]
  82. else:
  83. raise Exception('hi')
  84. except:
  85. print("caught!")
  86. def r(x: int) -> None:
  87. if x == 0:
  88. [0][1]
  89. elif x == 1:
  90. raise Exception('hi')
  91. elif x == 2:
  92. {1: 1}[0]
  93. elif x == 3:
  94. a = object() # type: Any
  95. a.lol
  96. def f(b: bool) -> None:
  97. try:
  98. r(int(b))
  99. except AttributeError:
  100. print('no')
  101. except:
  102. print(str(wrapsys.exc_info()[1]))
  103. print(str(wrapsys.exc_info()[1]))
  104. def h() -> None:
  105. while True:
  106. try:
  107. raise Exception('gonna break')
  108. except:
  109. print(str(wrapsys.exc_info()[1]))
  110. break
  111. print(str(wrapsys.exc_info()[1]))
  112. def i() -> None:
  113. try:
  114. r(0)
  115. except:
  116. print(type(wrapsys.exc_info()[1]))
  117. raise
  118. def j(n: int) -> None:
  119. try:
  120. r(n)
  121. except (IndexError, KeyError):
  122. print("lookup!")
  123. except AttributeError as e:
  124. print("attr! --", e)
  125. def k() -> None:
  126. try:
  127. r(1)
  128. except:
  129. r(0)
  130. def l() -> None:
  131. try:
  132. r(0)
  133. except IndexError:
  134. try:
  135. r(2)
  136. except KeyError as e:
  137. print("key! --", e)
  138. def m(x: object) -> int:
  139. try:
  140. st = id(x)
  141. except Exception:
  142. return -1
  143. return st + 1
  144. def iter_exception() -> Iterator[str]:
  145. try:
  146. r(0)
  147. except KeyError as e:
  148. yield 'lol'
  149. [file wrapsys.py]
  150. # This is a gross hack around some limitations of the test system/mypyc.
  151. from typing import Any
  152. import sys
  153. def exc_info() -> Any:
  154. return sys.exc_info() # type: ignore
  155. [file driver.py]
  156. import sys, traceback
  157. from native import g, f, h, i, j, k, l, m, iter_exception
  158. from testutil import assertRaises
  159. print("== i ==")
  160. try:
  161. i()
  162. except:
  163. traceback.print_exc(file=sys.stdout)
  164. print("== k ==")
  165. try:
  166. k()
  167. except:
  168. traceback.print_exc(file=sys.stdout)
  169. print("== g ==")
  170. g(True)
  171. g(False)
  172. print("== f ==")
  173. f(True)
  174. f(False)
  175. print("== h ==")
  176. h()
  177. print("== j ==")
  178. j(0)
  179. j(2)
  180. j(3)
  181. try:
  182. j(1)
  183. except:
  184. print("out!")
  185. print("== l ==")
  186. l()
  187. m('lol')
  188. with assertRaises(IndexError):
  189. list(iter_exception())
  190. [out]
  191. == i ==
  192. <class 'IndexError'>
  193. Traceback (most recent call last):
  194. File "driver.py", line 6, in <module>
  195. i()
  196. File "native.py", line 44, in i
  197. r(0)
  198. File "native.py", line 15, in r
  199. [0][1]
  200. IndexError: list index out of range
  201. == k ==
  202. Traceback (most recent call last):
  203. File "native.py", line 59, in k
  204. r(1)
  205. File "native.py", line 17, in r
  206. raise Exception('hi')
  207. Exception: hi
  208. During handling of the above exception, another exception occurred:
  209. Traceback (most recent call last):
  210. File "driver.py", line 12, in <module>
  211. k()
  212. File "native.py", line 61, in k
  213. r(0)
  214. File "native.py", line 15, in r
  215. [0][1]
  216. IndexError: list index out of range
  217. == g ==
  218. caught!
  219. caught!
  220. == f ==
  221. hi
  222. None
  223. list index out of range
  224. None
  225. == h ==
  226. gonna break
  227. None
  228. == j ==
  229. lookup!
  230. lookup!
  231. attr! -- 'object' object has no attribute 'lol'
  232. out!
  233. == l ==
  234. key! -- 0
  235. [case testTryFinally]
  236. from typing import Any
  237. import wrapsys
  238. def a(b1: bool, b2: int) -> None:
  239. try:
  240. if b1:
  241. raise Exception('hi')
  242. finally:
  243. print('finally:', str(wrapsys.exc_info()[1]))
  244. if b2 == 2:
  245. return
  246. if b2 == 1:
  247. raise Exception('again!')
  248. def b(b1: int, b2: int) -> str:
  249. try:
  250. if b1 == 1:
  251. raise Exception('hi')
  252. elif b1 == 2:
  253. [0][1]
  254. elif b1 == 3:
  255. return 'try'
  256. except IndexError:
  257. print('except')
  258. finally:
  259. print('finally:', str(wrapsys.exc_info()[1]))
  260. if b2 == 2:
  261. return 'finally'
  262. if b2 == 1:
  263. raise Exception('again!')
  264. return 'outer'
  265. def c() -> str:
  266. try:
  267. try:
  268. return 'wee'
  269. finally:
  270. print("out a")
  271. finally:
  272. print("out b")
  273. [file wrapsys.py]
  274. # This is a gross hack around some limitations of the test system/mypyc.
  275. from typing import Any
  276. import sys
  277. def exc_info() -> Any:
  278. return sys.exc_info() # type: ignore
  279. [file driver.py]
  280. import traceback
  281. import sys
  282. from native import a, b, c
  283. def run(f):
  284. try:
  285. x = f()
  286. if x:
  287. print("returned:", x)
  288. except Exception as e:
  289. print("caught:", type(e).__name__ + ": " + str(e))
  290. print("== a ==")
  291. for i in range(3):
  292. for b1 in [False, True]:
  293. run(lambda: a(b1, i))
  294. print("== b ==")
  295. for i in range(4):
  296. for j in range(3):
  297. run(lambda: b(i, j))
  298. print("== b ==")
  299. print(c())
  300. [out]
  301. == a ==
  302. finally: None
  303. finally: hi
  304. caught: Exception: hi
  305. finally: None
  306. caught: Exception: again!
  307. finally: hi
  308. caught: Exception: again!
  309. finally: None
  310. finally: hi
  311. == b ==
  312. finally: None
  313. returned: outer
  314. finally: None
  315. caught: Exception: again!
  316. finally: None
  317. returned: finally
  318. finally: hi
  319. caught: Exception: hi
  320. finally: hi
  321. caught: Exception: again!
  322. finally: hi
  323. returned: finally
  324. except
  325. finally: None
  326. returned: outer
  327. except
  328. finally: None
  329. caught: Exception: again!
  330. except
  331. finally: None
  332. returned: finally
  333. finally: None
  334. returned: try
  335. finally: None
  336. caught: Exception: again!
  337. finally: None
  338. returned: finally
  339. == b ==
  340. out a
  341. out b
  342. wee
  343. [case testCustomException]
  344. from typing import List
  345. class ListOutOfBounds(IndexError):
  346. pass
  347. class UserListWarning(UserWarning):
  348. pass
  349. def f(l: List[int], k: int) -> int:
  350. try:
  351. return l[k]
  352. except IndexError:
  353. raise ListOutOfBounds("Ruh-roh from f!")
  354. def g(l: List[int], k: int) -> int:
  355. try:
  356. return f([1,2,3], 3)
  357. except ListOutOfBounds:
  358. raise ListOutOfBounds("Ruh-roh from g!")
  359. def k(l: List[int], k: int) -> int:
  360. try:
  361. return g([1,2,3], 3)
  362. except IndexError:
  363. raise UserListWarning("Ruh-roh from k!")
  364. def h() -> int:
  365. try:
  366. return k([1,2,3], 3)
  367. except UserWarning:
  368. return -1
  369. [file driver.py]
  370. from native import h
  371. assert h() == -1
  372. [case testExceptionAtModuleTopLevel]
  373. from typing import Any
  374. def f(x: int) -> None: pass
  375. y: Any = ''
  376. f(y)
  377. [file driver.py]
  378. import traceback
  379. try:
  380. import native
  381. except TypeError:
  382. traceback.print_exc()
  383. else:
  384. assert False
  385. [out]
  386. Traceback (most recent call last):
  387. File "driver.py", line 3, in <module>
  388. import native
  389. File "native.py", line 6, in <module>
  390. f(y)
  391. TypeError: int object expected; got str