| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819 |
- # Testing the line trace facility.
- from test import support
- import unittest
- from unittest.mock import MagicMock
- import sys
- import difflib
- import gc
- from functools import wraps
- import asyncio
- from test.support import import_helper
- support.requires_working_socket(module=True)
- class tracecontext:
- """Context manager that traces its enter and exit."""
- def __init__(self, output, value):
- self.output = output
- self.value = value
- def __enter__(self):
- self.output.append(self.value)
- def __exit__(self, *exc_info):
- self.output.append(-self.value)
- class asynctracecontext:
- """Asynchronous context manager that traces its aenter and aexit."""
- def __init__(self, output, value):
- self.output = output
- self.value = value
- async def __aenter__(self):
- self.output.append(self.value)
- async def __aexit__(self, *exc_info):
- self.output.append(-self.value)
- async def asynciter(iterable):
- """Convert an iterable to an asynchronous iterator."""
- for x in iterable:
- yield x
- # A very basic example. If this fails, we're in deep trouble.
- def basic():
- return 1
- basic.events = [(0, 'call'),
- (1, 'line'),
- (1, 'return')]
- # Many of the tests below are tricky because they involve pass statements.
- # If there is implicit control flow around a pass statement (in an except
- # clause or else clause) under what conditions do you set a line number
- # following that clause?
- # Some constructs like "while 0:", "if 0:" or "if 1:...else:..." could be optimized
- # away. Make sure that those lines aren't skipped.
- def arigo_example0():
- x = 1
- del x
- while 0:
- pass
- x = 1
- arigo_example0.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (5, 'line'),
- (5, 'return')]
- def arigo_example1():
- x = 1
- del x
- if 0:
- pass
- x = 1
- arigo_example1.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (5, 'line'),
- (5, 'return')]
- def arigo_example2():
- x = 1
- del x
- if 1:
- x = 1
- else:
- pass
- return None
- arigo_example2.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (7, 'line'),
- (7, 'return')]
- # check that lines consisting of just one instruction get traced:
- def one_instr_line():
- x = 1
- del x
- x = 1
- one_instr_line.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'return')]
- def no_pop_tops(): # 0
- x = 1 # 1
- for a in range(2): # 2
- if a: # 3
- x = 1 # 4
- else: # 5
- x = 1 # 6
- no_pop_tops.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (6, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (2, 'line'),
- (2, 'return')]
- def no_pop_blocks():
- y = 1
- while not y:
- bla
- x = 1
- no_pop_blocks.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (4, 'line'),
- (4, 'return')]
- def called(): # line -3
- x = 1
- def call(): # line 0
- called()
- call.events = [(0, 'call'),
- (1, 'line'),
- (-3, 'call'),
- (-2, 'line'),
- (-2, 'return'),
- (1, 'return')]
- def raises():
- raise Exception
- def test_raise():
- try:
- raises()
- except Exception:
- pass
- test_raise.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (-3, 'call'),
- (-2, 'line'),
- (-2, 'exception'),
- (-2, 'return'),
- (2, 'exception'),
- (3, 'line'),
- (4, 'line'),
- (4, 'return')]
- def _settrace_and_return(tracefunc):
- sys.settrace(tracefunc)
- sys._getframe().f_back.f_trace = tracefunc
- def settrace_and_return(tracefunc):
- _settrace_and_return(tracefunc)
- settrace_and_return.events = [(1, 'return')]
- def _settrace_and_raise(tracefunc):
- sys.settrace(tracefunc)
- sys._getframe().f_back.f_trace = tracefunc
- raise RuntimeError
- def settrace_and_raise(tracefunc):
- try:
- _settrace_and_raise(tracefunc)
- except RuntimeError:
- pass
- settrace_and_raise.events = [(2, 'exception'),
- (3, 'line'),
- (4, 'line'),
- (4, 'return')]
- # implicit return example
- # This test is interesting because of the else: pass
- # part of the code. The code generate for the true
- # part of the if contains a jump past the else branch.
- # The compiler then generates an implicit "return None"
- # Internally, the compiler visits the pass statement
- # and stores its line number for use on the next instruction.
- # The next instruction is the implicit return None.
- def ireturn_example():
- a = 5
- b = 5
- if a == b:
- b = a+1
- else:
- pass
- ireturn_example.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (4, 'return')]
- # Tight loop with while(1) example (SF #765624)
- def tightloop_example():
- items = range(0, 3)
- try:
- i = 0
- while 1:
- b = items[i]; i+=1
- except IndexError:
- pass
- tightloop_example.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (4, 'line'),
- (5, 'line'),
- (4, 'line'),
- (5, 'line'),
- (4, 'line'),
- (5, 'line'),
- (5, 'exception'),
- (6, 'line'),
- (7, 'line'),
- (7, 'return')]
- def tighterloop_example():
- items = range(1, 4)
- try:
- i = 0
- while 1: i = items[i]
- except IndexError:
- pass
- tighterloop_example.events = [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (4, 'line'),
- (4, 'line'),
- (4, 'line'),
- (4, 'exception'),
- (5, 'line'),
- (6, 'line'),
- (6, 'return')]
- def generator_function():
- try:
- yield True
- "continued"
- finally:
- "finally"
- def generator_example():
- # any() will leave the generator before its end
- x = any(generator_function())
- # the following lines were not traced
- for x in range(10):
- y = x
- generator_example.events = ([(0, 'call'),
- (2, 'line'),
- (-6, 'call'),
- (-5, 'line'),
- (-4, 'line'),
- (-4, 'return'),
- (-4, 'call'),
- (-4, 'exception'),
- (-1, 'line'),
- (-1, 'return')] +
- [(5, 'line'), (6, 'line')] * 10 +
- [(5, 'line'), (5, 'return')])
- class Tracer:
- def __init__(self, trace_line_events=None, trace_opcode_events=None):
- self.trace_line_events = trace_line_events
- self.trace_opcode_events = trace_opcode_events
- self.events = []
- def _reconfigure_frame(self, frame):
- if self.trace_line_events is not None:
- frame.f_trace_lines = self.trace_line_events
- if self.trace_opcode_events is not None:
- frame.f_trace_opcodes = self.trace_opcode_events
- def trace(self, frame, event, arg):
- self._reconfigure_frame(frame)
- self.events.append((frame.f_lineno, event))
- return self.trace
- def traceWithGenexp(self, frame, event, arg):
- self._reconfigure_frame(frame)
- (o for o in [1])
- self.events.append((frame.f_lineno, event))
- return self.trace
- class TraceTestCase(unittest.TestCase):
- # Disable gc collection when tracing, otherwise the
- # deallocators may be traced as well.
- def setUp(self):
- self.using_gc = gc.isenabled()
- gc.disable()
- self.addCleanup(sys.settrace, sys.gettrace())
- def tearDown(self):
- if self.using_gc:
- gc.enable()
- @staticmethod
- def make_tracer():
- """Helper to allow test subclasses to configure tracers differently"""
- return Tracer()
- def compare_events(self, line_offset, events, expected_events):
- events = [(l - line_offset, e) for (l, e) in events]
- if events != expected_events:
- self.fail(
- "events did not match expectation:\n" +
- "\n".join(difflib.ndiff([str(x) for x in expected_events],
- [str(x) for x in events])))
- def run_and_compare(self, func, events):
- tracer = self.make_tracer()
- sys.settrace(tracer.trace)
- func()
- sys.settrace(None)
- self.compare_events(func.__code__.co_firstlineno,
- tracer.events, events)
- def run_test(self, func):
- self.run_and_compare(func, func.events)
- def run_test2(self, func):
- tracer = self.make_tracer()
- func(tracer.trace)
- sys.settrace(None)
- self.compare_events(func.__code__.co_firstlineno,
- tracer.events, func.events)
- def test_set_and_retrieve_none(self):
- sys.settrace(None)
- assert sys.gettrace() is None
- def test_set_and_retrieve_func(self):
- def fn(*args):
- pass
- sys.settrace(fn)
- try:
- assert sys.gettrace() is fn
- finally:
- sys.settrace(None)
- def test_01_basic(self):
- self.run_test(basic)
- def test_02_arigo0(self):
- self.run_test(arigo_example0)
- def test_02_arigo1(self):
- self.run_test(arigo_example1)
- def test_02_arigo2(self):
- self.run_test(arigo_example2)
- def test_03_one_instr(self):
- self.run_test(one_instr_line)
- def test_04_no_pop_blocks(self):
- self.run_test(no_pop_blocks)
- def test_05_no_pop_tops(self):
- self.run_test(no_pop_tops)
- def test_06_call(self):
- self.run_test(call)
- def test_07_raise(self):
- self.run_test(test_raise)
- def test_08_settrace_and_return(self):
- self.run_test2(settrace_and_return)
- def test_09_settrace_and_raise(self):
- self.run_test2(settrace_and_raise)
- def test_10_ireturn(self):
- self.run_test(ireturn_example)
- def test_11_tightloop(self):
- self.run_test(tightloop_example)
- def test_12_tighterloop(self):
- self.run_test(tighterloop_example)
- def test_13_genexp(self):
- self.run_test(generator_example)
- # issue1265: if the trace function contains a generator,
- # and if the traced function contains another generator
- # that is not completely exhausted, the trace stopped.
- # Worse: the 'finally' clause was not invoked.
- tracer = self.make_tracer()
- sys.settrace(tracer.traceWithGenexp)
- generator_example()
- sys.settrace(None)
- self.compare_events(generator_example.__code__.co_firstlineno,
- tracer.events, generator_example.events)
- def test_14_onliner_if(self):
- def onliners():
- if True: x=False
- else: x=True
- return 0
- self.run_and_compare(
- onliners,
- [(0, 'call'),
- (1, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_15_loops(self):
- # issue1750076: "while" expression is skipped by debugger
- def for_example():
- for x in range(2):
- pass
- self.run_and_compare(
- for_example,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (1, 'line'),
- (2, 'line'),
- (1, 'line'),
- (1, 'return')])
- def while_example():
- # While expression should be traced on every loop
- x = 2
- while x > 0:
- x -= 1
- self.run_and_compare(
- while_example,
- [(0, 'call'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (3, 'line'),
- (4, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_16_blank_lines(self):
- namespace = {}
- exec("def f():\n" + "\n" * 256 + " pass", namespace)
- self.run_and_compare(
- namespace["f"],
- [(0, 'call'),
- (257, 'line'),
- (257, 'return')])
- def test_17_none_f_trace(self):
- # Issue 20041: fix TypeError when f_trace is set to None.
- def func():
- sys._getframe().f_trace = None
- lineno = 2
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line')])
- def test_18_except_with_name(self):
- def func():
- try:
- try:
- raise Exception
- except Exception as e:
- raise
- x = "Something"
- y = "Something"
- except Exception:
- pass
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (4, 'line'),
- (5, 'line'),
- (8, 'line'),
- (9, 'line'),
- (9, 'return')])
- def test_19_except_with_finally(self):
- def func():
- try:
- try:
- raise Exception
- finally:
- y = "Something"
- except Exception:
- b = 23
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (5, 'line'),
- (6, 'line'),
- (7, 'line'),
- (7, 'return')])
- def test_20_async_for_loop(self):
- class AsyncIteratorWrapper:
- def __init__(self, obj):
- self._it = iter(obj)
- def __aiter__(self):
- return self
- async def __anext__(self):
- try:
- return next(self._it)
- except StopIteration:
- raise StopAsyncIteration
- async def doit_async():
- async for letter in AsyncIteratorWrapper("abc"):
- x = letter
- y = 42
- def run(tracer):
- x = doit_async()
- try:
- sys.settrace(tracer)
- x.send(None)
- finally:
- sys.settrace(None)
- tracer = self.make_tracer()
- events = [
- (0, 'call'),
- (1, 'line'),
- (-12, 'call'),
- (-11, 'line'),
- (-11, 'return'),
- (-9, 'call'),
- (-8, 'line'),
- (-8, 'return'),
- (-6, 'call'),
- (-5, 'line'),
- (-4, 'line'),
- (-4, 'return'),
- (1, 'exception'),
- (2, 'line'),
- (1, 'line'),
- (-6, 'call'),
- (-5, 'line'),
- (-4, 'line'),
- (-4, 'return'),
- (1, 'exception'),
- (2, 'line'),
- (1, 'line'),
- (-6, 'call'),
- (-5, 'line'),
- (-4, 'line'),
- (-4, 'return'),
- (1, 'exception'),
- (2, 'line'),
- (1, 'line'),
- (-6, 'call'),
- (-5, 'line'),
- (-4, 'line'),
- (-4, 'exception'),
- (-3, 'line'),
- (-2, 'line'),
- (-2, 'exception'),
- (-2, 'return'),
- (1, 'exception'),
- (3, 'line'),
- (3, 'return')]
- try:
- run(tracer.trace)
- except Exception:
- pass
- self.compare_events(doit_async.__code__.co_firstlineno,
- tracer.events, events)
- def test_async_for_backwards_jump_has_no_line(self):
- async def arange(n):
- for i in range(n):
- yield i
- async def f():
- async for i in arange(3):
- if i > 100:
- break # should never be traced
- tracer = self.make_tracer()
- coro = f()
- try:
- sys.settrace(tracer.trace)
- coro.send(None)
- except Exception:
- pass
- finally:
- sys.settrace(None)
- events = [
- (0, 'call'),
- (1, 'line'),
- (-3, 'call'),
- (-2, 'line'),
- (-1, 'line'),
- (-1, 'return'),
- (1, 'exception'),
- (2, 'line'),
- (1, 'line'),
- (-1, 'call'),
- (-2, 'line'),
- (-1, 'line'),
- (-1, 'return'),
- (1, 'exception'),
- (2, 'line'),
- (1, 'line'),
- (-1, 'call'),
- (-2, 'line'),
- (-1, 'line'),
- (-1, 'return'),
- (1, 'exception'),
- (2, 'line'),
- (1, 'line'),
- (-1, 'call'),
- (-2, 'line'),
- (-2, 'return'),
- (1, 'exception'),
- (1, 'return'),
- ]
- self.compare_events(f.__code__.co_firstlineno,
- tracer.events, events)
- def test_21_repeated_pass(self):
- def func():
- pass
- pass
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (2, 'return')])
- def test_loop_in_try_except(self):
- # https://bugs.python.org/issue41670
- def func():
- try:
- for i in []: pass
- return 1
- except:
- return 2
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_try_except_no_exception(self):
- def func():
- try:
- 2
- except:
- 4
- else:
- 6
- if False:
- 8
- else:
- 10
- if func.__name__ == 'Fred':
- 12
- finally:
- 14
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (6, 'line'),
- (7, 'line'),
- (10, 'line'),
- (11, 'line'),
- (14, 'line'),
- (14, 'return')])
- def test_try_exception_in_else(self):
- def func():
- try:
- try:
- 3
- except:
- 5
- else:
- 7
- raise Exception
- finally:
- 10
- except:
- 12
- finally:
- 14
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (7, 'line'),
- (8, 'line'),
- (8, 'exception'),
- (10, 'line'),
- (11, 'line'),
- (12, 'line'),
- (14, 'line'),
- (14, 'return')])
- def test_nested_loops(self):
- def func():
- for i in range(2):
- for j in range(2):
- a = i + j
- return a == 1
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (2, 'line'),
- (3, 'line'),
- (2, 'line'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (2, 'line'),
- (3, 'line'),
- (2, 'line'),
- (1, 'line'),
- (4, 'line'),
- (4, 'return')])
- def test_if_break(self):
- def func():
- seq = [1, 0]
- while seq:
- n = seq.pop()
- if n:
- break # line 5
- else:
- n = 99
- return n # line 8
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (8, 'line'),
- (8, 'return')])
- def test_break_through_finally(self):
- def func():
- a, c, d, i = 1, 1, 1, 99
- try:
- for i in range(3):
- try:
- a = 5
- if i > 0:
- break # line 7
- a = 8
- finally:
- c = 10
- except:
- d = 12 # line 12
- assert a == 5 and c == 10 and d == 1 # line 13
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (6, 'line'),
- (8, 'line'),
- (10, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (6, 'line'),
- (7, 'line'),
- (10, 'line'),
- (13, 'line'),
- (13, 'return')])
- def test_continue_through_finally(self):
- def func():
- a, b, c, d, i = 1, 1, 1, 1, 99
- try:
- for i in range(2):
- try:
- a = 5
- if i > 0:
- continue # line 7
- b = 8
- finally:
- c = 10
- except:
- d = 12 # line 12
- assert (a, b, c, d) == (5, 8, 10, 1) # line 13
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (6, 'line'),
- (8, 'line'),
- (10, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (6, 'line'),
- (7, 'line'),
- (10, 'line'),
- (3, 'line'),
- (13, 'line'),
- (13, 'return')])
- def test_return_through_finally(self):
- def func():
- try:
- return 2
- finally:
- 4
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (4, 'line'),
- (4, 'return')])
- def test_try_except_with_wrong_type(self):
- def func():
- try:
- 2/0
- except IndexError:
- 4
- finally:
- return 6
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (2, 'exception'),
- (3, 'line'),
- (6, 'line'),
- (6, 'return')])
- def test_break_to_continue1(self):
- def func():
- TRUE = 1
- x = [1]
- while x:
- x.pop()
- while TRUE:
- break
- continue
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (6, 'line'),
- (7, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_break_to_continue2(self):
- def func():
- TRUE = 1
- x = [1]
- while x:
- x.pop()
- while TRUE:
- break
- else:
- continue
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (6, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_break_to_break(self):
- def func():
- TRUE = 1
- while TRUE:
- while TRUE:
- break
- break
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (5, 'return')])
- def test_nested_ifs(self):
- def func():
- a = b = 1
- if a == 1:
- if b == 1:
- x = 4
- else:
- y = 6
- else:
- z = 8
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (4, 'return')])
- def test_nested_ifs_with_and(self):
- def func():
- if A:
- if B:
- if C:
- if D:
- return False
- else:
- return False
- elif E and F:
- return True
- A = B = True
- C = False
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_nested_try_if(self):
- def func():
- x = "hello"
- try:
- 3/0
- except ZeroDivisionError:
- if x == 'raise':
- raise ValueError() # line 6
- f = 7
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (4, 'line'),
- (5, 'line'),
- (7, 'line'),
- (7, 'return')])
- def test_if_false_in_with(self):
- class C:
- def __enter__(self):
- return self
- def __exit__(*args):
- pass
- def func():
- with C():
- if False:
- pass
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (-5, 'call'),
- (-4, 'line'),
- (-4, 'return'),
- (2, 'line'),
- (1, 'line'),
- (-3, 'call'),
- (-2, 'line'),
- (-2, 'return'),
- (1, 'return')])
- def test_if_false_in_try_except(self):
- def func():
- try:
- if False:
- pass
- except Exception:
- X
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (2, 'return')])
- def test_implicit_return_in_class(self):
- def func():
- class A:
- if 3 < 9:
- a = 1
- else:
- a = 2
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (1, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'return'),
- (1, 'return')])
- def test_try_in_try(self):
- def func():
- try:
- try:
- pass
- except Exception as ex:
- pass
- except Exception:
- pass
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_try_in_try_with_exception(self):
- def func():
- try:
- try:
- raise TypeError
- except ValueError as ex:
- 5
- except TypeError:
- 7
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (4, 'line'),
- (6, 'line'),
- (7, 'line'),
- (7, 'return')])
- def func():
- try:
- try:
- raise ValueError
- except ValueError as ex:
- 5
- except TypeError:
- 7
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (4, 'line'),
- (5, 'line'),
- (5, 'return')])
- def test_if_in_if_in_if(self):
- def func(a=0, p=1, z=1):
- if p:
- if a:
- if z:
- pass
- else:
- pass
- else:
- pass
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (2, 'return')])
- def test_early_exit_with(self):
- class C:
- def __enter__(self):
- return self
- def __exit__(*args):
- pass
- def func_break():
- for i in (1,2):
- with C():
- break
- pass
- def func_return():
- with C():
- return
- self.run_and_compare(func_break,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (-5, 'call'),
- (-4, 'line'),
- (-4, 'return'),
- (3, 'line'),
- (2, 'line'),
- (-3, 'call'),
- (-2, 'line'),
- (-2, 'return'),
- (4, 'line'),
- (4, 'return')])
- self.run_and_compare(func_return,
- [(0, 'call'),
- (1, 'line'),
- (-11, 'call'),
- (-10, 'line'),
- (-10, 'return'),
- (2, 'line'),
- (1, 'line'),
- (-9, 'call'),
- (-8, 'line'),
- (-8, 'return'),
- (1, 'return')])
- def test_flow_converges_on_same_line(self):
- def foo(x):
- if x:
- try:
- 1/(x - 1)
- except ZeroDivisionError:
- pass
- return x
- def func():
- for i in range(2):
- foo(i)
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (-8, 'call'),
- (-7, 'line'),
- (-2, 'line'),
- (-2, 'return'),
- (1, 'line'),
- (2, 'line'),
- (-8, 'call'),
- (-7, 'line'),
- (-6, 'line'),
- (-5, 'line'),
- (-5, 'exception'),
- (-4, 'line'),
- (-3, 'line'),
- (-2, 'line'),
- (-2, 'return'),
- (1, 'line'),
- (1, 'return')])
- def test_no_tracing_of_named_except_cleanup(self):
- def func():
- x = 0
- try:
- 1/x
- except ZeroDivisionError as error:
- if x:
- raise
- return "done"
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (4, 'line'),
- (5, 'line'),
- (7, 'line'),
- (7, 'return')])
- def test_tracing_exception_raised_in_with(self):
- class NullCtx:
- def __enter__(self):
- return self
- def __exit__(self, *excinfo):
- pass
- def func():
- try:
- with NullCtx():
- 1/0
- except ZeroDivisionError:
- pass
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (-5, 'call'),
- (-4, 'line'),
- (-4, 'return'),
- (3, 'line'),
- (3, 'exception'),
- (2, 'line'),
- (-3, 'call'),
- (-2, 'line'),
- (-2, 'return'),
- (4, 'line'),
- (5, 'line'),
- (5, 'return')])
- def test_try_except_star_no_exception(self):
- def func():
- try:
- 2
- except* Exception:
- 4
- else:
- 6
- if False:
- 8
- else:
- 10
- if func.__name__ == 'Fred':
- 12
- finally:
- 14
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (6, 'line'),
- (7, 'line'),
- (10, 'line'),
- (11, 'line'),
- (14, 'line'),
- (14, 'return')])
- def test_try_except_star_named_no_exception(self):
- def func():
- try:
- 2
- except* Exception as e:
- 4
- else:
- 6
- finally:
- 8
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (6, 'line'),
- (8, 'line'),
- (8, 'return')])
- def test_try_except_star_exception_caught(self):
- def func():
- try:
- raise ValueError(2)
- except* ValueError:
- 4
- else:
- 6
- finally:
- 8
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (2, 'exception'),
- (3, 'line'),
- (4, 'line'),
- (8, 'line'),
- (8, 'return')])
- def test_try_except_star_named_exception_caught(self):
- def func():
- try:
- raise ValueError(2)
- except* ValueError as e:
- 4
- else:
- 6
- finally:
- 8
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (2, 'exception'),
- (3, 'line'),
- (4, 'line'),
- (8, 'line'),
- (8, 'return')])
- def test_try_except_star_exception_not_caught(self):
- def func():
- try:
- try:
- raise ValueError(3)
- except* TypeError:
- 5
- except ValueError:
- 7
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (4, 'line'),
- (6, 'line'),
- (7, 'line'),
- (7, 'return')])
- def test_try_except_star_named_exception_not_caught(self):
- def func():
- try:
- try:
- raise ValueError(3)
- except* TypeError as e:
- 5
- except ValueError:
- 7
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (4, 'line'),
- (6, 'line'),
- (7, 'line'),
- (7, 'return')])
- def test_try_except_star_nested(self):
- def func():
- try:
- try:
- raise ExceptionGroup(
- 'eg',
- [ValueError(5), TypeError('bad type')])
- except* TypeError as e:
- 7
- except* OSError:
- 9
- except* ValueError:
- raise
- except* ValueError:
- try:
- raise TypeError(14)
- except* OSError:
- 16
- except* TypeError as e:
- 18
- return 0
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (4, 'line'),
- (5, 'line'),
- (3, 'line'),
- (3, 'exception'),
- (6, 'line'),
- (7, 'line'),
- (8, 'line'),
- (10, 'line'),
- (11, 'line'),
- (12, 'line'),
- (13, 'line'),
- (14, 'line'),
- (14, 'exception'),
- (15, 'line'),
- (17, 'line'),
- (18, 'line'),
- (19, 'line'),
- (19, 'return')])
- def test_notrace_lambda(self):
- #Regression test for issue 46314
- def func():
- 1
- lambda x: 2
- 3
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'return')])
- def test_class_creation_with_docstrings(self):
- def func():
- class Class_1:
- ''' the docstring. 2'''
- def __init__(self):
- ''' Another docstring. 4'''
- self.a = 5
- self.run_and_compare(func,
- [(0, 'call'),
- (1, 'line'),
- (1, 'call'),
- (1, 'line'),
- (2, 'line'),
- (3, 'line'),
- (3, 'return'),
- (1, 'return')])
- @support.cpython_only
- def test_no_line_event_after_creating_generator(self):
- # Spurious line events before call events only show up with C tracer
- # Skip this test if the _testcapi module isn't available.
- _testcapi = import_helper.import_module('_testcapi')
- def gen():
- yield 1
- def func():
- for _ in (
- gen()
- ):
- pass
- EXPECTED_EVENTS = [
- (0, 'call'),
- (2, 'line'),
- (1, 'line'),
- (-3, 'call'),
- (-2, 'line'),
- (-2, 'return'),
- (4, 'line'),
- (1, 'line'),
- (-2, 'call'),
- (-2, 'return'),
- (1, 'return'),
- ]
- # C level events should be the same as expected and the same as Python level.
- events = []
- # Turning on and off tracing must be on same line to avoid unwanted LINE events.
- _testcapi.settrace_to_record(events); func(); sys.settrace(None)
- start_line = func.__code__.co_firstlineno
- events = [
- (line-start_line, EVENT_NAMES[what])
- for (what, line, arg) in events
- ]
- self.assertEqual(events, EXPECTED_EVENTS)
- self.run_and_compare(func, EXPECTED_EVENTS)
- def test_very_large_function(self):
- # There is a separate code path when the number of lines > (1 << 15).
- d = {}
- exec("""def f(): # line 0
- x = 0 # line 1
- y = 1 # line 2
- %s # lines 3 through (1 << 16)
- x += 1 #
- return""" % ('\n' * (1 << 16),), d)
- f = d['f']
- EXPECTED_EVENTS = [
- (0, 'call'),
- (1, 'line'),
- (2, 'line'),
- (65540, 'line'),
- (65541, 'line'),
- (65541, 'return'),
- ]
- self.run_and_compare(f, EXPECTED_EVENTS)
- EVENT_NAMES = [
- 'call',
- 'exception',
- 'line',
- 'return'
- ]
- class SkipLineEventsTraceTestCase(TraceTestCase):
- """Repeat the trace tests, but with per-line events skipped"""
- def compare_events(self, line_offset, events, expected_events):
- skip_line_events = [e for e in expected_events if e[1] != 'line']
- super().compare_events(line_offset, events, skip_line_events)
- @staticmethod
- def make_tracer():
- return Tracer(trace_line_events=False)
- @support.cpython_only
- class TraceOpcodesTestCase(TraceTestCase):
- """Repeat the trace tests, but with per-opcodes events enabled"""
- def compare_events(self, line_offset, events, expected_events):
- skip_opcode_events = [e for e in events if e[1] != 'opcode']
- if len(events) > 1:
- self.assertLess(len(skip_opcode_events), len(events),
- msg="No 'opcode' events received by the tracer")
- super().compare_events(line_offset, skip_opcode_events, expected_events)
- @staticmethod
- def make_tracer():
- return Tracer(trace_opcode_events=True)
- class RaisingTraceFuncTestCase(unittest.TestCase):
- def setUp(self):
- self.addCleanup(sys.settrace, sys.gettrace())
- def trace(self, frame, event, arg):
- """A trace function that raises an exception in response to a
- specific trace event."""
- if event == self.raiseOnEvent:
- raise ValueError # just something that isn't RuntimeError
- else:
- return self.trace
- def f(self):
- """The function to trace; raises an exception if that's the case
- we're testing, so that the 'exception' trace event fires."""
- if self.raiseOnEvent == 'exception':
- x = 0
- y = 1/x
- else:
- return 1
- def run_test_for_event(self, event):
- """Tests that an exception raised in response to the given event is
- handled OK."""
- self.raiseOnEvent = event
- try:
- for i in range(sys.getrecursionlimit() + 1):
- sys.settrace(self.trace)
- try:
- self.f()
- except ValueError:
- pass
- else:
- self.fail("exception not raised!")
- except RuntimeError:
- self.fail("recursion counter not reset")
- # Test the handling of exceptions raised by each kind of trace event.
- def test_call(self):
- self.run_test_for_event('call')
- def test_line(self):
- self.run_test_for_event('line')
- def test_return(self):
- self.run_test_for_event('return')
- def test_exception(self):
- self.run_test_for_event('exception')
- def test_trash_stack(self):
- def f():
- for i in range(5):
- print(i) # line tracing will raise an exception at this line
- def g(frame, why, extra):
- if (why == 'line' and
- frame.f_lineno == f.__code__.co_firstlineno + 2):
- raise RuntimeError("i am crashing")
- return g
- sys.settrace(g)
- try:
- f()
- except RuntimeError:
- # the test is really that this doesn't segfault:
- import gc
- gc.collect()
- else:
- self.fail("exception not propagated")
- def test_exception_arguments(self):
- def f():
- x = 0
- # this should raise an error
- x.no_such_attr
- def g(frame, event, arg):
- if (event == 'exception'):
- type, exception, trace = arg
- self.assertIsInstance(exception, Exception)
- return g
- existing = sys.gettrace()
- try:
- sys.settrace(g)
- try:
- f()
- except AttributeError:
- # this is expected
- pass
- finally:
- sys.settrace(existing)
- def test_line_event_raises_before_opcode_event(self):
- exception = ValueError("BOOM!")
- def trace(frame, event, arg):
- if event == "line":
- raise exception
- frame.f_trace_opcodes = True
- return trace
- def f():
- pass
- with self.assertRaises(ValueError) as caught:
- sys.settrace(trace)
- f()
- self.assertIs(caught.exception, exception)
- # 'Jump' tests: assigning to frame.f_lineno within a trace function
- # moves the execution position - it's how debuggers implement a Jump
- # command (aka. "Set next statement").
- class JumpTracer:
- """Defines a trace function that jumps from one place to another."""
- def __init__(self, function, jumpFrom, jumpTo, event='line',
- decorated=False):
- self.code = function.__code__
- self.jumpFrom = jumpFrom
- self.jumpTo = jumpTo
- self.event = event
- self.firstLine = None if decorated else self.code.co_firstlineno
- self.done = False
- def trace(self, frame, event, arg):
- if self.done:
- return
- # frame.f_code.co_firstlineno is the first line of the decorator when
- # 'function' is decorated and the decorator may be written using
- # multiple physical lines when it is too long. Use the first line
- # trace event in 'function' to find the first line of 'function'.
- if (self.firstLine is None and frame.f_code == self.code and
- event == 'line'):
- self.firstLine = frame.f_lineno - 1
- if (event == self.event and self.firstLine is not None and
- frame.f_lineno == self.firstLine + self.jumpFrom):
- f = frame
- while f is not None and f.f_code != self.code:
- f = f.f_back
- if f is not None:
- # Cope with non-integer self.jumpTo (because of
- # no_jump_to_non_integers below).
- try:
- frame.f_lineno = self.firstLine + self.jumpTo
- except TypeError:
- frame.f_lineno = self.jumpTo
- self.done = True
- return self.trace
- # This verifies the line-numbers-must-be-integers rule.
- def no_jump_to_non_integers(output):
- try:
- output.append(2)
- except ValueError as e:
- output.append('integer' in str(e))
- # This verifies that you can't set f_lineno via _getframe or similar
- # trickery.
- def no_jump_without_trace_function():
- try:
- previous_frame = sys._getframe().f_back
- previous_frame.f_lineno = previous_frame.f_lineno
- except ValueError as e:
- # This is the exception we wanted; make sure the error message
- # talks about trace functions.
- if 'trace' not in str(e):
- raise
- else:
- # Something's wrong - the expected exception wasn't raised.
- raise AssertionError("Trace-function-less jump failed to fail")
- class JumpTestCase(unittest.TestCase):
- def setUp(self):
- self.addCleanup(sys.settrace, sys.gettrace())
- sys.settrace(None)
- def compare_jump_output(self, expected, received):
- if received != expected:
- self.fail( "Outputs don't match:\n" +
- "Expected: " + repr(expected) + "\n" +
- "Received: " + repr(received))
- def run_test(self, func, jumpFrom, jumpTo, expected, error=None,
- event='line', decorated=False):
- tracer = JumpTracer(func, jumpFrom, jumpTo, event, decorated)
- sys.settrace(tracer.trace)
- output = []
- if error is None:
- func(output)
- else:
- with self.assertRaisesRegex(*error):
- func(output)
- sys.settrace(None)
- self.compare_jump_output(expected, output)
- def run_async_test(self, func, jumpFrom, jumpTo, expected, error=None,
- event='line', decorated=False):
- tracer = JumpTracer(func, jumpFrom, jumpTo, event, decorated)
- sys.settrace(tracer.trace)
- output = []
- if error is None:
- asyncio.run(func(output))
- else:
- with self.assertRaisesRegex(*error):
- asyncio.run(func(output))
- sys.settrace(None)
- asyncio.set_event_loop_policy(None)
- self.compare_jump_output(expected, output)
- def jump_test(jumpFrom, jumpTo, expected, error=None, event='line'):
- """Decorator that creates a test that makes a jump
- from one place to another in the following code.
- """
- def decorator(func):
- @wraps(func)
- def test(self):
- self.run_test(func, jumpFrom, jumpTo, expected,
- error=error, event=event, decorated=True)
- return test
- return decorator
- def async_jump_test(jumpFrom, jumpTo, expected, error=None, event='line'):
- """Decorator that creates a test that makes a jump
- from one place to another in the following asynchronous code.
- """
- def decorator(func):
- @wraps(func)
- def test(self):
- self.run_async_test(func, jumpFrom, jumpTo, expected,
- error=error, event=event, decorated=True)
- return test
- return decorator
- ## The first set of 'jump' tests are for things that are allowed:
- @jump_test(1, 3, [3])
- def test_jump_simple_forwards(output):
- output.append(1)
- output.append(2)
- output.append(3)
- @jump_test(2, 1, [1, 1, 2])
- def test_jump_simple_backwards(output):
- output.append(1)
- output.append(2)
- @jump_test(3, 5, [2, 5])
- def test_jump_out_of_block_forwards(output):
- for i in 1, 2:
- output.append(2)
- for j in [3]: # Also tests jumping over a block
- output.append(4)
- output.append(5)
- @jump_test(6, 1, [1, 3, 5, 1, 3, 5, 6, 7])
- def test_jump_out_of_block_backwards(output):
- output.append(1)
- for i in [1]:
- output.append(3)
- for j in [2]: # Also tests jumping over a block
- output.append(5)
- output.append(6)
- output.append(7)
- @async_jump_test(4, 5, [3, 5])
- async def test_jump_out_of_async_for_block_forwards(output):
- for i in [1]:
- async for i in asynciter([1, 2]):
- output.append(3)
- output.append(4)
- output.append(5)
- @async_jump_test(5, 2, [2, 4, 2, 4, 5, 6])
- async def test_jump_out_of_async_for_block_backwards(output):
- for i in [1]:
- output.append(2)
- async for i in asynciter([1]):
- output.append(4)
- output.append(5)
- output.append(6)
- @jump_test(1, 2, [3])
- def test_jump_to_codeless_line(output):
- output.append(1)
- # Jumping to this line should skip to the next one.
- output.append(3)
- @jump_test(2, 2, [1, 2, 3])
- def test_jump_to_same_line(output):
- output.append(1)
- output.append(2)
- output.append(3)
- # Tests jumping within a finally block, and over one.
- @jump_test(4, 9, [2, 9])
- def test_jump_in_nested_finally(output):
- try:
- output.append(2)
- finally:
- output.append(4)
- try:
- output.append(6)
- finally:
- output.append(8)
- output.append(9)
- @jump_test(6, 7, [2, 7], (ZeroDivisionError, ''))
- def test_jump_in_nested_finally_2(output):
- try:
- output.append(2)
- 1/0
- return
- finally:
- output.append(6)
- output.append(7)
- output.append(8)
- @jump_test(6, 11, [2, 11], (ZeroDivisionError, ''))
- def test_jump_in_nested_finally_3(output):
- try:
- output.append(2)
- 1/0
- return
- finally:
- output.append(6)
- try:
- output.append(8)
- finally:
- output.append(10)
- output.append(11)
- output.append(12)
- @jump_test(5, 11, [2, 4], (ValueError, 'exception'))
- def test_no_jump_over_return_try_finally_in_finally_block(output):
- try:
- output.append(2)
- finally:
- output.append(4)
- output.append(5)
- return
- try:
- output.append(8)
- finally:
- output.append(10)
- pass
- output.append(12)
- @jump_test(3, 4, [1], (ValueError, 'after'))
- def test_no_jump_infinite_while_loop(output):
- output.append(1)
- while True:
- output.append(3)
- output.append(4)
- @jump_test(2, 4, [4, 4])
- def test_jump_forwards_into_while_block(output):
- i = 1
- output.append(2)
- while i <= 2:
- output.append(4)
- i += 1
- @jump_test(5, 3, [3, 3, 3, 5])
- def test_jump_backwards_into_while_block(output):
- i = 1
- while i <= 2:
- output.append(3)
- i += 1
- output.append(5)
- @jump_test(2, 3, [1, 3])
- def test_jump_forwards_out_of_with_block(output):
- with tracecontext(output, 1):
- output.append(2)
- output.append(3)
- @async_jump_test(2, 3, [1, 3])
- async def test_jump_forwards_out_of_async_with_block(output):
- async with asynctracecontext(output, 1):
- output.append(2)
- output.append(3)
- @jump_test(3, 1, [1, 2, 1, 2, 3, -2])
- def test_jump_backwards_out_of_with_block(output):
- output.append(1)
- with tracecontext(output, 2):
- output.append(3)
- @async_jump_test(3, 1, [1, 2, 1, 2, 3, -2])
- async def test_jump_backwards_out_of_async_with_block(output):
- output.append(1)
- async with asynctracecontext(output, 2):
- output.append(3)
- @jump_test(2, 5, [5])
- def test_jump_forwards_out_of_try_finally_block(output):
- try:
- output.append(2)
- finally:
- output.append(4)
- output.append(5)
- @jump_test(3, 1, [1, 1, 3, 5])
- def test_jump_backwards_out_of_try_finally_block(output):
- output.append(1)
- try:
- output.append(3)
- finally:
- output.append(5)
- @jump_test(2, 6, [6])
- def test_jump_forwards_out_of_try_except_block(output):
- try:
- output.append(2)
- except:
- output.append(4)
- raise
- output.append(6)
- @jump_test(3, 1, [1, 1, 3])
- def test_jump_backwards_out_of_try_except_block(output):
- output.append(1)
- try:
- output.append(3)
- except:
- output.append(5)
- raise
- @jump_test(5, 7, [4, 7, 8])
- def test_jump_between_except_blocks(output):
- try:
- 1/0
- except ZeroDivisionError:
- output.append(4)
- output.append(5)
- except FloatingPointError:
- output.append(7)
- output.append(8)
- @jump_test(5, 7, [4, 7, 8])
- def test_jump_from_except_to_finally(output):
- try:
- 1/0
- except ZeroDivisionError:
- output.append(4)
- output.append(5)
- finally:
- output.append(7)
- output.append(8)
- @jump_test(5, 6, [4, 6, 7])
- def test_jump_within_except_block(output):
- try:
- 1/0
- except:
- output.append(4)
- output.append(5)
- output.append(6)
- output.append(7)
- @jump_test(6, 1, [1, 5, 1, 5])
- def test_jump_over_try_except(output):
- output.append(1)
- try:
- 1 / 0
- except ZeroDivisionError as e:
- output.append(5)
- x = 42 # has to be a two-instruction block
- @jump_test(2, 4, [1, 4, 5, -4])
- def test_jump_across_with(output):
- output.append(1)
- with tracecontext(output, 2):
- output.append(3)
- with tracecontext(output, 4):
- output.append(5)
- @async_jump_test(2, 4, [1, 4, 5, -4])
- async def test_jump_across_async_with(output):
- output.append(1)
- async with asynctracecontext(output, 2):
- output.append(3)
- async with asynctracecontext(output, 4):
- output.append(5)
- @jump_test(4, 5, [1, 3, 5, 6])
- def test_jump_out_of_with_block_within_for_block(output):
- output.append(1)
- for i in [1]:
- with tracecontext(output, 3):
- output.append(4)
- output.append(5)
- output.append(6)
- @async_jump_test(4, 5, [1, 3, 5, 6])
- async def test_jump_out_of_async_with_block_within_for_block(output):
- output.append(1)
- for i in [1]:
- async with asynctracecontext(output, 3):
- output.append(4)
- output.append(5)
- output.append(6)
- @jump_test(4, 5, [1, 2, 3, 5, -2, 6])
- def test_jump_out_of_with_block_within_with_block(output):
- output.append(1)
- with tracecontext(output, 2):
- with tracecontext(output, 3):
- output.append(4)
- output.append(5)
- output.append(6)
- @async_jump_test(4, 5, [1, 2, 3, 5, -2, 6])
- async def test_jump_out_of_async_with_block_within_with_block(output):
- output.append(1)
- with tracecontext(output, 2):
- async with asynctracecontext(output, 3):
- output.append(4)
- output.append(5)
- output.append(6)
- @jump_test(5, 6, [2, 4, 6, 7])
- def test_jump_out_of_with_block_within_finally_block(output):
- try:
- output.append(2)
- finally:
- with tracecontext(output, 4):
- output.append(5)
- output.append(6)
- output.append(7)
- @async_jump_test(5, 6, [2, 4, 6, 7])
- async def test_jump_out_of_async_with_block_within_finally_block(output):
- try:
- output.append(2)
- finally:
- async with asynctracecontext(output, 4):
- output.append(5)
- output.append(6)
- output.append(7)
- @jump_test(8, 11, [1, 3, 5, 11, 12])
- def test_jump_out_of_complex_nested_blocks(output):
- output.append(1)
- for i in [1]:
- output.append(3)
- for j in [1, 2]:
- output.append(5)
- try:
- for k in [1, 2]:
- output.append(8)
- finally:
- output.append(10)
- output.append(11)
- output.append(12)
- @jump_test(3, 5, [1, 2, 5])
- def test_jump_out_of_with_assignment(output):
- output.append(1)
- with tracecontext(output, 2) \
- as x:
- output.append(4)
- output.append(5)
- @async_jump_test(3, 5, [1, 2, 5])
- async def test_jump_out_of_async_with_assignment(output):
- output.append(1)
- async with asynctracecontext(output, 2) \
- as x:
- output.append(4)
- output.append(5)
- @jump_test(3, 6, [1, 6, 8, 9])
- def test_jump_over_return_in_try_finally_block(output):
- output.append(1)
- try:
- output.append(3)
- if not output: # always false
- return
- output.append(6)
- finally:
- output.append(8)
- output.append(9)
- @jump_test(5, 8, [1, 3, 8, 10, 11, 13])
- def test_jump_over_break_in_try_finally_block(output):
- output.append(1)
- while True:
- output.append(3)
- try:
- output.append(5)
- if not output: # always false
- break
- output.append(8)
- finally:
- output.append(10)
- output.append(11)
- break
- output.append(13)
- @jump_test(1, 7, [7, 8])
- def test_jump_over_for_block_before_else(output):
- output.append(1)
- if not output: # always false
- for i in [3]:
- output.append(4)
- else:
- output.append(6)
- output.append(7)
- output.append(8)
- @async_jump_test(1, 7, [7, 8])
- async def test_jump_over_async_for_block_before_else(output):
- output.append(1)
- if not output: # always false
- async for i in asynciter([3]):
- output.append(4)
- else:
- output.append(6)
- output.append(7)
- output.append(8)
- # The second set of 'jump' tests are for things that are not allowed:
- @jump_test(2, 3, [1], (ValueError, 'after'))
- def test_no_jump_too_far_forwards(output):
- output.append(1)
- output.append(2)
- @jump_test(2, -2, [1], (ValueError, 'before'))
- def test_no_jump_too_far_backwards(output):
- output.append(1)
- output.append(2)
- # Test each kind of 'except' line.
- @jump_test(2, 3, [4], (ValueError, 'except'))
- def test_no_jump_to_except_1(output):
- try:
- output.append(2)
- except:
- output.append(4)
- raise
- @jump_test(2, 3, [4], (ValueError, 'except'))
- def test_no_jump_to_except_2(output):
- try:
- output.append(2)
- except ValueError:
- output.append(4)
- raise
- @jump_test(2, 3, [4], (ValueError, 'except'))
- def test_no_jump_to_except_3(output):
- try:
- output.append(2)
- except ValueError as e:
- output.append(4)
- raise e
- @jump_test(2, 3, [4], (ValueError, 'except'))
- def test_no_jump_to_except_4(output):
- try:
- output.append(2)
- except (ValueError, RuntimeError) as e:
- output.append(4)
- raise e
- @jump_test(1, 3, [], (ValueError, 'into'))
- def test_no_jump_forwards_into_for_block(output):
- output.append(1)
- for i in 1, 2:
- output.append(3)
- @async_jump_test(1, 3, [], (ValueError, 'into'))
- async def test_no_jump_forwards_into_async_for_block(output):
- output.append(1)
- async for i in asynciter([1, 2]):
- output.append(3)
- pass
- @jump_test(3, 2, [2, 2], (ValueError, 'into'))
- def test_no_jump_backwards_into_for_block(output):
- for i in 1, 2:
- output.append(2)
- output.append(3)
- @async_jump_test(3, 2, [2, 2], (ValueError, "can't jump into the body of a for loop"))
- async def test_no_jump_backwards_into_async_for_block(output):
- async for i in asynciter([1, 2]):
- output.append(2)
- output.append(3)
- @jump_test(1, 3, [], (ValueError, 'stack'))
- def test_no_jump_forwards_into_with_block(output):
- output.append(1)
- with tracecontext(output, 2):
- output.append(3)
- @async_jump_test(1, 3, [], (ValueError, 'stack'))
- async def test_no_jump_forwards_into_async_with_block(output):
- output.append(1)
- async with asynctracecontext(output, 2):
- output.append(3)
- @jump_test(3, 2, [1, 2, -1], (ValueError, 'stack'))
- def test_no_jump_backwards_into_with_block(output):
- with tracecontext(output, 1):
- output.append(2)
- output.append(3)
- @async_jump_test(3, 2, [1, 2, -1], (ValueError, 'stack'))
- async def test_no_jump_backwards_into_async_with_block(output):
- async with asynctracecontext(output, 1):
- output.append(2)
- output.append(3)
- @jump_test(1, 3, [3, 5])
- def test_jump_forwards_into_try_finally_block(output):
- output.append(1)
- try:
- output.append(3)
- finally:
- output.append(5)
- @jump_test(5, 2, [2, 4, 2, 4, 5])
- def test_jump_backwards_into_try_finally_block(output):
- try:
- output.append(2)
- finally:
- output.append(4)
- output.append(5)
- @jump_test(1, 3, [3])
- def test_jump_forwards_into_try_except_block(output):
- output.append(1)
- try:
- output.append(3)
- except:
- output.append(5)
- raise
- @jump_test(6, 2, [2, 2, 6])
- def test_jump_backwards_into_try_except_block(output):
- try:
- output.append(2)
- except:
- output.append(4)
- raise
- output.append(6)
- # 'except' with a variable creates an implicit finally block
- @jump_test(5, 7, [4, 7, 8])
- def test_jump_between_except_blocks_2(output):
- try:
- 1/0
- except ZeroDivisionError:
- output.append(4)
- output.append(5)
- except FloatingPointError as e:
- output.append(7)
- output.append(8)
- @jump_test(1, 5, [5])
- def test_jump_into_finally_block(output):
- output.append(1)
- try:
- output.append(3)
- finally:
- output.append(5)
- @jump_test(3, 6, [2, 6, 7])
- def test_jump_into_finally_block_from_try_block(output):
- try:
- output.append(2)
- output.append(3)
- finally: # still executed if the jump is failed
- output.append(5)
- output.append(6)
- output.append(7)
- @jump_test(5, 1, [1, 3, 1, 3, 5])
- def test_jump_out_of_finally_block(output):
- output.append(1)
- try:
- output.append(3)
- finally:
- output.append(5)
- @jump_test(1, 5, [], (ValueError, "can't jump into an 'except' block as there's no exception"))
- def test_no_jump_into_bare_except_block(output):
- output.append(1)
- try:
- output.append(3)
- except:
- output.append(5)
- @jump_test(1, 5, [], (ValueError, "can't jump into an 'except' block as there's no exception"))
- def test_no_jump_into_qualified_except_block(output):
- output.append(1)
- try:
- output.append(3)
- except Exception:
- output.append(5)
- @jump_test(3, 6, [2, 5, 6], (ValueError, "can't jump into an 'except' block as there's no exception"))
- def test_no_jump_into_bare_except_block_from_try_block(output):
- try:
- output.append(2)
- output.append(3)
- except: # executed if the jump is failed
- output.append(5)
- output.append(6)
- raise
- output.append(8)
- @jump_test(3, 6, [2], (ValueError, "can't jump into an 'except' block as there's no exception"))
- def test_no_jump_into_qualified_except_block_from_try_block(output):
- try:
- output.append(2)
- output.append(3)
- except ZeroDivisionError:
- output.append(5)
- output.append(6)
- raise
- output.append(8)
- @jump_test(7, 1, [1, 3, 6, 1, 3, 6, 7])
- def test_jump_out_of_bare_except_block(output):
- output.append(1)
- try:
- output.append(3)
- 1/0
- except:
- output.append(6)
- output.append(7)
- @jump_test(7, 1, [1, 3, 6, 1, 3, 6, 7])
- def test_jump_out_of_qualified_except_block(output):
- output.append(1)
- try:
- output.append(3)
- 1/0
- except Exception:
- output.append(6)
- output.append(7)
- @jump_test(3, 5, [1, 2, 5, -2])
- def test_jump_between_with_blocks(output):
- output.append(1)
- with tracecontext(output, 2):
- output.append(3)
- with tracecontext(output, 4):
- output.append(5)
- @async_jump_test(3, 5, [1, 2, 5, -2])
- async def test_jump_between_async_with_blocks(output):
- output.append(1)
- async with asynctracecontext(output, 2):
- output.append(3)
- async with asynctracecontext(output, 4):
- output.append(5)
- @jump_test(5, 7, [2, 4], (ValueError, "after"))
- def test_no_jump_over_return_out_of_finally_block(output):
- try:
- output.append(2)
- finally:
- output.append(4)
- output.append(5)
- return
- output.append(7)
- @jump_test(7, 4, [1, 6], (ValueError, 'into'))
- def test_no_jump_into_for_block_before_else(output):
- output.append(1)
- if not output: # always false
- for i in [3]:
- output.append(4)
- else:
- output.append(6)
- output.append(7)
- output.append(8)
- @async_jump_test(7, 4, [1, 6], (ValueError, 'into'))
- async def test_no_jump_into_async_for_block_before_else(output):
- output.append(1)
- if not output: # always false
- async for i in asynciter([3]):
- output.append(4)
- else:
- output.append(6)
- output.append(7)
- output.append(8)
- def test_no_jump_to_non_integers(self):
- self.run_test(no_jump_to_non_integers, 2, "Spam", [True])
- def test_no_jump_without_trace_function(self):
- # Must set sys.settrace(None) in setUp(), else condition is not
- # triggered.
- no_jump_without_trace_function()
- def test_large_function(self):
- d = {}
- exec("""def f(output): # line 0
- x = 0 # line 1
- y = 1 # line 2
- ''' # line 3
- %s # lines 4-1004
- ''' # line 1005
- x += 1 # line 1006
- output.append(x) # line 1007
- return""" % ('\n' * 1000,), d)
- f = d['f']
- self.run_test(f, 2, 1007, [0])
- def test_jump_to_firstlineno(self):
- # This tests that PDB can jump back to the first line in a
- # file. See issue #1689458. It can only be triggered in a
- # function call if the function is defined on a single line.
- code = compile("""
- # Comments don't count.
- output.append(2) # firstlineno is here.
- output.append(3)
- output.append(4)
- """, "<fake module>", "exec")
- class fake_function:
- __code__ = code
- tracer = JumpTracer(fake_function, 4, 1)
- sys.settrace(tracer.trace)
- namespace = {"output": []}
- exec(code, namespace)
- sys.settrace(None)
- self.compare_jump_output([2, 3, 2, 3, 4], namespace["output"])
- @jump_test(2, 3, [1], event='call', error=(ValueError, "can't jump from"
- " the 'call' trace event of a new frame"))
- def test_no_jump_from_call(output):
- output.append(1)
- def nested():
- output.append(3)
- nested()
- output.append(5)
- @jump_test(2, 1, [1], event='return', error=(ValueError,
- "can only jump from a 'line' trace event"))
- def test_no_jump_from_return_event(output):
- output.append(1)
- return
- @jump_test(2, 1, [1], event='exception', error=(ValueError,
- "can only jump from a 'line' trace event"))
- def test_no_jump_from_exception_event(output):
- output.append(1)
- 1 / 0
- @jump_test(3, 2, [2, 5], event='return')
- def test_jump_from_yield(output):
- def gen():
- output.append(2)
- yield 3
- next(gen())
- output.append(5)
- @jump_test(2, 3, [1, 3])
- def test_jump_forward_over_listcomp(output):
- output.append(1)
- x = [i for i in range(10)]
- output.append(3)
- # checking for segfaults.
- # See https://github.com/python/cpython/issues/92311
- @jump_test(3, 1, [])
- def test_jump_backward_over_listcomp(output):
- a = 1
- x = [i for i in range(10)]
- c = 3
- @jump_test(8, 2, [2, 7, 2])
- def test_jump_backward_over_listcomp_v2(output):
- flag = False
- output.append(2)
- if flag:
- return
- x = [i for i in range(5)]
- flag = 6
- output.append(7)
- output.append(8)
- @async_jump_test(2, 3, [1, 3])
- async def test_jump_forward_over_async_listcomp(output):
- output.append(1)
- x = [i async for i in asynciter(range(10))]
- output.append(3)
- @async_jump_test(3, 1, [])
- async def test_jump_backward_over_async_listcomp(output):
- a = 1
- x = [i async for i in asynciter(range(10))]
- c = 3
- @async_jump_test(8, 2, [2, 7, 2])
- async def test_jump_backward_over_async_listcomp_v2(output):
- flag = False
- output.append(2)
- if flag:
- return
- x = [i async for i in asynciter(range(5))]
- flag = 6
- output.append(7)
- output.append(8)
- # checking for segfaults.
- @jump_test(3, 7, [], error=(ValueError, "stack"))
- def test_jump_with_null_on_stack_load_global(output):
- a = 1
- print(
- output.append(3)
- )
- output.append(5)
- (
- ( # 7
- a
- +
- 10
- )
- +
- 13
- )
- output.append(15)
- # checking for segfaults.
- @jump_test(4, 8, [], error=(ValueError, "stack"))
- def test_jump_with_null_on_stack_push_null(output):
- a = 1
- f = print
- f(
- output.append(4)
- )
- output.append(6)
- (
- ( # 8
- a
- +
- 11
- )
- +
- 14
- )
- output.append(16)
- # checking for segfaults.
- @jump_test(3, 7, [], error=(ValueError, "stack"))
- def test_jump_with_null_on_stack_load_attr(output):
- a = 1
- list.append(
- output, 3
- )
- output.append(5)
- (
- ( # 7
- a
- +
- 10
- )
- +
- 13
- )
- output.append(15)
- @jump_test(2, 3, [1, 3])
- def test_jump_extended_args_unpack_ex_simple(output):
- output.append(1)
- _, *_, _ = output.append(2) or "Spam"
- output.append(3)
- @jump_test(3, 4, [1, 4, 4, 5])
- def test_jump_extended_args_unpack_ex_tricky(output):
- output.append(1)
- (
- _, *_, _
- ) = output.append(4) or "Spam"
- output.append(5)
- def test_jump_extended_args_for_iter(self):
- # In addition to failing when extended arg handling is broken, this can
- # also hang for a *very* long time:
- source = [
- "def f(output):",
- " output.append(1)",
- " for _ in spam:",
- *(f" output.append({i})" for i in range(3, 100_000)),
- f" output.append(100_000)",
- ]
- namespace = {}
- exec("\n".join(source), namespace)
- f = namespace["f"]
- self.run_test(f, 2, 100_000, [1, 100_000])
- @jump_test(2, 3, [1, 3])
- def test_jump_or_pop(output):
- output.append(1)
- _ = output.append(2) and "Spam"
- output.append(3)
- class TestExtendedArgs(unittest.TestCase):
- def setUp(self):
- self.addCleanup(sys.settrace, sys.gettrace())
- sys.settrace(None)
- def count_traces(self, func):
- # warmup
- for _ in range(20):
- func()
- counts = {"call": 0, "line": 0, "return": 0}
- def trace(frame, event, arg):
- counts[event] += 1
- return trace
- sys.settrace(trace)
- func()
- sys.settrace(None)
- return counts
- def test_trace_unpack_long_sequence(self):
- ns = {}
- code = "def f():\n (" + "y,\n "*300 + ") = range(300)"
- exec(code, ns)
- counts = self.count_traces(ns["f"])
- self.assertEqual(counts, {'call': 1, 'line': 301, 'return': 1})
- def test_trace_lots_of_globals(self):
- code = """if 1:
- def f():
- return (
- {}
- )
- """.format("\n+\n".join(f"var{i}\n" for i in range(1000)))
- ns = {f"var{i}": i for i in range(1000)}
- exec(code, ns)
- counts = self.count_traces(ns["f"])
- self.assertEqual(counts, {'call': 1, 'line': 2000, 'return': 1})
- class TestEdgeCases(unittest.TestCase):
- def setUp(self):
- self.addCleanup(sys.settrace, sys.gettrace())
- sys.settrace(None)
- def test_reentrancy(self):
- def foo(*args):
- ...
- def bar(*args):
- ...
- class A:
- def __call__(self, *args):
- pass
- def __del__(self):
- sys.settrace(bar)
- sys.settrace(A())
- with support.catch_unraisable_exception() as cm:
- sys.settrace(foo)
- self.assertEqual(cm.unraisable.object, A.__del__)
- self.assertIsInstance(cm.unraisable.exc_value, RuntimeError)
- self.assertEqual(sys.gettrace(), foo)
- def test_same_object(self):
- def foo(*args):
- ...
- sys.settrace(foo)
- del foo
- sys.settrace(sys.gettrace())
- if __name__ == "__main__":
- unittest.main()
|