test_sys_settrace.py 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819
  1. # Testing the line trace facility.
  2. from test import support
  3. import unittest
  4. from unittest.mock import MagicMock
  5. import sys
  6. import difflib
  7. import gc
  8. from functools import wraps
  9. import asyncio
  10. from test.support import import_helper
  11. support.requires_working_socket(module=True)
  12. class tracecontext:
  13. """Context manager that traces its enter and exit."""
  14. def __init__(self, output, value):
  15. self.output = output
  16. self.value = value
  17. def __enter__(self):
  18. self.output.append(self.value)
  19. def __exit__(self, *exc_info):
  20. self.output.append(-self.value)
  21. class asynctracecontext:
  22. """Asynchronous context manager that traces its aenter and aexit."""
  23. def __init__(self, output, value):
  24. self.output = output
  25. self.value = value
  26. async def __aenter__(self):
  27. self.output.append(self.value)
  28. async def __aexit__(self, *exc_info):
  29. self.output.append(-self.value)
  30. async def asynciter(iterable):
  31. """Convert an iterable to an asynchronous iterator."""
  32. for x in iterable:
  33. yield x
  34. # A very basic example. If this fails, we're in deep trouble.
  35. def basic():
  36. return 1
  37. basic.events = [(0, 'call'),
  38. (1, 'line'),
  39. (1, 'return')]
  40. # Many of the tests below are tricky because they involve pass statements.
  41. # If there is implicit control flow around a pass statement (in an except
  42. # clause or else clause) under what conditions do you set a line number
  43. # following that clause?
  44. # Some constructs like "while 0:", "if 0:" or "if 1:...else:..." could be optimized
  45. # away. Make sure that those lines aren't skipped.
  46. def arigo_example0():
  47. x = 1
  48. del x
  49. while 0:
  50. pass
  51. x = 1
  52. arigo_example0.events = [(0, 'call'),
  53. (1, 'line'),
  54. (2, 'line'),
  55. (3, 'line'),
  56. (5, 'line'),
  57. (5, 'return')]
  58. def arigo_example1():
  59. x = 1
  60. del x
  61. if 0:
  62. pass
  63. x = 1
  64. arigo_example1.events = [(0, 'call'),
  65. (1, 'line'),
  66. (2, 'line'),
  67. (3, 'line'),
  68. (5, 'line'),
  69. (5, 'return')]
  70. def arigo_example2():
  71. x = 1
  72. del x
  73. if 1:
  74. x = 1
  75. else:
  76. pass
  77. return None
  78. arigo_example2.events = [(0, 'call'),
  79. (1, 'line'),
  80. (2, 'line'),
  81. (3, 'line'),
  82. (4, 'line'),
  83. (7, 'line'),
  84. (7, 'return')]
  85. # check that lines consisting of just one instruction get traced:
  86. def one_instr_line():
  87. x = 1
  88. del x
  89. x = 1
  90. one_instr_line.events = [(0, 'call'),
  91. (1, 'line'),
  92. (2, 'line'),
  93. (3, 'line'),
  94. (3, 'return')]
  95. def no_pop_tops(): # 0
  96. x = 1 # 1
  97. for a in range(2): # 2
  98. if a: # 3
  99. x = 1 # 4
  100. else: # 5
  101. x = 1 # 6
  102. no_pop_tops.events = [(0, 'call'),
  103. (1, 'line'),
  104. (2, 'line'),
  105. (3, 'line'),
  106. (6, 'line'),
  107. (2, 'line'),
  108. (3, 'line'),
  109. (4, 'line'),
  110. (2, 'line'),
  111. (2, 'return')]
  112. def no_pop_blocks():
  113. y = 1
  114. while not y:
  115. bla
  116. x = 1
  117. no_pop_blocks.events = [(0, 'call'),
  118. (1, 'line'),
  119. (2, 'line'),
  120. (4, 'line'),
  121. (4, 'return')]
  122. def called(): # line -3
  123. x = 1
  124. def call(): # line 0
  125. called()
  126. call.events = [(0, 'call'),
  127. (1, 'line'),
  128. (-3, 'call'),
  129. (-2, 'line'),
  130. (-2, 'return'),
  131. (1, 'return')]
  132. def raises():
  133. raise Exception
  134. def test_raise():
  135. try:
  136. raises()
  137. except Exception:
  138. pass
  139. test_raise.events = [(0, 'call'),
  140. (1, 'line'),
  141. (2, 'line'),
  142. (-3, 'call'),
  143. (-2, 'line'),
  144. (-2, 'exception'),
  145. (-2, 'return'),
  146. (2, 'exception'),
  147. (3, 'line'),
  148. (4, 'line'),
  149. (4, 'return')]
  150. def _settrace_and_return(tracefunc):
  151. sys.settrace(tracefunc)
  152. sys._getframe().f_back.f_trace = tracefunc
  153. def settrace_and_return(tracefunc):
  154. _settrace_and_return(tracefunc)
  155. settrace_and_return.events = [(1, 'return')]
  156. def _settrace_and_raise(tracefunc):
  157. sys.settrace(tracefunc)
  158. sys._getframe().f_back.f_trace = tracefunc
  159. raise RuntimeError
  160. def settrace_and_raise(tracefunc):
  161. try:
  162. _settrace_and_raise(tracefunc)
  163. except RuntimeError:
  164. pass
  165. settrace_and_raise.events = [(2, 'exception'),
  166. (3, 'line'),
  167. (4, 'line'),
  168. (4, 'return')]
  169. # implicit return example
  170. # This test is interesting because of the else: pass
  171. # part of the code. The code generate for the true
  172. # part of the if contains a jump past the else branch.
  173. # The compiler then generates an implicit "return None"
  174. # Internally, the compiler visits the pass statement
  175. # and stores its line number for use on the next instruction.
  176. # The next instruction is the implicit return None.
  177. def ireturn_example():
  178. a = 5
  179. b = 5
  180. if a == b:
  181. b = a+1
  182. else:
  183. pass
  184. ireturn_example.events = [(0, 'call'),
  185. (1, 'line'),
  186. (2, 'line'),
  187. (3, 'line'),
  188. (4, 'line'),
  189. (4, 'return')]
  190. # Tight loop with while(1) example (SF #765624)
  191. def tightloop_example():
  192. items = range(0, 3)
  193. try:
  194. i = 0
  195. while 1:
  196. b = items[i]; i+=1
  197. except IndexError:
  198. pass
  199. tightloop_example.events = [(0, 'call'),
  200. (1, 'line'),
  201. (2, 'line'),
  202. (3, 'line'),
  203. (4, 'line'),
  204. (5, 'line'),
  205. (4, 'line'),
  206. (5, 'line'),
  207. (4, 'line'),
  208. (5, 'line'),
  209. (4, 'line'),
  210. (5, 'line'),
  211. (5, 'exception'),
  212. (6, 'line'),
  213. (7, 'line'),
  214. (7, 'return')]
  215. def tighterloop_example():
  216. items = range(1, 4)
  217. try:
  218. i = 0
  219. while 1: i = items[i]
  220. except IndexError:
  221. pass
  222. tighterloop_example.events = [(0, 'call'),
  223. (1, 'line'),
  224. (2, 'line'),
  225. (3, 'line'),
  226. (4, 'line'),
  227. (4, 'line'),
  228. (4, 'line'),
  229. (4, 'line'),
  230. (4, 'exception'),
  231. (5, 'line'),
  232. (6, 'line'),
  233. (6, 'return')]
  234. def generator_function():
  235. try:
  236. yield True
  237. "continued"
  238. finally:
  239. "finally"
  240. def generator_example():
  241. # any() will leave the generator before its end
  242. x = any(generator_function())
  243. # the following lines were not traced
  244. for x in range(10):
  245. y = x
  246. generator_example.events = ([(0, 'call'),
  247. (2, 'line'),
  248. (-6, 'call'),
  249. (-5, 'line'),
  250. (-4, 'line'),
  251. (-4, 'return'),
  252. (-4, 'call'),
  253. (-4, 'exception'),
  254. (-1, 'line'),
  255. (-1, 'return')] +
  256. [(5, 'line'), (6, 'line')] * 10 +
  257. [(5, 'line'), (5, 'return')])
  258. class Tracer:
  259. def __init__(self, trace_line_events=None, trace_opcode_events=None):
  260. self.trace_line_events = trace_line_events
  261. self.trace_opcode_events = trace_opcode_events
  262. self.events = []
  263. def _reconfigure_frame(self, frame):
  264. if self.trace_line_events is not None:
  265. frame.f_trace_lines = self.trace_line_events
  266. if self.trace_opcode_events is not None:
  267. frame.f_trace_opcodes = self.trace_opcode_events
  268. def trace(self, frame, event, arg):
  269. self._reconfigure_frame(frame)
  270. self.events.append((frame.f_lineno, event))
  271. return self.trace
  272. def traceWithGenexp(self, frame, event, arg):
  273. self._reconfigure_frame(frame)
  274. (o for o in [1])
  275. self.events.append((frame.f_lineno, event))
  276. return self.trace
  277. class TraceTestCase(unittest.TestCase):
  278. # Disable gc collection when tracing, otherwise the
  279. # deallocators may be traced as well.
  280. def setUp(self):
  281. self.using_gc = gc.isenabled()
  282. gc.disable()
  283. self.addCleanup(sys.settrace, sys.gettrace())
  284. def tearDown(self):
  285. if self.using_gc:
  286. gc.enable()
  287. @staticmethod
  288. def make_tracer():
  289. """Helper to allow test subclasses to configure tracers differently"""
  290. return Tracer()
  291. def compare_events(self, line_offset, events, expected_events):
  292. events = [(l - line_offset, e) for (l, e) in events]
  293. if events != expected_events:
  294. self.fail(
  295. "events did not match expectation:\n" +
  296. "\n".join(difflib.ndiff([str(x) for x in expected_events],
  297. [str(x) for x in events])))
  298. def run_and_compare(self, func, events):
  299. tracer = self.make_tracer()
  300. sys.settrace(tracer.trace)
  301. func()
  302. sys.settrace(None)
  303. self.compare_events(func.__code__.co_firstlineno,
  304. tracer.events, events)
  305. def run_test(self, func):
  306. self.run_and_compare(func, func.events)
  307. def run_test2(self, func):
  308. tracer = self.make_tracer()
  309. func(tracer.trace)
  310. sys.settrace(None)
  311. self.compare_events(func.__code__.co_firstlineno,
  312. tracer.events, func.events)
  313. def test_set_and_retrieve_none(self):
  314. sys.settrace(None)
  315. assert sys.gettrace() is None
  316. def test_set_and_retrieve_func(self):
  317. def fn(*args):
  318. pass
  319. sys.settrace(fn)
  320. try:
  321. assert sys.gettrace() is fn
  322. finally:
  323. sys.settrace(None)
  324. def test_01_basic(self):
  325. self.run_test(basic)
  326. def test_02_arigo0(self):
  327. self.run_test(arigo_example0)
  328. def test_02_arigo1(self):
  329. self.run_test(arigo_example1)
  330. def test_02_arigo2(self):
  331. self.run_test(arigo_example2)
  332. def test_03_one_instr(self):
  333. self.run_test(one_instr_line)
  334. def test_04_no_pop_blocks(self):
  335. self.run_test(no_pop_blocks)
  336. def test_05_no_pop_tops(self):
  337. self.run_test(no_pop_tops)
  338. def test_06_call(self):
  339. self.run_test(call)
  340. def test_07_raise(self):
  341. self.run_test(test_raise)
  342. def test_08_settrace_and_return(self):
  343. self.run_test2(settrace_and_return)
  344. def test_09_settrace_and_raise(self):
  345. self.run_test2(settrace_and_raise)
  346. def test_10_ireturn(self):
  347. self.run_test(ireturn_example)
  348. def test_11_tightloop(self):
  349. self.run_test(tightloop_example)
  350. def test_12_tighterloop(self):
  351. self.run_test(tighterloop_example)
  352. def test_13_genexp(self):
  353. self.run_test(generator_example)
  354. # issue1265: if the trace function contains a generator,
  355. # and if the traced function contains another generator
  356. # that is not completely exhausted, the trace stopped.
  357. # Worse: the 'finally' clause was not invoked.
  358. tracer = self.make_tracer()
  359. sys.settrace(tracer.traceWithGenexp)
  360. generator_example()
  361. sys.settrace(None)
  362. self.compare_events(generator_example.__code__.co_firstlineno,
  363. tracer.events, generator_example.events)
  364. def test_14_onliner_if(self):
  365. def onliners():
  366. if True: x=False
  367. else: x=True
  368. return 0
  369. self.run_and_compare(
  370. onliners,
  371. [(0, 'call'),
  372. (1, 'line'),
  373. (3, 'line'),
  374. (3, 'return')])
  375. def test_15_loops(self):
  376. # issue1750076: "while" expression is skipped by debugger
  377. def for_example():
  378. for x in range(2):
  379. pass
  380. self.run_and_compare(
  381. for_example,
  382. [(0, 'call'),
  383. (1, 'line'),
  384. (2, 'line'),
  385. (1, 'line'),
  386. (2, 'line'),
  387. (1, 'line'),
  388. (1, 'return')])
  389. def while_example():
  390. # While expression should be traced on every loop
  391. x = 2
  392. while x > 0:
  393. x -= 1
  394. self.run_and_compare(
  395. while_example,
  396. [(0, 'call'),
  397. (2, 'line'),
  398. (3, 'line'),
  399. (4, 'line'),
  400. (3, 'line'),
  401. (4, 'line'),
  402. (3, 'line'),
  403. (3, 'return')])
  404. def test_16_blank_lines(self):
  405. namespace = {}
  406. exec("def f():\n" + "\n" * 256 + " pass", namespace)
  407. self.run_and_compare(
  408. namespace["f"],
  409. [(0, 'call'),
  410. (257, 'line'),
  411. (257, 'return')])
  412. def test_17_none_f_trace(self):
  413. # Issue 20041: fix TypeError when f_trace is set to None.
  414. def func():
  415. sys._getframe().f_trace = None
  416. lineno = 2
  417. self.run_and_compare(func,
  418. [(0, 'call'),
  419. (1, 'line')])
  420. def test_18_except_with_name(self):
  421. def func():
  422. try:
  423. try:
  424. raise Exception
  425. except Exception as e:
  426. raise
  427. x = "Something"
  428. y = "Something"
  429. except Exception:
  430. pass
  431. self.run_and_compare(func,
  432. [(0, 'call'),
  433. (1, 'line'),
  434. (2, 'line'),
  435. (3, 'line'),
  436. (3, 'exception'),
  437. (4, 'line'),
  438. (5, 'line'),
  439. (8, 'line'),
  440. (9, 'line'),
  441. (9, 'return')])
  442. def test_19_except_with_finally(self):
  443. def func():
  444. try:
  445. try:
  446. raise Exception
  447. finally:
  448. y = "Something"
  449. except Exception:
  450. b = 23
  451. self.run_and_compare(func,
  452. [(0, 'call'),
  453. (1, 'line'),
  454. (2, 'line'),
  455. (3, 'line'),
  456. (3, 'exception'),
  457. (5, 'line'),
  458. (6, 'line'),
  459. (7, 'line'),
  460. (7, 'return')])
  461. def test_20_async_for_loop(self):
  462. class AsyncIteratorWrapper:
  463. def __init__(self, obj):
  464. self._it = iter(obj)
  465. def __aiter__(self):
  466. return self
  467. async def __anext__(self):
  468. try:
  469. return next(self._it)
  470. except StopIteration:
  471. raise StopAsyncIteration
  472. async def doit_async():
  473. async for letter in AsyncIteratorWrapper("abc"):
  474. x = letter
  475. y = 42
  476. def run(tracer):
  477. x = doit_async()
  478. try:
  479. sys.settrace(tracer)
  480. x.send(None)
  481. finally:
  482. sys.settrace(None)
  483. tracer = self.make_tracer()
  484. events = [
  485. (0, 'call'),
  486. (1, 'line'),
  487. (-12, 'call'),
  488. (-11, 'line'),
  489. (-11, 'return'),
  490. (-9, 'call'),
  491. (-8, 'line'),
  492. (-8, 'return'),
  493. (-6, 'call'),
  494. (-5, 'line'),
  495. (-4, 'line'),
  496. (-4, 'return'),
  497. (1, 'exception'),
  498. (2, 'line'),
  499. (1, 'line'),
  500. (-6, 'call'),
  501. (-5, 'line'),
  502. (-4, 'line'),
  503. (-4, 'return'),
  504. (1, 'exception'),
  505. (2, 'line'),
  506. (1, 'line'),
  507. (-6, 'call'),
  508. (-5, 'line'),
  509. (-4, 'line'),
  510. (-4, 'return'),
  511. (1, 'exception'),
  512. (2, 'line'),
  513. (1, 'line'),
  514. (-6, 'call'),
  515. (-5, 'line'),
  516. (-4, 'line'),
  517. (-4, 'exception'),
  518. (-3, 'line'),
  519. (-2, 'line'),
  520. (-2, 'exception'),
  521. (-2, 'return'),
  522. (1, 'exception'),
  523. (3, 'line'),
  524. (3, 'return')]
  525. try:
  526. run(tracer.trace)
  527. except Exception:
  528. pass
  529. self.compare_events(doit_async.__code__.co_firstlineno,
  530. tracer.events, events)
  531. def test_async_for_backwards_jump_has_no_line(self):
  532. async def arange(n):
  533. for i in range(n):
  534. yield i
  535. async def f():
  536. async for i in arange(3):
  537. if i > 100:
  538. break # should never be traced
  539. tracer = self.make_tracer()
  540. coro = f()
  541. try:
  542. sys.settrace(tracer.trace)
  543. coro.send(None)
  544. except Exception:
  545. pass
  546. finally:
  547. sys.settrace(None)
  548. events = [
  549. (0, 'call'),
  550. (1, 'line'),
  551. (-3, 'call'),
  552. (-2, 'line'),
  553. (-1, 'line'),
  554. (-1, 'return'),
  555. (1, 'exception'),
  556. (2, 'line'),
  557. (1, 'line'),
  558. (-1, 'call'),
  559. (-2, 'line'),
  560. (-1, 'line'),
  561. (-1, 'return'),
  562. (1, 'exception'),
  563. (2, 'line'),
  564. (1, 'line'),
  565. (-1, 'call'),
  566. (-2, 'line'),
  567. (-1, 'line'),
  568. (-1, 'return'),
  569. (1, 'exception'),
  570. (2, 'line'),
  571. (1, 'line'),
  572. (-1, 'call'),
  573. (-2, 'line'),
  574. (-2, 'return'),
  575. (1, 'exception'),
  576. (1, 'return'),
  577. ]
  578. self.compare_events(f.__code__.co_firstlineno,
  579. tracer.events, events)
  580. def test_21_repeated_pass(self):
  581. def func():
  582. pass
  583. pass
  584. self.run_and_compare(func,
  585. [(0, 'call'),
  586. (1, 'line'),
  587. (2, 'line'),
  588. (2, 'return')])
  589. def test_loop_in_try_except(self):
  590. # https://bugs.python.org/issue41670
  591. def func():
  592. try:
  593. for i in []: pass
  594. return 1
  595. except:
  596. return 2
  597. self.run_and_compare(func,
  598. [(0, 'call'),
  599. (1, 'line'),
  600. (2, 'line'),
  601. (3, 'line'),
  602. (3, 'return')])
  603. def test_try_except_no_exception(self):
  604. def func():
  605. try:
  606. 2
  607. except:
  608. 4
  609. else:
  610. 6
  611. if False:
  612. 8
  613. else:
  614. 10
  615. if func.__name__ == 'Fred':
  616. 12
  617. finally:
  618. 14
  619. self.run_and_compare(func,
  620. [(0, 'call'),
  621. (1, 'line'),
  622. (2, 'line'),
  623. (6, 'line'),
  624. (7, 'line'),
  625. (10, 'line'),
  626. (11, 'line'),
  627. (14, 'line'),
  628. (14, 'return')])
  629. def test_try_exception_in_else(self):
  630. def func():
  631. try:
  632. try:
  633. 3
  634. except:
  635. 5
  636. else:
  637. 7
  638. raise Exception
  639. finally:
  640. 10
  641. except:
  642. 12
  643. finally:
  644. 14
  645. self.run_and_compare(func,
  646. [(0, 'call'),
  647. (1, 'line'),
  648. (2, 'line'),
  649. (3, 'line'),
  650. (7, 'line'),
  651. (8, 'line'),
  652. (8, 'exception'),
  653. (10, 'line'),
  654. (11, 'line'),
  655. (12, 'line'),
  656. (14, 'line'),
  657. (14, 'return')])
  658. def test_nested_loops(self):
  659. def func():
  660. for i in range(2):
  661. for j in range(2):
  662. a = i + j
  663. return a == 1
  664. self.run_and_compare(func,
  665. [(0, 'call'),
  666. (1, 'line'),
  667. (2, 'line'),
  668. (3, 'line'),
  669. (2, 'line'),
  670. (3, 'line'),
  671. (2, 'line'),
  672. (1, 'line'),
  673. (2, 'line'),
  674. (3, 'line'),
  675. (2, 'line'),
  676. (3, 'line'),
  677. (2, 'line'),
  678. (1, 'line'),
  679. (4, 'line'),
  680. (4, 'return')])
  681. def test_if_break(self):
  682. def func():
  683. seq = [1, 0]
  684. while seq:
  685. n = seq.pop()
  686. if n:
  687. break # line 5
  688. else:
  689. n = 99
  690. return n # line 8
  691. self.run_and_compare(func,
  692. [(0, 'call'),
  693. (1, 'line'),
  694. (2, 'line'),
  695. (3, 'line'),
  696. (4, 'line'),
  697. (2, 'line'),
  698. (3, 'line'),
  699. (4, 'line'),
  700. (5, 'line'),
  701. (8, 'line'),
  702. (8, 'return')])
  703. def test_break_through_finally(self):
  704. def func():
  705. a, c, d, i = 1, 1, 1, 99
  706. try:
  707. for i in range(3):
  708. try:
  709. a = 5
  710. if i > 0:
  711. break # line 7
  712. a = 8
  713. finally:
  714. c = 10
  715. except:
  716. d = 12 # line 12
  717. assert a == 5 and c == 10 and d == 1 # line 13
  718. self.run_and_compare(func,
  719. [(0, 'call'),
  720. (1, 'line'),
  721. (2, 'line'),
  722. (3, 'line'),
  723. (4, 'line'),
  724. (5, 'line'),
  725. (6, 'line'),
  726. (8, 'line'),
  727. (10, 'line'),
  728. (3, 'line'),
  729. (4, 'line'),
  730. (5, 'line'),
  731. (6, 'line'),
  732. (7, 'line'),
  733. (10, 'line'),
  734. (13, 'line'),
  735. (13, 'return')])
  736. def test_continue_through_finally(self):
  737. def func():
  738. a, b, c, d, i = 1, 1, 1, 1, 99
  739. try:
  740. for i in range(2):
  741. try:
  742. a = 5
  743. if i > 0:
  744. continue # line 7
  745. b = 8
  746. finally:
  747. c = 10
  748. except:
  749. d = 12 # line 12
  750. assert (a, b, c, d) == (5, 8, 10, 1) # line 13
  751. self.run_and_compare(func,
  752. [(0, 'call'),
  753. (1, 'line'),
  754. (2, 'line'),
  755. (3, 'line'),
  756. (4, 'line'),
  757. (5, 'line'),
  758. (6, 'line'),
  759. (8, 'line'),
  760. (10, 'line'),
  761. (3, 'line'),
  762. (4, 'line'),
  763. (5, 'line'),
  764. (6, 'line'),
  765. (7, 'line'),
  766. (10, 'line'),
  767. (3, 'line'),
  768. (13, 'line'),
  769. (13, 'return')])
  770. def test_return_through_finally(self):
  771. def func():
  772. try:
  773. return 2
  774. finally:
  775. 4
  776. self.run_and_compare(func,
  777. [(0, 'call'),
  778. (1, 'line'),
  779. (2, 'line'),
  780. (4, 'line'),
  781. (4, 'return')])
  782. def test_try_except_with_wrong_type(self):
  783. def func():
  784. try:
  785. 2/0
  786. except IndexError:
  787. 4
  788. finally:
  789. return 6
  790. self.run_and_compare(func,
  791. [(0, 'call'),
  792. (1, 'line'),
  793. (2, 'line'),
  794. (2, 'exception'),
  795. (3, 'line'),
  796. (6, 'line'),
  797. (6, 'return')])
  798. def test_break_to_continue1(self):
  799. def func():
  800. TRUE = 1
  801. x = [1]
  802. while x:
  803. x.pop()
  804. while TRUE:
  805. break
  806. continue
  807. self.run_and_compare(func,
  808. [(0, 'call'),
  809. (1, 'line'),
  810. (2, 'line'),
  811. (3, 'line'),
  812. (4, 'line'),
  813. (5, 'line'),
  814. (6, 'line'),
  815. (7, 'line'),
  816. (3, 'line'),
  817. (3, 'return')])
  818. def test_break_to_continue2(self):
  819. def func():
  820. TRUE = 1
  821. x = [1]
  822. while x:
  823. x.pop()
  824. while TRUE:
  825. break
  826. else:
  827. continue
  828. self.run_and_compare(func,
  829. [(0, 'call'),
  830. (1, 'line'),
  831. (2, 'line'),
  832. (3, 'line'),
  833. (4, 'line'),
  834. (5, 'line'),
  835. (6, 'line'),
  836. (3, 'line'),
  837. (3, 'return')])
  838. def test_break_to_break(self):
  839. def func():
  840. TRUE = 1
  841. while TRUE:
  842. while TRUE:
  843. break
  844. break
  845. self.run_and_compare(func,
  846. [(0, 'call'),
  847. (1, 'line'),
  848. (2, 'line'),
  849. (3, 'line'),
  850. (4, 'line'),
  851. (5, 'line'),
  852. (5, 'return')])
  853. def test_nested_ifs(self):
  854. def func():
  855. a = b = 1
  856. if a == 1:
  857. if b == 1:
  858. x = 4
  859. else:
  860. y = 6
  861. else:
  862. z = 8
  863. self.run_and_compare(func,
  864. [(0, 'call'),
  865. (1, 'line'),
  866. (2, 'line'),
  867. (3, 'line'),
  868. (4, 'line'),
  869. (4, 'return')])
  870. def test_nested_ifs_with_and(self):
  871. def func():
  872. if A:
  873. if B:
  874. if C:
  875. if D:
  876. return False
  877. else:
  878. return False
  879. elif E and F:
  880. return True
  881. A = B = True
  882. C = False
  883. self.run_and_compare(func,
  884. [(0, 'call'),
  885. (1, 'line'),
  886. (2, 'line'),
  887. (3, 'line'),
  888. (3, 'return')])
  889. def test_nested_try_if(self):
  890. def func():
  891. x = "hello"
  892. try:
  893. 3/0
  894. except ZeroDivisionError:
  895. if x == 'raise':
  896. raise ValueError() # line 6
  897. f = 7
  898. self.run_and_compare(func,
  899. [(0, 'call'),
  900. (1, 'line'),
  901. (2, 'line'),
  902. (3, 'line'),
  903. (3, 'exception'),
  904. (4, 'line'),
  905. (5, 'line'),
  906. (7, 'line'),
  907. (7, 'return')])
  908. def test_if_false_in_with(self):
  909. class C:
  910. def __enter__(self):
  911. return self
  912. def __exit__(*args):
  913. pass
  914. def func():
  915. with C():
  916. if False:
  917. pass
  918. self.run_and_compare(func,
  919. [(0, 'call'),
  920. (1, 'line'),
  921. (-5, 'call'),
  922. (-4, 'line'),
  923. (-4, 'return'),
  924. (2, 'line'),
  925. (1, 'line'),
  926. (-3, 'call'),
  927. (-2, 'line'),
  928. (-2, 'return'),
  929. (1, 'return')])
  930. def test_if_false_in_try_except(self):
  931. def func():
  932. try:
  933. if False:
  934. pass
  935. except Exception:
  936. X
  937. self.run_and_compare(func,
  938. [(0, 'call'),
  939. (1, 'line'),
  940. (2, 'line'),
  941. (2, 'return')])
  942. def test_implicit_return_in_class(self):
  943. def func():
  944. class A:
  945. if 3 < 9:
  946. a = 1
  947. else:
  948. a = 2
  949. self.run_and_compare(func,
  950. [(0, 'call'),
  951. (1, 'line'),
  952. (1, 'call'),
  953. (1, 'line'),
  954. (2, 'line'),
  955. (3, 'line'),
  956. (3, 'return'),
  957. (1, 'return')])
  958. def test_try_in_try(self):
  959. def func():
  960. try:
  961. try:
  962. pass
  963. except Exception as ex:
  964. pass
  965. except Exception:
  966. pass
  967. self.run_and_compare(func,
  968. [(0, 'call'),
  969. (1, 'line'),
  970. (2, 'line'),
  971. (3, 'line'),
  972. (3, 'return')])
  973. def test_try_in_try_with_exception(self):
  974. def func():
  975. try:
  976. try:
  977. raise TypeError
  978. except ValueError as ex:
  979. 5
  980. except TypeError:
  981. 7
  982. self.run_and_compare(func,
  983. [(0, 'call'),
  984. (1, 'line'),
  985. (2, 'line'),
  986. (3, 'line'),
  987. (3, 'exception'),
  988. (4, 'line'),
  989. (6, 'line'),
  990. (7, 'line'),
  991. (7, 'return')])
  992. def func():
  993. try:
  994. try:
  995. raise ValueError
  996. except ValueError as ex:
  997. 5
  998. except TypeError:
  999. 7
  1000. self.run_and_compare(func,
  1001. [(0, 'call'),
  1002. (1, 'line'),
  1003. (2, 'line'),
  1004. (3, 'line'),
  1005. (3, 'exception'),
  1006. (4, 'line'),
  1007. (5, 'line'),
  1008. (5, 'return')])
  1009. def test_if_in_if_in_if(self):
  1010. def func(a=0, p=1, z=1):
  1011. if p:
  1012. if a:
  1013. if z:
  1014. pass
  1015. else:
  1016. pass
  1017. else:
  1018. pass
  1019. self.run_and_compare(func,
  1020. [(0, 'call'),
  1021. (1, 'line'),
  1022. (2, 'line'),
  1023. (2, 'return')])
  1024. def test_early_exit_with(self):
  1025. class C:
  1026. def __enter__(self):
  1027. return self
  1028. def __exit__(*args):
  1029. pass
  1030. def func_break():
  1031. for i in (1,2):
  1032. with C():
  1033. break
  1034. pass
  1035. def func_return():
  1036. with C():
  1037. return
  1038. self.run_and_compare(func_break,
  1039. [(0, 'call'),
  1040. (1, 'line'),
  1041. (2, 'line'),
  1042. (-5, 'call'),
  1043. (-4, 'line'),
  1044. (-4, 'return'),
  1045. (3, 'line'),
  1046. (2, 'line'),
  1047. (-3, 'call'),
  1048. (-2, 'line'),
  1049. (-2, 'return'),
  1050. (4, 'line'),
  1051. (4, 'return')])
  1052. self.run_and_compare(func_return,
  1053. [(0, 'call'),
  1054. (1, 'line'),
  1055. (-11, 'call'),
  1056. (-10, 'line'),
  1057. (-10, 'return'),
  1058. (2, 'line'),
  1059. (1, 'line'),
  1060. (-9, 'call'),
  1061. (-8, 'line'),
  1062. (-8, 'return'),
  1063. (1, 'return')])
  1064. def test_flow_converges_on_same_line(self):
  1065. def foo(x):
  1066. if x:
  1067. try:
  1068. 1/(x - 1)
  1069. except ZeroDivisionError:
  1070. pass
  1071. return x
  1072. def func():
  1073. for i in range(2):
  1074. foo(i)
  1075. self.run_and_compare(func,
  1076. [(0, 'call'),
  1077. (1, 'line'),
  1078. (2, 'line'),
  1079. (-8, 'call'),
  1080. (-7, 'line'),
  1081. (-2, 'line'),
  1082. (-2, 'return'),
  1083. (1, 'line'),
  1084. (2, 'line'),
  1085. (-8, 'call'),
  1086. (-7, 'line'),
  1087. (-6, 'line'),
  1088. (-5, 'line'),
  1089. (-5, 'exception'),
  1090. (-4, 'line'),
  1091. (-3, 'line'),
  1092. (-2, 'line'),
  1093. (-2, 'return'),
  1094. (1, 'line'),
  1095. (1, 'return')])
  1096. def test_no_tracing_of_named_except_cleanup(self):
  1097. def func():
  1098. x = 0
  1099. try:
  1100. 1/x
  1101. except ZeroDivisionError as error:
  1102. if x:
  1103. raise
  1104. return "done"
  1105. self.run_and_compare(func,
  1106. [(0, 'call'),
  1107. (1, 'line'),
  1108. (2, 'line'),
  1109. (3, 'line'),
  1110. (3, 'exception'),
  1111. (4, 'line'),
  1112. (5, 'line'),
  1113. (7, 'line'),
  1114. (7, 'return')])
  1115. def test_tracing_exception_raised_in_with(self):
  1116. class NullCtx:
  1117. def __enter__(self):
  1118. return self
  1119. def __exit__(self, *excinfo):
  1120. pass
  1121. def func():
  1122. try:
  1123. with NullCtx():
  1124. 1/0
  1125. except ZeroDivisionError:
  1126. pass
  1127. self.run_and_compare(func,
  1128. [(0, 'call'),
  1129. (1, 'line'),
  1130. (2, 'line'),
  1131. (-5, 'call'),
  1132. (-4, 'line'),
  1133. (-4, 'return'),
  1134. (3, 'line'),
  1135. (3, 'exception'),
  1136. (2, 'line'),
  1137. (-3, 'call'),
  1138. (-2, 'line'),
  1139. (-2, 'return'),
  1140. (4, 'line'),
  1141. (5, 'line'),
  1142. (5, 'return')])
  1143. def test_try_except_star_no_exception(self):
  1144. def func():
  1145. try:
  1146. 2
  1147. except* Exception:
  1148. 4
  1149. else:
  1150. 6
  1151. if False:
  1152. 8
  1153. else:
  1154. 10
  1155. if func.__name__ == 'Fred':
  1156. 12
  1157. finally:
  1158. 14
  1159. self.run_and_compare(func,
  1160. [(0, 'call'),
  1161. (1, 'line'),
  1162. (2, 'line'),
  1163. (6, 'line'),
  1164. (7, 'line'),
  1165. (10, 'line'),
  1166. (11, 'line'),
  1167. (14, 'line'),
  1168. (14, 'return')])
  1169. def test_try_except_star_named_no_exception(self):
  1170. def func():
  1171. try:
  1172. 2
  1173. except* Exception as e:
  1174. 4
  1175. else:
  1176. 6
  1177. finally:
  1178. 8
  1179. self.run_and_compare(func,
  1180. [(0, 'call'),
  1181. (1, 'line'),
  1182. (2, 'line'),
  1183. (6, 'line'),
  1184. (8, 'line'),
  1185. (8, 'return')])
  1186. def test_try_except_star_exception_caught(self):
  1187. def func():
  1188. try:
  1189. raise ValueError(2)
  1190. except* ValueError:
  1191. 4
  1192. else:
  1193. 6
  1194. finally:
  1195. 8
  1196. self.run_and_compare(func,
  1197. [(0, 'call'),
  1198. (1, 'line'),
  1199. (2, 'line'),
  1200. (2, 'exception'),
  1201. (3, 'line'),
  1202. (4, 'line'),
  1203. (8, 'line'),
  1204. (8, 'return')])
  1205. def test_try_except_star_named_exception_caught(self):
  1206. def func():
  1207. try:
  1208. raise ValueError(2)
  1209. except* ValueError as e:
  1210. 4
  1211. else:
  1212. 6
  1213. finally:
  1214. 8
  1215. self.run_and_compare(func,
  1216. [(0, 'call'),
  1217. (1, 'line'),
  1218. (2, 'line'),
  1219. (2, 'exception'),
  1220. (3, 'line'),
  1221. (4, 'line'),
  1222. (8, 'line'),
  1223. (8, 'return')])
  1224. def test_try_except_star_exception_not_caught(self):
  1225. def func():
  1226. try:
  1227. try:
  1228. raise ValueError(3)
  1229. except* TypeError:
  1230. 5
  1231. except ValueError:
  1232. 7
  1233. self.run_and_compare(func,
  1234. [(0, 'call'),
  1235. (1, 'line'),
  1236. (2, 'line'),
  1237. (3, 'line'),
  1238. (3, 'exception'),
  1239. (4, 'line'),
  1240. (6, 'line'),
  1241. (7, 'line'),
  1242. (7, 'return')])
  1243. def test_try_except_star_named_exception_not_caught(self):
  1244. def func():
  1245. try:
  1246. try:
  1247. raise ValueError(3)
  1248. except* TypeError as e:
  1249. 5
  1250. except ValueError:
  1251. 7
  1252. self.run_and_compare(func,
  1253. [(0, 'call'),
  1254. (1, 'line'),
  1255. (2, 'line'),
  1256. (3, 'line'),
  1257. (3, 'exception'),
  1258. (4, 'line'),
  1259. (6, 'line'),
  1260. (7, 'line'),
  1261. (7, 'return')])
  1262. def test_try_except_star_nested(self):
  1263. def func():
  1264. try:
  1265. try:
  1266. raise ExceptionGroup(
  1267. 'eg',
  1268. [ValueError(5), TypeError('bad type')])
  1269. except* TypeError as e:
  1270. 7
  1271. except* OSError:
  1272. 9
  1273. except* ValueError:
  1274. raise
  1275. except* ValueError:
  1276. try:
  1277. raise TypeError(14)
  1278. except* OSError:
  1279. 16
  1280. except* TypeError as e:
  1281. 18
  1282. return 0
  1283. self.run_and_compare(func,
  1284. [(0, 'call'),
  1285. (1, 'line'),
  1286. (2, 'line'),
  1287. (3, 'line'),
  1288. (4, 'line'),
  1289. (5, 'line'),
  1290. (3, 'line'),
  1291. (3, 'exception'),
  1292. (6, 'line'),
  1293. (7, 'line'),
  1294. (8, 'line'),
  1295. (10, 'line'),
  1296. (11, 'line'),
  1297. (12, 'line'),
  1298. (13, 'line'),
  1299. (14, 'line'),
  1300. (14, 'exception'),
  1301. (15, 'line'),
  1302. (17, 'line'),
  1303. (18, 'line'),
  1304. (19, 'line'),
  1305. (19, 'return')])
  1306. def test_notrace_lambda(self):
  1307. #Regression test for issue 46314
  1308. def func():
  1309. 1
  1310. lambda x: 2
  1311. 3
  1312. self.run_and_compare(func,
  1313. [(0, 'call'),
  1314. (1, 'line'),
  1315. (2, 'line'),
  1316. (3, 'line'),
  1317. (3, 'return')])
  1318. def test_class_creation_with_docstrings(self):
  1319. def func():
  1320. class Class_1:
  1321. ''' the docstring. 2'''
  1322. def __init__(self):
  1323. ''' Another docstring. 4'''
  1324. self.a = 5
  1325. self.run_and_compare(func,
  1326. [(0, 'call'),
  1327. (1, 'line'),
  1328. (1, 'call'),
  1329. (1, 'line'),
  1330. (2, 'line'),
  1331. (3, 'line'),
  1332. (3, 'return'),
  1333. (1, 'return')])
  1334. @support.cpython_only
  1335. def test_no_line_event_after_creating_generator(self):
  1336. # Spurious line events before call events only show up with C tracer
  1337. # Skip this test if the _testcapi module isn't available.
  1338. _testcapi = import_helper.import_module('_testcapi')
  1339. def gen():
  1340. yield 1
  1341. def func():
  1342. for _ in (
  1343. gen()
  1344. ):
  1345. pass
  1346. EXPECTED_EVENTS = [
  1347. (0, 'call'),
  1348. (2, 'line'),
  1349. (1, 'line'),
  1350. (-3, 'call'),
  1351. (-2, 'line'),
  1352. (-2, 'return'),
  1353. (4, 'line'),
  1354. (1, 'line'),
  1355. (-2, 'call'),
  1356. (-2, 'return'),
  1357. (1, 'return'),
  1358. ]
  1359. # C level events should be the same as expected and the same as Python level.
  1360. events = []
  1361. # Turning on and off tracing must be on same line to avoid unwanted LINE events.
  1362. _testcapi.settrace_to_record(events); func(); sys.settrace(None)
  1363. start_line = func.__code__.co_firstlineno
  1364. events = [
  1365. (line-start_line, EVENT_NAMES[what])
  1366. for (what, line, arg) in events
  1367. ]
  1368. self.assertEqual(events, EXPECTED_EVENTS)
  1369. self.run_and_compare(func, EXPECTED_EVENTS)
  1370. def test_very_large_function(self):
  1371. # There is a separate code path when the number of lines > (1 << 15).
  1372. d = {}
  1373. exec("""def f(): # line 0
  1374. x = 0 # line 1
  1375. y = 1 # line 2
  1376. %s # lines 3 through (1 << 16)
  1377. x += 1 #
  1378. return""" % ('\n' * (1 << 16),), d)
  1379. f = d['f']
  1380. EXPECTED_EVENTS = [
  1381. (0, 'call'),
  1382. (1, 'line'),
  1383. (2, 'line'),
  1384. (65540, 'line'),
  1385. (65541, 'line'),
  1386. (65541, 'return'),
  1387. ]
  1388. self.run_and_compare(f, EXPECTED_EVENTS)
  1389. EVENT_NAMES = [
  1390. 'call',
  1391. 'exception',
  1392. 'line',
  1393. 'return'
  1394. ]
  1395. class SkipLineEventsTraceTestCase(TraceTestCase):
  1396. """Repeat the trace tests, but with per-line events skipped"""
  1397. def compare_events(self, line_offset, events, expected_events):
  1398. skip_line_events = [e for e in expected_events if e[1] != 'line']
  1399. super().compare_events(line_offset, events, skip_line_events)
  1400. @staticmethod
  1401. def make_tracer():
  1402. return Tracer(trace_line_events=False)
  1403. @support.cpython_only
  1404. class TraceOpcodesTestCase(TraceTestCase):
  1405. """Repeat the trace tests, but with per-opcodes events enabled"""
  1406. def compare_events(self, line_offset, events, expected_events):
  1407. skip_opcode_events = [e for e in events if e[1] != 'opcode']
  1408. if len(events) > 1:
  1409. self.assertLess(len(skip_opcode_events), len(events),
  1410. msg="No 'opcode' events received by the tracer")
  1411. super().compare_events(line_offset, skip_opcode_events, expected_events)
  1412. @staticmethod
  1413. def make_tracer():
  1414. return Tracer(trace_opcode_events=True)
  1415. class RaisingTraceFuncTestCase(unittest.TestCase):
  1416. def setUp(self):
  1417. self.addCleanup(sys.settrace, sys.gettrace())
  1418. def trace(self, frame, event, arg):
  1419. """A trace function that raises an exception in response to a
  1420. specific trace event."""
  1421. if event == self.raiseOnEvent:
  1422. raise ValueError # just something that isn't RuntimeError
  1423. else:
  1424. return self.trace
  1425. def f(self):
  1426. """The function to trace; raises an exception if that's the case
  1427. we're testing, so that the 'exception' trace event fires."""
  1428. if self.raiseOnEvent == 'exception':
  1429. x = 0
  1430. y = 1/x
  1431. else:
  1432. return 1
  1433. def run_test_for_event(self, event):
  1434. """Tests that an exception raised in response to the given event is
  1435. handled OK."""
  1436. self.raiseOnEvent = event
  1437. try:
  1438. for i in range(sys.getrecursionlimit() + 1):
  1439. sys.settrace(self.trace)
  1440. try:
  1441. self.f()
  1442. except ValueError:
  1443. pass
  1444. else:
  1445. self.fail("exception not raised!")
  1446. except RuntimeError:
  1447. self.fail("recursion counter not reset")
  1448. # Test the handling of exceptions raised by each kind of trace event.
  1449. def test_call(self):
  1450. self.run_test_for_event('call')
  1451. def test_line(self):
  1452. self.run_test_for_event('line')
  1453. def test_return(self):
  1454. self.run_test_for_event('return')
  1455. def test_exception(self):
  1456. self.run_test_for_event('exception')
  1457. def test_trash_stack(self):
  1458. def f():
  1459. for i in range(5):
  1460. print(i) # line tracing will raise an exception at this line
  1461. def g(frame, why, extra):
  1462. if (why == 'line' and
  1463. frame.f_lineno == f.__code__.co_firstlineno + 2):
  1464. raise RuntimeError("i am crashing")
  1465. return g
  1466. sys.settrace(g)
  1467. try:
  1468. f()
  1469. except RuntimeError:
  1470. # the test is really that this doesn't segfault:
  1471. import gc
  1472. gc.collect()
  1473. else:
  1474. self.fail("exception not propagated")
  1475. def test_exception_arguments(self):
  1476. def f():
  1477. x = 0
  1478. # this should raise an error
  1479. x.no_such_attr
  1480. def g(frame, event, arg):
  1481. if (event == 'exception'):
  1482. type, exception, trace = arg
  1483. self.assertIsInstance(exception, Exception)
  1484. return g
  1485. existing = sys.gettrace()
  1486. try:
  1487. sys.settrace(g)
  1488. try:
  1489. f()
  1490. except AttributeError:
  1491. # this is expected
  1492. pass
  1493. finally:
  1494. sys.settrace(existing)
  1495. def test_line_event_raises_before_opcode_event(self):
  1496. exception = ValueError("BOOM!")
  1497. def trace(frame, event, arg):
  1498. if event == "line":
  1499. raise exception
  1500. frame.f_trace_opcodes = True
  1501. return trace
  1502. def f():
  1503. pass
  1504. with self.assertRaises(ValueError) as caught:
  1505. sys.settrace(trace)
  1506. f()
  1507. self.assertIs(caught.exception, exception)
  1508. # 'Jump' tests: assigning to frame.f_lineno within a trace function
  1509. # moves the execution position - it's how debuggers implement a Jump
  1510. # command (aka. "Set next statement").
  1511. class JumpTracer:
  1512. """Defines a trace function that jumps from one place to another."""
  1513. def __init__(self, function, jumpFrom, jumpTo, event='line',
  1514. decorated=False):
  1515. self.code = function.__code__
  1516. self.jumpFrom = jumpFrom
  1517. self.jumpTo = jumpTo
  1518. self.event = event
  1519. self.firstLine = None if decorated else self.code.co_firstlineno
  1520. self.done = False
  1521. def trace(self, frame, event, arg):
  1522. if self.done:
  1523. return
  1524. # frame.f_code.co_firstlineno is the first line of the decorator when
  1525. # 'function' is decorated and the decorator may be written using
  1526. # multiple physical lines when it is too long. Use the first line
  1527. # trace event in 'function' to find the first line of 'function'.
  1528. if (self.firstLine is None and frame.f_code == self.code and
  1529. event == 'line'):
  1530. self.firstLine = frame.f_lineno - 1
  1531. if (event == self.event and self.firstLine is not None and
  1532. frame.f_lineno == self.firstLine + self.jumpFrom):
  1533. f = frame
  1534. while f is not None and f.f_code != self.code:
  1535. f = f.f_back
  1536. if f is not None:
  1537. # Cope with non-integer self.jumpTo (because of
  1538. # no_jump_to_non_integers below).
  1539. try:
  1540. frame.f_lineno = self.firstLine + self.jumpTo
  1541. except TypeError:
  1542. frame.f_lineno = self.jumpTo
  1543. self.done = True
  1544. return self.trace
  1545. # This verifies the line-numbers-must-be-integers rule.
  1546. def no_jump_to_non_integers(output):
  1547. try:
  1548. output.append(2)
  1549. except ValueError as e:
  1550. output.append('integer' in str(e))
  1551. # This verifies that you can't set f_lineno via _getframe or similar
  1552. # trickery.
  1553. def no_jump_without_trace_function():
  1554. try:
  1555. previous_frame = sys._getframe().f_back
  1556. previous_frame.f_lineno = previous_frame.f_lineno
  1557. except ValueError as e:
  1558. # This is the exception we wanted; make sure the error message
  1559. # talks about trace functions.
  1560. if 'trace' not in str(e):
  1561. raise
  1562. else:
  1563. # Something's wrong - the expected exception wasn't raised.
  1564. raise AssertionError("Trace-function-less jump failed to fail")
  1565. class JumpTestCase(unittest.TestCase):
  1566. def setUp(self):
  1567. self.addCleanup(sys.settrace, sys.gettrace())
  1568. sys.settrace(None)
  1569. def compare_jump_output(self, expected, received):
  1570. if received != expected:
  1571. self.fail( "Outputs don't match:\n" +
  1572. "Expected: " + repr(expected) + "\n" +
  1573. "Received: " + repr(received))
  1574. def run_test(self, func, jumpFrom, jumpTo, expected, error=None,
  1575. event='line', decorated=False):
  1576. tracer = JumpTracer(func, jumpFrom, jumpTo, event, decorated)
  1577. sys.settrace(tracer.trace)
  1578. output = []
  1579. if error is None:
  1580. func(output)
  1581. else:
  1582. with self.assertRaisesRegex(*error):
  1583. func(output)
  1584. sys.settrace(None)
  1585. self.compare_jump_output(expected, output)
  1586. def run_async_test(self, func, jumpFrom, jumpTo, expected, error=None,
  1587. event='line', decorated=False):
  1588. tracer = JumpTracer(func, jumpFrom, jumpTo, event, decorated)
  1589. sys.settrace(tracer.trace)
  1590. output = []
  1591. if error is None:
  1592. asyncio.run(func(output))
  1593. else:
  1594. with self.assertRaisesRegex(*error):
  1595. asyncio.run(func(output))
  1596. sys.settrace(None)
  1597. asyncio.set_event_loop_policy(None)
  1598. self.compare_jump_output(expected, output)
  1599. def jump_test(jumpFrom, jumpTo, expected, error=None, event='line'):
  1600. """Decorator that creates a test that makes a jump
  1601. from one place to another in the following code.
  1602. """
  1603. def decorator(func):
  1604. @wraps(func)
  1605. def test(self):
  1606. self.run_test(func, jumpFrom, jumpTo, expected,
  1607. error=error, event=event, decorated=True)
  1608. return test
  1609. return decorator
  1610. def async_jump_test(jumpFrom, jumpTo, expected, error=None, event='line'):
  1611. """Decorator that creates a test that makes a jump
  1612. from one place to another in the following asynchronous code.
  1613. """
  1614. def decorator(func):
  1615. @wraps(func)
  1616. def test(self):
  1617. self.run_async_test(func, jumpFrom, jumpTo, expected,
  1618. error=error, event=event, decorated=True)
  1619. return test
  1620. return decorator
  1621. ## The first set of 'jump' tests are for things that are allowed:
  1622. @jump_test(1, 3, [3])
  1623. def test_jump_simple_forwards(output):
  1624. output.append(1)
  1625. output.append(2)
  1626. output.append(3)
  1627. @jump_test(2, 1, [1, 1, 2])
  1628. def test_jump_simple_backwards(output):
  1629. output.append(1)
  1630. output.append(2)
  1631. @jump_test(3, 5, [2, 5])
  1632. def test_jump_out_of_block_forwards(output):
  1633. for i in 1, 2:
  1634. output.append(2)
  1635. for j in [3]: # Also tests jumping over a block
  1636. output.append(4)
  1637. output.append(5)
  1638. @jump_test(6, 1, [1, 3, 5, 1, 3, 5, 6, 7])
  1639. def test_jump_out_of_block_backwards(output):
  1640. output.append(1)
  1641. for i in [1]:
  1642. output.append(3)
  1643. for j in [2]: # Also tests jumping over a block
  1644. output.append(5)
  1645. output.append(6)
  1646. output.append(7)
  1647. @async_jump_test(4, 5, [3, 5])
  1648. async def test_jump_out_of_async_for_block_forwards(output):
  1649. for i in [1]:
  1650. async for i in asynciter([1, 2]):
  1651. output.append(3)
  1652. output.append(4)
  1653. output.append(5)
  1654. @async_jump_test(5, 2, [2, 4, 2, 4, 5, 6])
  1655. async def test_jump_out_of_async_for_block_backwards(output):
  1656. for i in [1]:
  1657. output.append(2)
  1658. async for i in asynciter([1]):
  1659. output.append(4)
  1660. output.append(5)
  1661. output.append(6)
  1662. @jump_test(1, 2, [3])
  1663. def test_jump_to_codeless_line(output):
  1664. output.append(1)
  1665. # Jumping to this line should skip to the next one.
  1666. output.append(3)
  1667. @jump_test(2, 2, [1, 2, 3])
  1668. def test_jump_to_same_line(output):
  1669. output.append(1)
  1670. output.append(2)
  1671. output.append(3)
  1672. # Tests jumping within a finally block, and over one.
  1673. @jump_test(4, 9, [2, 9])
  1674. def test_jump_in_nested_finally(output):
  1675. try:
  1676. output.append(2)
  1677. finally:
  1678. output.append(4)
  1679. try:
  1680. output.append(6)
  1681. finally:
  1682. output.append(8)
  1683. output.append(9)
  1684. @jump_test(6, 7, [2, 7], (ZeroDivisionError, ''))
  1685. def test_jump_in_nested_finally_2(output):
  1686. try:
  1687. output.append(2)
  1688. 1/0
  1689. return
  1690. finally:
  1691. output.append(6)
  1692. output.append(7)
  1693. output.append(8)
  1694. @jump_test(6, 11, [2, 11], (ZeroDivisionError, ''))
  1695. def test_jump_in_nested_finally_3(output):
  1696. try:
  1697. output.append(2)
  1698. 1/0
  1699. return
  1700. finally:
  1701. output.append(6)
  1702. try:
  1703. output.append(8)
  1704. finally:
  1705. output.append(10)
  1706. output.append(11)
  1707. output.append(12)
  1708. @jump_test(5, 11, [2, 4], (ValueError, 'exception'))
  1709. def test_no_jump_over_return_try_finally_in_finally_block(output):
  1710. try:
  1711. output.append(2)
  1712. finally:
  1713. output.append(4)
  1714. output.append(5)
  1715. return
  1716. try:
  1717. output.append(8)
  1718. finally:
  1719. output.append(10)
  1720. pass
  1721. output.append(12)
  1722. @jump_test(3, 4, [1], (ValueError, 'after'))
  1723. def test_no_jump_infinite_while_loop(output):
  1724. output.append(1)
  1725. while True:
  1726. output.append(3)
  1727. output.append(4)
  1728. @jump_test(2, 4, [4, 4])
  1729. def test_jump_forwards_into_while_block(output):
  1730. i = 1
  1731. output.append(2)
  1732. while i <= 2:
  1733. output.append(4)
  1734. i += 1
  1735. @jump_test(5, 3, [3, 3, 3, 5])
  1736. def test_jump_backwards_into_while_block(output):
  1737. i = 1
  1738. while i <= 2:
  1739. output.append(3)
  1740. i += 1
  1741. output.append(5)
  1742. @jump_test(2, 3, [1, 3])
  1743. def test_jump_forwards_out_of_with_block(output):
  1744. with tracecontext(output, 1):
  1745. output.append(2)
  1746. output.append(3)
  1747. @async_jump_test(2, 3, [1, 3])
  1748. async def test_jump_forwards_out_of_async_with_block(output):
  1749. async with asynctracecontext(output, 1):
  1750. output.append(2)
  1751. output.append(3)
  1752. @jump_test(3, 1, [1, 2, 1, 2, 3, -2])
  1753. def test_jump_backwards_out_of_with_block(output):
  1754. output.append(1)
  1755. with tracecontext(output, 2):
  1756. output.append(3)
  1757. @async_jump_test(3, 1, [1, 2, 1, 2, 3, -2])
  1758. async def test_jump_backwards_out_of_async_with_block(output):
  1759. output.append(1)
  1760. async with asynctracecontext(output, 2):
  1761. output.append(3)
  1762. @jump_test(2, 5, [5])
  1763. def test_jump_forwards_out_of_try_finally_block(output):
  1764. try:
  1765. output.append(2)
  1766. finally:
  1767. output.append(4)
  1768. output.append(5)
  1769. @jump_test(3, 1, [1, 1, 3, 5])
  1770. def test_jump_backwards_out_of_try_finally_block(output):
  1771. output.append(1)
  1772. try:
  1773. output.append(3)
  1774. finally:
  1775. output.append(5)
  1776. @jump_test(2, 6, [6])
  1777. def test_jump_forwards_out_of_try_except_block(output):
  1778. try:
  1779. output.append(2)
  1780. except:
  1781. output.append(4)
  1782. raise
  1783. output.append(6)
  1784. @jump_test(3, 1, [1, 1, 3])
  1785. def test_jump_backwards_out_of_try_except_block(output):
  1786. output.append(1)
  1787. try:
  1788. output.append(3)
  1789. except:
  1790. output.append(5)
  1791. raise
  1792. @jump_test(5, 7, [4, 7, 8])
  1793. def test_jump_between_except_blocks(output):
  1794. try:
  1795. 1/0
  1796. except ZeroDivisionError:
  1797. output.append(4)
  1798. output.append(5)
  1799. except FloatingPointError:
  1800. output.append(7)
  1801. output.append(8)
  1802. @jump_test(5, 7, [4, 7, 8])
  1803. def test_jump_from_except_to_finally(output):
  1804. try:
  1805. 1/0
  1806. except ZeroDivisionError:
  1807. output.append(4)
  1808. output.append(5)
  1809. finally:
  1810. output.append(7)
  1811. output.append(8)
  1812. @jump_test(5, 6, [4, 6, 7])
  1813. def test_jump_within_except_block(output):
  1814. try:
  1815. 1/0
  1816. except:
  1817. output.append(4)
  1818. output.append(5)
  1819. output.append(6)
  1820. output.append(7)
  1821. @jump_test(6, 1, [1, 5, 1, 5])
  1822. def test_jump_over_try_except(output):
  1823. output.append(1)
  1824. try:
  1825. 1 / 0
  1826. except ZeroDivisionError as e:
  1827. output.append(5)
  1828. x = 42 # has to be a two-instruction block
  1829. @jump_test(2, 4, [1, 4, 5, -4])
  1830. def test_jump_across_with(output):
  1831. output.append(1)
  1832. with tracecontext(output, 2):
  1833. output.append(3)
  1834. with tracecontext(output, 4):
  1835. output.append(5)
  1836. @async_jump_test(2, 4, [1, 4, 5, -4])
  1837. async def test_jump_across_async_with(output):
  1838. output.append(1)
  1839. async with asynctracecontext(output, 2):
  1840. output.append(3)
  1841. async with asynctracecontext(output, 4):
  1842. output.append(5)
  1843. @jump_test(4, 5, [1, 3, 5, 6])
  1844. def test_jump_out_of_with_block_within_for_block(output):
  1845. output.append(1)
  1846. for i in [1]:
  1847. with tracecontext(output, 3):
  1848. output.append(4)
  1849. output.append(5)
  1850. output.append(6)
  1851. @async_jump_test(4, 5, [1, 3, 5, 6])
  1852. async def test_jump_out_of_async_with_block_within_for_block(output):
  1853. output.append(1)
  1854. for i in [1]:
  1855. async with asynctracecontext(output, 3):
  1856. output.append(4)
  1857. output.append(5)
  1858. output.append(6)
  1859. @jump_test(4, 5, [1, 2, 3, 5, -2, 6])
  1860. def test_jump_out_of_with_block_within_with_block(output):
  1861. output.append(1)
  1862. with tracecontext(output, 2):
  1863. with tracecontext(output, 3):
  1864. output.append(4)
  1865. output.append(5)
  1866. output.append(6)
  1867. @async_jump_test(4, 5, [1, 2, 3, 5, -2, 6])
  1868. async def test_jump_out_of_async_with_block_within_with_block(output):
  1869. output.append(1)
  1870. with tracecontext(output, 2):
  1871. async with asynctracecontext(output, 3):
  1872. output.append(4)
  1873. output.append(5)
  1874. output.append(6)
  1875. @jump_test(5, 6, [2, 4, 6, 7])
  1876. def test_jump_out_of_with_block_within_finally_block(output):
  1877. try:
  1878. output.append(2)
  1879. finally:
  1880. with tracecontext(output, 4):
  1881. output.append(5)
  1882. output.append(6)
  1883. output.append(7)
  1884. @async_jump_test(5, 6, [2, 4, 6, 7])
  1885. async def test_jump_out_of_async_with_block_within_finally_block(output):
  1886. try:
  1887. output.append(2)
  1888. finally:
  1889. async with asynctracecontext(output, 4):
  1890. output.append(5)
  1891. output.append(6)
  1892. output.append(7)
  1893. @jump_test(8, 11, [1, 3, 5, 11, 12])
  1894. def test_jump_out_of_complex_nested_blocks(output):
  1895. output.append(1)
  1896. for i in [1]:
  1897. output.append(3)
  1898. for j in [1, 2]:
  1899. output.append(5)
  1900. try:
  1901. for k in [1, 2]:
  1902. output.append(8)
  1903. finally:
  1904. output.append(10)
  1905. output.append(11)
  1906. output.append(12)
  1907. @jump_test(3, 5, [1, 2, 5])
  1908. def test_jump_out_of_with_assignment(output):
  1909. output.append(1)
  1910. with tracecontext(output, 2) \
  1911. as x:
  1912. output.append(4)
  1913. output.append(5)
  1914. @async_jump_test(3, 5, [1, 2, 5])
  1915. async def test_jump_out_of_async_with_assignment(output):
  1916. output.append(1)
  1917. async with asynctracecontext(output, 2) \
  1918. as x:
  1919. output.append(4)
  1920. output.append(5)
  1921. @jump_test(3, 6, [1, 6, 8, 9])
  1922. def test_jump_over_return_in_try_finally_block(output):
  1923. output.append(1)
  1924. try:
  1925. output.append(3)
  1926. if not output: # always false
  1927. return
  1928. output.append(6)
  1929. finally:
  1930. output.append(8)
  1931. output.append(9)
  1932. @jump_test(5, 8, [1, 3, 8, 10, 11, 13])
  1933. def test_jump_over_break_in_try_finally_block(output):
  1934. output.append(1)
  1935. while True:
  1936. output.append(3)
  1937. try:
  1938. output.append(5)
  1939. if not output: # always false
  1940. break
  1941. output.append(8)
  1942. finally:
  1943. output.append(10)
  1944. output.append(11)
  1945. break
  1946. output.append(13)
  1947. @jump_test(1, 7, [7, 8])
  1948. def test_jump_over_for_block_before_else(output):
  1949. output.append(1)
  1950. if not output: # always false
  1951. for i in [3]:
  1952. output.append(4)
  1953. else:
  1954. output.append(6)
  1955. output.append(7)
  1956. output.append(8)
  1957. @async_jump_test(1, 7, [7, 8])
  1958. async def test_jump_over_async_for_block_before_else(output):
  1959. output.append(1)
  1960. if not output: # always false
  1961. async for i in asynciter([3]):
  1962. output.append(4)
  1963. else:
  1964. output.append(6)
  1965. output.append(7)
  1966. output.append(8)
  1967. # The second set of 'jump' tests are for things that are not allowed:
  1968. @jump_test(2, 3, [1], (ValueError, 'after'))
  1969. def test_no_jump_too_far_forwards(output):
  1970. output.append(1)
  1971. output.append(2)
  1972. @jump_test(2, -2, [1], (ValueError, 'before'))
  1973. def test_no_jump_too_far_backwards(output):
  1974. output.append(1)
  1975. output.append(2)
  1976. # Test each kind of 'except' line.
  1977. @jump_test(2, 3, [4], (ValueError, 'except'))
  1978. def test_no_jump_to_except_1(output):
  1979. try:
  1980. output.append(2)
  1981. except:
  1982. output.append(4)
  1983. raise
  1984. @jump_test(2, 3, [4], (ValueError, 'except'))
  1985. def test_no_jump_to_except_2(output):
  1986. try:
  1987. output.append(2)
  1988. except ValueError:
  1989. output.append(4)
  1990. raise
  1991. @jump_test(2, 3, [4], (ValueError, 'except'))
  1992. def test_no_jump_to_except_3(output):
  1993. try:
  1994. output.append(2)
  1995. except ValueError as e:
  1996. output.append(4)
  1997. raise e
  1998. @jump_test(2, 3, [4], (ValueError, 'except'))
  1999. def test_no_jump_to_except_4(output):
  2000. try:
  2001. output.append(2)
  2002. except (ValueError, RuntimeError) as e:
  2003. output.append(4)
  2004. raise e
  2005. @jump_test(1, 3, [], (ValueError, 'into'))
  2006. def test_no_jump_forwards_into_for_block(output):
  2007. output.append(1)
  2008. for i in 1, 2:
  2009. output.append(3)
  2010. @async_jump_test(1, 3, [], (ValueError, 'into'))
  2011. async def test_no_jump_forwards_into_async_for_block(output):
  2012. output.append(1)
  2013. async for i in asynciter([1, 2]):
  2014. output.append(3)
  2015. pass
  2016. @jump_test(3, 2, [2, 2], (ValueError, 'into'))
  2017. def test_no_jump_backwards_into_for_block(output):
  2018. for i in 1, 2:
  2019. output.append(2)
  2020. output.append(3)
  2021. @async_jump_test(3, 2, [2, 2], (ValueError, "can't jump into the body of a for loop"))
  2022. async def test_no_jump_backwards_into_async_for_block(output):
  2023. async for i in asynciter([1, 2]):
  2024. output.append(2)
  2025. output.append(3)
  2026. @jump_test(1, 3, [], (ValueError, 'stack'))
  2027. def test_no_jump_forwards_into_with_block(output):
  2028. output.append(1)
  2029. with tracecontext(output, 2):
  2030. output.append(3)
  2031. @async_jump_test(1, 3, [], (ValueError, 'stack'))
  2032. async def test_no_jump_forwards_into_async_with_block(output):
  2033. output.append(1)
  2034. async with asynctracecontext(output, 2):
  2035. output.append(3)
  2036. @jump_test(3, 2, [1, 2, -1], (ValueError, 'stack'))
  2037. def test_no_jump_backwards_into_with_block(output):
  2038. with tracecontext(output, 1):
  2039. output.append(2)
  2040. output.append(3)
  2041. @async_jump_test(3, 2, [1, 2, -1], (ValueError, 'stack'))
  2042. async def test_no_jump_backwards_into_async_with_block(output):
  2043. async with asynctracecontext(output, 1):
  2044. output.append(2)
  2045. output.append(3)
  2046. @jump_test(1, 3, [3, 5])
  2047. def test_jump_forwards_into_try_finally_block(output):
  2048. output.append(1)
  2049. try:
  2050. output.append(3)
  2051. finally:
  2052. output.append(5)
  2053. @jump_test(5, 2, [2, 4, 2, 4, 5])
  2054. def test_jump_backwards_into_try_finally_block(output):
  2055. try:
  2056. output.append(2)
  2057. finally:
  2058. output.append(4)
  2059. output.append(5)
  2060. @jump_test(1, 3, [3])
  2061. def test_jump_forwards_into_try_except_block(output):
  2062. output.append(1)
  2063. try:
  2064. output.append(3)
  2065. except:
  2066. output.append(5)
  2067. raise
  2068. @jump_test(6, 2, [2, 2, 6])
  2069. def test_jump_backwards_into_try_except_block(output):
  2070. try:
  2071. output.append(2)
  2072. except:
  2073. output.append(4)
  2074. raise
  2075. output.append(6)
  2076. # 'except' with a variable creates an implicit finally block
  2077. @jump_test(5, 7, [4, 7, 8])
  2078. def test_jump_between_except_blocks_2(output):
  2079. try:
  2080. 1/0
  2081. except ZeroDivisionError:
  2082. output.append(4)
  2083. output.append(5)
  2084. except FloatingPointError as e:
  2085. output.append(7)
  2086. output.append(8)
  2087. @jump_test(1, 5, [5])
  2088. def test_jump_into_finally_block(output):
  2089. output.append(1)
  2090. try:
  2091. output.append(3)
  2092. finally:
  2093. output.append(5)
  2094. @jump_test(3, 6, [2, 6, 7])
  2095. def test_jump_into_finally_block_from_try_block(output):
  2096. try:
  2097. output.append(2)
  2098. output.append(3)
  2099. finally: # still executed if the jump is failed
  2100. output.append(5)
  2101. output.append(6)
  2102. output.append(7)
  2103. @jump_test(5, 1, [1, 3, 1, 3, 5])
  2104. def test_jump_out_of_finally_block(output):
  2105. output.append(1)
  2106. try:
  2107. output.append(3)
  2108. finally:
  2109. output.append(5)
  2110. @jump_test(1, 5, [], (ValueError, "can't jump into an 'except' block as there's no exception"))
  2111. def test_no_jump_into_bare_except_block(output):
  2112. output.append(1)
  2113. try:
  2114. output.append(3)
  2115. except:
  2116. output.append(5)
  2117. @jump_test(1, 5, [], (ValueError, "can't jump into an 'except' block as there's no exception"))
  2118. def test_no_jump_into_qualified_except_block(output):
  2119. output.append(1)
  2120. try:
  2121. output.append(3)
  2122. except Exception:
  2123. output.append(5)
  2124. @jump_test(3, 6, [2, 5, 6], (ValueError, "can't jump into an 'except' block as there's no exception"))
  2125. def test_no_jump_into_bare_except_block_from_try_block(output):
  2126. try:
  2127. output.append(2)
  2128. output.append(3)
  2129. except: # executed if the jump is failed
  2130. output.append(5)
  2131. output.append(6)
  2132. raise
  2133. output.append(8)
  2134. @jump_test(3, 6, [2], (ValueError, "can't jump into an 'except' block as there's no exception"))
  2135. def test_no_jump_into_qualified_except_block_from_try_block(output):
  2136. try:
  2137. output.append(2)
  2138. output.append(3)
  2139. except ZeroDivisionError:
  2140. output.append(5)
  2141. output.append(6)
  2142. raise
  2143. output.append(8)
  2144. @jump_test(7, 1, [1, 3, 6, 1, 3, 6, 7])
  2145. def test_jump_out_of_bare_except_block(output):
  2146. output.append(1)
  2147. try:
  2148. output.append(3)
  2149. 1/0
  2150. except:
  2151. output.append(6)
  2152. output.append(7)
  2153. @jump_test(7, 1, [1, 3, 6, 1, 3, 6, 7])
  2154. def test_jump_out_of_qualified_except_block(output):
  2155. output.append(1)
  2156. try:
  2157. output.append(3)
  2158. 1/0
  2159. except Exception:
  2160. output.append(6)
  2161. output.append(7)
  2162. @jump_test(3, 5, [1, 2, 5, -2])
  2163. def test_jump_between_with_blocks(output):
  2164. output.append(1)
  2165. with tracecontext(output, 2):
  2166. output.append(3)
  2167. with tracecontext(output, 4):
  2168. output.append(5)
  2169. @async_jump_test(3, 5, [1, 2, 5, -2])
  2170. async def test_jump_between_async_with_blocks(output):
  2171. output.append(1)
  2172. async with asynctracecontext(output, 2):
  2173. output.append(3)
  2174. async with asynctracecontext(output, 4):
  2175. output.append(5)
  2176. @jump_test(5, 7, [2, 4], (ValueError, "after"))
  2177. def test_no_jump_over_return_out_of_finally_block(output):
  2178. try:
  2179. output.append(2)
  2180. finally:
  2181. output.append(4)
  2182. output.append(5)
  2183. return
  2184. output.append(7)
  2185. @jump_test(7, 4, [1, 6], (ValueError, 'into'))
  2186. def test_no_jump_into_for_block_before_else(output):
  2187. output.append(1)
  2188. if not output: # always false
  2189. for i in [3]:
  2190. output.append(4)
  2191. else:
  2192. output.append(6)
  2193. output.append(7)
  2194. output.append(8)
  2195. @async_jump_test(7, 4, [1, 6], (ValueError, 'into'))
  2196. async def test_no_jump_into_async_for_block_before_else(output):
  2197. output.append(1)
  2198. if not output: # always false
  2199. async for i in asynciter([3]):
  2200. output.append(4)
  2201. else:
  2202. output.append(6)
  2203. output.append(7)
  2204. output.append(8)
  2205. def test_no_jump_to_non_integers(self):
  2206. self.run_test(no_jump_to_non_integers, 2, "Spam", [True])
  2207. def test_no_jump_without_trace_function(self):
  2208. # Must set sys.settrace(None) in setUp(), else condition is not
  2209. # triggered.
  2210. no_jump_without_trace_function()
  2211. def test_large_function(self):
  2212. d = {}
  2213. exec("""def f(output): # line 0
  2214. x = 0 # line 1
  2215. y = 1 # line 2
  2216. ''' # line 3
  2217. %s # lines 4-1004
  2218. ''' # line 1005
  2219. x += 1 # line 1006
  2220. output.append(x) # line 1007
  2221. return""" % ('\n' * 1000,), d)
  2222. f = d['f']
  2223. self.run_test(f, 2, 1007, [0])
  2224. def test_jump_to_firstlineno(self):
  2225. # This tests that PDB can jump back to the first line in a
  2226. # file. See issue #1689458. It can only be triggered in a
  2227. # function call if the function is defined on a single line.
  2228. code = compile("""
  2229. # Comments don't count.
  2230. output.append(2) # firstlineno is here.
  2231. output.append(3)
  2232. output.append(4)
  2233. """, "<fake module>", "exec")
  2234. class fake_function:
  2235. __code__ = code
  2236. tracer = JumpTracer(fake_function, 4, 1)
  2237. sys.settrace(tracer.trace)
  2238. namespace = {"output": []}
  2239. exec(code, namespace)
  2240. sys.settrace(None)
  2241. self.compare_jump_output([2, 3, 2, 3, 4], namespace["output"])
  2242. @jump_test(2, 3, [1], event='call', error=(ValueError, "can't jump from"
  2243. " the 'call' trace event of a new frame"))
  2244. def test_no_jump_from_call(output):
  2245. output.append(1)
  2246. def nested():
  2247. output.append(3)
  2248. nested()
  2249. output.append(5)
  2250. @jump_test(2, 1, [1], event='return', error=(ValueError,
  2251. "can only jump from a 'line' trace event"))
  2252. def test_no_jump_from_return_event(output):
  2253. output.append(1)
  2254. return
  2255. @jump_test(2, 1, [1], event='exception', error=(ValueError,
  2256. "can only jump from a 'line' trace event"))
  2257. def test_no_jump_from_exception_event(output):
  2258. output.append(1)
  2259. 1 / 0
  2260. @jump_test(3, 2, [2, 5], event='return')
  2261. def test_jump_from_yield(output):
  2262. def gen():
  2263. output.append(2)
  2264. yield 3
  2265. next(gen())
  2266. output.append(5)
  2267. @jump_test(2, 3, [1, 3])
  2268. def test_jump_forward_over_listcomp(output):
  2269. output.append(1)
  2270. x = [i for i in range(10)]
  2271. output.append(3)
  2272. # checking for segfaults.
  2273. # See https://github.com/python/cpython/issues/92311
  2274. @jump_test(3, 1, [])
  2275. def test_jump_backward_over_listcomp(output):
  2276. a = 1
  2277. x = [i for i in range(10)]
  2278. c = 3
  2279. @jump_test(8, 2, [2, 7, 2])
  2280. def test_jump_backward_over_listcomp_v2(output):
  2281. flag = False
  2282. output.append(2)
  2283. if flag:
  2284. return
  2285. x = [i for i in range(5)]
  2286. flag = 6
  2287. output.append(7)
  2288. output.append(8)
  2289. @async_jump_test(2, 3, [1, 3])
  2290. async def test_jump_forward_over_async_listcomp(output):
  2291. output.append(1)
  2292. x = [i async for i in asynciter(range(10))]
  2293. output.append(3)
  2294. @async_jump_test(3, 1, [])
  2295. async def test_jump_backward_over_async_listcomp(output):
  2296. a = 1
  2297. x = [i async for i in asynciter(range(10))]
  2298. c = 3
  2299. @async_jump_test(8, 2, [2, 7, 2])
  2300. async def test_jump_backward_over_async_listcomp_v2(output):
  2301. flag = False
  2302. output.append(2)
  2303. if flag:
  2304. return
  2305. x = [i async for i in asynciter(range(5))]
  2306. flag = 6
  2307. output.append(7)
  2308. output.append(8)
  2309. # checking for segfaults.
  2310. @jump_test(3, 7, [], error=(ValueError, "stack"))
  2311. def test_jump_with_null_on_stack_load_global(output):
  2312. a = 1
  2313. print(
  2314. output.append(3)
  2315. )
  2316. output.append(5)
  2317. (
  2318. ( # 7
  2319. a
  2320. +
  2321. 10
  2322. )
  2323. +
  2324. 13
  2325. )
  2326. output.append(15)
  2327. # checking for segfaults.
  2328. @jump_test(4, 8, [], error=(ValueError, "stack"))
  2329. def test_jump_with_null_on_stack_push_null(output):
  2330. a = 1
  2331. f = print
  2332. f(
  2333. output.append(4)
  2334. )
  2335. output.append(6)
  2336. (
  2337. ( # 8
  2338. a
  2339. +
  2340. 11
  2341. )
  2342. +
  2343. 14
  2344. )
  2345. output.append(16)
  2346. # checking for segfaults.
  2347. @jump_test(3, 7, [], error=(ValueError, "stack"))
  2348. def test_jump_with_null_on_stack_load_attr(output):
  2349. a = 1
  2350. list.append(
  2351. output, 3
  2352. )
  2353. output.append(5)
  2354. (
  2355. ( # 7
  2356. a
  2357. +
  2358. 10
  2359. )
  2360. +
  2361. 13
  2362. )
  2363. output.append(15)
  2364. @jump_test(2, 3, [1, 3])
  2365. def test_jump_extended_args_unpack_ex_simple(output):
  2366. output.append(1)
  2367. _, *_, _ = output.append(2) or "Spam"
  2368. output.append(3)
  2369. @jump_test(3, 4, [1, 4, 4, 5])
  2370. def test_jump_extended_args_unpack_ex_tricky(output):
  2371. output.append(1)
  2372. (
  2373. _, *_, _
  2374. ) = output.append(4) or "Spam"
  2375. output.append(5)
  2376. def test_jump_extended_args_for_iter(self):
  2377. # In addition to failing when extended arg handling is broken, this can
  2378. # also hang for a *very* long time:
  2379. source = [
  2380. "def f(output):",
  2381. " output.append(1)",
  2382. " for _ in spam:",
  2383. *(f" output.append({i})" for i in range(3, 100_000)),
  2384. f" output.append(100_000)",
  2385. ]
  2386. namespace = {}
  2387. exec("\n".join(source), namespace)
  2388. f = namespace["f"]
  2389. self.run_test(f, 2, 100_000, [1, 100_000])
  2390. @jump_test(2, 3, [1, 3])
  2391. def test_jump_or_pop(output):
  2392. output.append(1)
  2393. _ = output.append(2) and "Spam"
  2394. output.append(3)
  2395. class TestExtendedArgs(unittest.TestCase):
  2396. def setUp(self):
  2397. self.addCleanup(sys.settrace, sys.gettrace())
  2398. sys.settrace(None)
  2399. def count_traces(self, func):
  2400. # warmup
  2401. for _ in range(20):
  2402. func()
  2403. counts = {"call": 0, "line": 0, "return": 0}
  2404. def trace(frame, event, arg):
  2405. counts[event] += 1
  2406. return trace
  2407. sys.settrace(trace)
  2408. func()
  2409. sys.settrace(None)
  2410. return counts
  2411. def test_trace_unpack_long_sequence(self):
  2412. ns = {}
  2413. code = "def f():\n (" + "y,\n "*300 + ") = range(300)"
  2414. exec(code, ns)
  2415. counts = self.count_traces(ns["f"])
  2416. self.assertEqual(counts, {'call': 1, 'line': 301, 'return': 1})
  2417. def test_trace_lots_of_globals(self):
  2418. code = """if 1:
  2419. def f():
  2420. return (
  2421. {}
  2422. )
  2423. """.format("\n+\n".join(f"var{i}\n" for i in range(1000)))
  2424. ns = {f"var{i}": i for i in range(1000)}
  2425. exec(code, ns)
  2426. counts = self.count_traces(ns["f"])
  2427. self.assertEqual(counts, {'call': 1, 'line': 2000, 'return': 1})
  2428. class TestEdgeCases(unittest.TestCase):
  2429. def setUp(self):
  2430. self.addCleanup(sys.settrace, sys.gettrace())
  2431. sys.settrace(None)
  2432. def test_reentrancy(self):
  2433. def foo(*args):
  2434. ...
  2435. def bar(*args):
  2436. ...
  2437. class A:
  2438. def __call__(self, *args):
  2439. pass
  2440. def __del__(self):
  2441. sys.settrace(bar)
  2442. sys.settrace(A())
  2443. with support.catch_unraisable_exception() as cm:
  2444. sys.settrace(foo)
  2445. self.assertEqual(cm.unraisable.object, A.__del__)
  2446. self.assertIsInstance(cm.unraisable.exc_value, RuntimeError)
  2447. self.assertEqual(sys.gettrace(), foo)
  2448. def test_same_object(self):
  2449. def foo(*args):
  2450. ...
  2451. sys.settrace(foo)
  2452. del foo
  2453. sys.settrace(sys.gettrace())
  2454. if __name__ == "__main__":
  2455. unittest.main()