a 5dg@sddlZddlZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZddlmZGdddeZGdddeZGdd d eZGd d d ejZGd d d ejZedkredS)N) TESTFNcaptured_stderrcheck_impl_detailcheck_warnings cpython_only gc_collect no_tracingunlink import_module script_helperSuppressCrashReport)supportc@seZdZddZdS)NaiveExceptioncCs ||_dSNxselfrr0/usr/local/lib/python3.9/test/test_exceptions.py__init__szNaiveException.__init__N__name__ __module__ __qualname__rrrrrrsrc@seZdZdZddZdS)SlottedNaiveExceptionrcCs ||_dSrrrrrrrszSlottedNaiveException.__init__N)rrr __slots__rrrrrrsrc@seZdZddZdS)BrokenStrExceptioncCs tddS)Nzstr() is broken) Exceptionrrrr__str__szBrokenStrException.__str__N)rrrr rrrrrsrc@seZdZddZddZddZddZdsd d Zd d Ze ddZ ddZ e ejdkdddZddZddZddZddZddZd d!Zd"d#Zd$d%Zed&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Z d4d5Z!d6d7Z"d8d9Z#d:d;Z$dd?Z&d@dAZ'dBdCZ(dDdEZ)dFdGZ*dHdIZ+dJdKZ,dLdMZ-edNdOZ.e dPdQZ/e dRdSZ0e dTdUZ1e dVdWZ2e dXdYZ3e dZd[Z4ed\d]Z5d^d_Z6d`daZ7dbdcZ8e dddeZ9dfdgZ:dhdiZ;djdkZdrS)tExceptionTestsc Cs|j||dz |dWn,|yH}zt|}WYd}~n d}~00z |dWn,|y}zt|}WYd}~n d}~00|||||j|Wdn1s0YdS)N)excexcnameZspam)subTeststr assertEqualr)rr"r#errZbuf1Zbuf2rrr raise_catch$s   zExceptionTests.raise_catchc Cs|td|tttd|tdttd}|ttd}tj }zDzddl }| dWntytYn0W|t_ |t tn|t_ |t t0|t d|t td d|td |ttd |td g}|t|jd |tdi}|t|jd|td|td|tdzt}Wnty^Yn0|tdd}tdD]}||7}qx|td|td|tdz tdWntyYn0|td|tdztdddWnty Yn 0| d|t!d |t"d!|t"tj#d|t$d"z gd#Wnt$yjYn0|t%d$|t%t&d%|t'd&z dd}Wnt'yYn0|t(d'z dd}Wn&t(y}z WYd}~n d}~00|t)d(dS))NAttributeErrorZundefined_attributeEOFErrorwrrOSErrorzthis file does not exist ImportErrorZundefined_module IndexError KeyErrorkeyKeyboardInterrupt MemoryError NameError OverflowError RuntimeErrorRecursionError SyntaxErrorz/ IndentationErrorTabErrorz"try: 1/0 1/0 finally: pass zexeczTabError not raised SystemError SystemExit TypeErrorr ValueErroriZeroDivisionErrorrStopAsyncIteration)*r(r) assertRaisesgetattrsysr*openrclosestdinmarshalloadsr r.r/ __import__r0 __getitem__r2r4r5r6Zundefined_variabler7ranger:r;r<r?r=r>compilefailr@rAexitrBrCchrrDrrE)rfpZ savestdinrLrdummyerrr testRaising1s                              &zExceptionTests.testRaisingcs(fdd}d}||d|dddS)Nc sj||dnzt|ddWnBtyb}z*|j|krNd||jfWYd}~nd}~00dWdn1s0YdS)N)srcmsg r?expected %s, got %s"failed to get expected SyntaxError)r$rQr<rZrR)rYrZrWrrrckmsgs *z4ExceptionTests.testSyntaxErrorMessage..ckmsgzHif 1: try: continue except: passz'continue' not properly in loopz continue rrr^srrrtestSyntaxErrorMessages  z%ExceptionTests.testSyntaxErrorMessagecs^tffdd }d}||dd}||dd}||dd }||d td }||d tdS) Nc sbzt|ddWnB|yR}z*|j|kr>d||jfWYd}~nd}~00ddS)Nr[r?r\r])rQrZrR)rYrZ exceptionrWrrrr^s  *z:ExceptionTests.testSyntaxErrorMissingParens..ckmsgzprint "old style"zHMissing parentheses in call to 'print'. Did you mean print("old style")?zprint "old style",zQMissing parentheses in call to 'print'. Did you mean print("old style", end=" ")?zexec "old style"z%Missing parentheses in call to 'exec'zif True: print "No indent"zexpected an indented blockz6if True: print() exec "mixed tabs and spaces"z2inconsistent use of tabs and spaces in indentation)r<r=r>r_rrrtestSyntaxErrorMissingParenss     z+ExceptionTests.testSyntaxErrorMissingParensutf-8c Cs|j|||d|t}t|ddWdn1s>0Y||jj|||jj||jjdurt |t s| |d}| d|d}| ||jjWdn1s0YdS)N)sourcelinenooffsetr[r?replace r8)r$rFr<rQr&rbrfrgtext isinstancer%decodesplitassertIn)rrYrfrgencodingcmlinerrrchecks *   zExceptionTests.checkcCs2|j}|ddd|ddd|ddd|d dd |d dd |d dd dd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd|ddd |d!dd|d"dd|d#dd|d$ddd%d|d&d'd |d(dd|d)dd|d*d+d,|d-dd.|d/dd|d0dd|d1dd|d2dd|d3dd|d4dd|d5dd|d6dd|d7dd|d8dd.|d9dd|d:dd|d;dd.|ddd.dS)?Nzdef fact(x): return x! r1z1 + r8z def spam(): print(1) print(2)zPython = "Python" +uPython = "Ṕýţĥòñ" +s+# -*- coding: cp1251 -*- Python = "ϳ" +cp1251)rosPython = "ϳ" +zx = "azlambda x: x = 2z f{a + b + c}zclass foo:return 1 zdef f(): continuezdef f(): breakz4try: pass except: pass except ValueError: passz(0x+1)zx = 0xIz0010 + 2z x = 32e-+4zx = 0o9uα = 0xIsα = 0xIs## -*- coding: iso8859-7 -*- = 0xIz iso8859-7sif 1: def foo(): ''' def bar(): pass def baz(): '''quux''' z"pass pass pass (1+) pass pass passz(1+)s#coding: utf8 print('我') rz!x = [(yield i) for i in range(3)]zdef f(): from _ import *zdef f(x, x): passzdef f(x): nonlocal xzdef f(x): x = 1 global xz nonlocal xz def f(): global x nonlocal xz#from __future__ import doesnt_existzfrom __future__ import bracesz#x=1 from __future__ import divisionzfoo(1=2)zdef f(): x, y: intz[*x for x in xs]zfoo(x for x in range(10), 100)z (yield i) = 2zdef f(*): passzfor 1 in []: pass)rrr Zuse_old_parser)rrrrrrtestSyntaxErrorOffsetsd                                      z$ExceptionTests.testSyntaxErrorOffsetcsjGdddtGdddfdd}fdd}fd d }tjd sf|||dS) Nc@seZdZddZdS)z9ExceptionTests.testSettingException..BadExceptioncSs tddS)Nzcan't instantiate BadException)r:)Zself_rrrr szBExceptionTests.testSettingException..BadException.__init__Nrrrrr BadException src@s eZdZdS)z=ExceptionTests.testSettingException..InvalidExceptionNrrrrrrrInvalidExceptionsrc sddl}z|dWnZtyr}zBt\}}}|jj}|jd |j dWYd}~nd}~00 ddS)Nrr8 test_capi1test_exceptions.pyExpected exception) _testcapiraise_exceptionrBrHexc_infotb_framef_coder&co_name assertTrue co_filenameendswithrR)rr'r"tbcorrrrrs(z7ExceptionTests.testSettingException..test_capi1c sddl}z|dWnrty}zZt\}}}|jj}|jd |j d|jj j}|jdWYd}~nd}~00 ddS)Nrrr test_capi2r)rrr:rHrrrr&rrrrf_backrR)rr'r"rrZco2rrrrs $z7ExceptionTests.testSettingException..test_capi2csddl}t|jddS)Nrr8)rrFr@r)r)rrrr test_capi3+s z7ExceptionTests.testSettingException..test_capi3java)rrHplatform startswith)rrrrr)rrrrtestSettingExceptions  z#ExceptionTests.testSettingExceptioncCsztWntyYn0|tt|ttdd|ttdddtddd}||jd||jd|t|dtd ddd }||jd ||jd ||jd||j d||j d|t|d td ddd}||jd ||jd||jd||j d||j d|t|dtdd}||jd||jd||jd||j d||j ddS)NiZ1001messagez[Errno 1001] messager~foobarz[Errno 9] foo: 'bar'rrursz[WinError 3] foo: 'bar'z[WinError 1001] foo) WindowsErrorr6assertIsr.r&r%errnowinerrorstrerrorfilename filename2)rr+rrrtest_WindowsError5sB    z ExceptionTests.test_WindowsErrorwin32ztest specific to WindowscCsLtd}d}|td||j|Wdn1s>0YdS)z:Should fill in unknown error code in Windows error messagectypeslcs@zWindows Error 0x%xN)r assertRaisesRegexr.Z pythonapiZPyErr_SetFromWindowsErr)rrcoderrrtest_windows_message\sz#ExceptionTests.test_windows_messagecCs tdddiftdddiftdddiftdddiftddddftdddddddftd d dddd dftd d d ddd dftd d d ddftddddddftdddddddftddddddddftdddddddddftddd ddddd!d"ftd#dd#dddddd$ftdddiftd%d%d&d'd(d)d*ftd&td+d(dd)fd&td+d(dd)fd&d+d(d)d*ftd,d,d&d+d(d)d*ftd-d-d.d/d(dd0ft dddd1ft dddd1fg}z"| t dddddddd2fWnt yYn0|D] \}}}z ||}Wn$td3||ftjd4Yn0t|jd5sD|t|jd6t|}|D]:}t||}|t|t||d7|||||fqPtfD]n} t| jdD]X} | || }| |} |D]6}tt| |} t||} || | d8||fqqqqdS)9Nrargs)r8)r)rr8r)rr)rrrrr)rrr)rrbazr)rrrNquuxr)rrr)errnoStr strErrorStr filenameStr)rrrrr)rrrr)r8rr)r8rr8)rrrrr)rZrjrrfrgprint_file_and_line)msgStrr)rrjrrZrrfrg)r)r linenoStr offsetStrtextStrrrr)rgrjrrrZrrf)rrrrrZprint_file_and_lineStr)rjrrrZrrfrg)asciiarr8ordinal not in rangerrrr)rroobjectstartreason)rrrr8r)あrr8ouchrr)rrrrend)rr)rrrrrrz exc=%r, args=%r)filerbuiltinsz%r.%s == %r, expected %rzpickled "%r", attribute "%s) BaseExceptionrAr.r< UnicodeErrorUnicodeEncodeErrorUnicodeDecodeError bytearrayUnicodeTranslateErrorrrappendrr6printrHstderrtyperrr&rr%rGreprpicklerPHIGHEST_PROTOCOLdumpsrM)rZ exceptionListr"rZexpectedrWr`Z checkArgNamevaluepprotocolnewZgotZwantrrrtestAttributesfs"     O        zExceptionTests.testAttributescCsz tdWntd}Yn0t|}||t||j|td|}||t||j|Gdddt}||}|||||j|dS)Nrtrsrc@s eZdZdS)z5ExceptionTests.testWithTraceback..MyExceptionNrrrrr MyExceptionsr) r0rHrrwith_tracebackassertIsInstancer& __traceback__r)rrrWrrrrtestWithTracebacks      z ExceptionTests.testWithTracebackc CsPz dt_Wn4ty@}z|dt|WYd}~nd}~00|ddS)Nrz!__traceback__ must be a tracebackzNo exception raised)rrrBrnr%rRrrWrrrtestInvalidTracebacks  &z#ExceptionTests.testInvalidTracebackcCsP|tttdd|tttd|tttdd|tttddS)N __cause__r8 __context__)rFrBsetattrrdelattrrrrrtestInvalidAttrsszExceptionTests.testInvalidAttrscCsJz tdWntd}Yn0t}||_d|_||jddS)Nrtrs)r0rHrrrr&)rrrWrrrtestNoneClearsTracebackAttrs z*ExceptionTests.testNoneClearsTracebackAttrcCsnt}||j||jt}||j||jGdddt}|}||j||jdS)Nc@s eZdZdS)z5ExceptionTests.testChainingAttrs..MyExceptionNrrrrrrsr)r assertIsNonerrrBr.)rrWrrrrtestChainingAttrss     z ExceptionTests.testChainingAttrsc Csz tWn(ty2}z|}WYd}~n d}~00||j||j||jt|_d|_||jt||j||jd|_||jdS)NF) rrrrZ assertFalse__suppress_context__r6rr)rr"rWrrrtestChainingDescriptors"s      z&ExceptionTests.testChainingDescriptorscCs<|jttddGdddt}|dd}||jddS)Nr8)rc@seZdZddZdS)z8ExceptionTests.testKeywordArgs..DerivedExceptioncSst|||_dSr)rr fancy_arg)rrrrrr:s zAExceptionTests.testKeywordArgs..DerivedException.__init__NrrrrrDerivedException9sr*)r)rFrBrr&r)rrrrrrtestKeywordArgs4s zExceptionTests.testKeywordArgscs4fdd|tfdd|tdS)NcsSrrr)frrrCsz/ExceptionTests.testInfiniteRecursion..fcs"zWStyYdS0dS)NrrCrgrrrGs z/ExceptionTests.testInfiniteRecursion..g)rFr;rr)rrrtestInfiniteRecursionAs   z$ExceptionTests.testInfiniteRecursioncCs8|tt|ttd|ttdddS)Nrb)rr%rrrrrtest_strNszExceptionTests.test_strc CsNz tWn0ty:}z||~WYd}~n d}~00|dtdS)NrW)rrZ assertNotInlocalsrrrrtestExceptionCleanupNamesTs   z(ExceptionTests.testExceptionCleanupNamesc sGdddtGddd}fdd}|t}z |Wn$yj}z WYd}~n d}~00d|||t}z |WnyYn0d|||t}z |Wn Yn0d|||t}dD]&}z |WnYq:Yn0qd|||t}z z |WntYn0WnRty}z8d|_d|tdd rt|WYd}~n d}~00|t}z |Wn<y(zzWn0Wny"Yn0Yn0dtdd r@t||Gd d d }|t}||Wdn1s0Ydtdd rt||dS) Nc@seZdZddZdS)z=ExceptionTests.testExceptionCleanupState..MyExceptioncSs ||_dSrobjrrrrrrcszFExceptionTests.testExceptionCleanupState..MyException.__init__Nrrrrrrbsrc@s eZdZdS)z7ExceptionTests.testExceptionCleanupState..MyObjNrrrrrMyObjesrcs}dSrr)Z local_refrrrrinner_raising_funchszDExceptionTests.testExceptionCleanupState..inner_raising_func)rF)Zcpythonc@seZdZddZddZdS)z9ExceptionTests.testExceptionCleanupState..ContextcSs|Srrrrrr __enter__szCExceptionTests.testExceptionCleanupState..Context.__enter__cSsdS)NTr)rexc_type exc_valueexc_tbrrr__exit__szBExceptionTests.testExceptionCleanupState..Context.__exit__N)rrrrrrrrrContextsr)rweakrefrefrr2rrr)rrrwrrWirrrrtestExceptionCleanupState^s                        & z(ExceptionTests.testExceptionCleanupStatec sDfdd}ztWn*ty>z|WYdn d00dS)NcsdSrrrrWrr print_errorszIExceptionTests.test_exception_target_in_nested_scope..print_error)Z somethingr)rrrrr%test_exception_target_in_nested_scopes  z4ExceptionTests.test_exception_target_in_nested_scopecCsdd}|}|t|t|tdd|t|t|tdd|t|dz tdWnty|}|t|t|tdt|t|t|tdt|t|t~|tdtYn0dS)NcssNz tdWn.ty:tdVtdVYn0tdVdS)NZcaughtr)r2rHrrrrr yield_raises   z:ExceptionTests.test_generator_leaking..yield_raiserr)r&nextr2rHrrB)rrrrrrtest_generator_leakings$  z%ExceptionTests.test_generator_leakingcCsfdd}ztWn ty0|}t|Yn0z t|WntyPYn0|tddS)Ncss dVdSrrrrrrrsz1ExceptionTests.test_generator_leaking2..gNNN)r:r  StopIterationr&rHrrritrrrtest_generator_leaking2s   z&ExceptionTests.test_generator_leaking2c Csdd}|}t|z ddWnbty}zJ|td|||}|td||||WYd}~n d}~00|tddS)Ncss0z dVWn ty*tdVYn0dSNr8rDrHrrrrrr s  z1ExceptionTests.test_generator_leaking3..gr8rr )r rDrrHrthrowr&)rrrrWZgen_excrrrtest_generator_leaking3 s  "z&ExceptionTests.test_generator_leaking3c Csdd}|}ztWnty0t|}Yn0||tz t|Wn8ty}z |td|WYd}~n d}~00|tddS)Ncss4z ddWn"ty.tdVYn0dS)Nr8rrrrrrr s   z1ExceptionTests.test_generator_leaking4..gr8r )rBr rrDrHrr&)rrrtprWrrrtest_generator_leaking4s   *z&ExceptionTests.test_generator_leaking4csHfdd}|}ztWnty4t|Yn0tt|dS)Nc3s.tdtdVtddS)Nr8r )rrHrr:r&rrrrr9sz>ExceptionTests.test_generator_doesnt_retain_old_exc..g)r:r rFr r rrr$test_generator_doesnt_retain_old_exc8s  z3ExceptionTests.test_generator_doesnt_retain_old_exccs4ddfdd}|t|tddS)Ncss dVdSrrrrrr simple_genFszIExceptionTests.test_generator_finalizing_and_exc_info..simple_gencs.}ztWnty(t|YS0dSr)r:r )genrrrrun_genHs  zFExceptionTests.test_generator_finalizing_and_exc_info..run_genr )rr&rHr)rrrrr&test_generator_finalizing_and_exc_infoDs  z5ExceptionTests.test_generator_finalizing_and_exc_infocsnGdddtGddd}fdd}|t}|}t|||d}||dS)Nc@seZdZddZdS)zFExceptionTests._check_generator_cleanup_exc_state..MyExceptioncSs ||_dSrrrrrrrVszOExceptionTests._check_generator_cleanup_exc_state..MyException.__init__NrrrrrrUsrc@s eZdZdS)z@ExceptionTests._check_generator_cleanup_exc_state..MyObjNrrrrrrXsrc3s*z Wny$dVYn0dSrrrrrr raising_gen[s  zFExceptionTests._check_generator_cleanup_exc_state..raising_gen)rrrr r)rZtestfuncrrrrrrr"_check_generator_cleanup_exc_stateRs z1ExceptionTests._check_generator_cleanup_exc_statecCsdd}||dS)NcSs(z|tWnty"Yn0dSr)rr:rrrrdo_throwks zGExceptionTests.test_generator_throw_cleanup_exc_state..do_throwr)rrrrr&test_generator_throw_cleanup_exc_statejsz5ExceptionTests.test_generator_throw_cleanup_exc_statecCsdd}||dS)NcSs |dSr)rJrrrrdo_closesszGExceptionTests.test_generator_close_cleanup_exc_state..do_closer)rr!rrr&test_generator_close_cleanup_exc_statersz5ExceptionTests.test_generator_close_cleanup_exc_statecCsdd}||dS)NcSsd}dSrrrrrrdo_delxszCExceptionTests.test_generator_del_cleanup_exc_state..do_delr)rr#rrr$test_generator_del_cleanup_exc_statewsz3ExceptionTests.test_generator_del_cleanup_exc_statecsfdd}|dS)Ncs.z t|WntyYn 0ddSNz should have raised StopIteration)r r rRrrrrdo_next}s   zEExceptionTests.test_generator_next_cleanup_exc_state..do_nextr)rr&rrr%test_generator_next_cleanup_exc_state|s z4ExceptionTests.test_generator_next_cleanup_exc_statecsfdd}|dS)Ncs0z|dWnty Yn 0ddSr%)sendr rRrrrrdo_sends  zEExceptionTests.test_generator_send_cleanup_exc_state..do_sendr)rr)rrr%test_generator_send_cleanup_exc_states z4ExceptionTests.test_generator_send_cleanup_exc_statecsBGfddd}dzt|Wn Yn0|ddS)NcseZdZfddZdS)z*ExceptionTests.test_3114..MyObjectcs tdSr)rHrrrrr__del__sz2ExceptionTests.test_3114..MyObject.__del__Nrrrr+rrrrMyObjectsr-rr )rr&)rr-rrr test_3114szExceptionTests.test_3114cCsftddddd}|t|dd|_|t|dd|_d |_|t|d d |_|t|d d |_|t|dtddddd}|t|dd|_|t|dd|_d |_|t|dd |_|t|dd |_|t|dtdddd}|t|dd|_|t|dd|_d |_|t|dd |_|t|ddS)NrZxxxxxr8rrz8'baz' codec can't encode characters in position 1-4: foorsz<'baz' codec can't encode character '\x78' in position 1: foolESh MQ)EzJ'baz' codec can't encode characters in position 1-4: 965230951443685724997izK'4000' codec can't encode characters in position 1-4: 965230951443685724997izN'4000' codec can't encode characters in position 1000-4: 965230951443685724997sxxxxxz3'baz' codec can't decode bytes in position 1-4: fooz5'baz' codec can't decode byte 0x78 in position 1: foozE'baz' codec can't decode bytes in position 1-4: 965230951443685724997zF'4000' codec can't decode bytes in position 1-4: 965230951443685724997zI'4000' codec can't decode bytes in position 1000-4: 965230951443685724997Zxxxxz/can't translate characters in position 1-4: fooz3can't translate character '\x78' in position 1: foozAcan't translate characters in position 1-4: 965230951443685724997zDcan't translate characters in position 1000-4: 965230951443685724997) rr&r%rrrorrr)rurrrtest_unicode_change_attributess>z-ExceptionTests.test_unicode_change_attributescCs.tttf}|D]}|t||dqdS)N)rrrr&r%__new__)rZklassesklassrrrtest_unicode_errors_no_objects z,ExceptionTests.test_unicode_errors_no_objectc sGdddt}Gdddt|d}tx}z tWnR|yd}z|dWYd}~n8d}~0tytYn |dYn 0|dWdn1s0Yfdd \}}}||tt||d t|dS) Nc@seZdZddZdS)z/ExceptionTests.test_badisinstance..MetacSs tdSrr)clssubclassrrr__subclasscheck__szAExceptionTests.test_badisinstance..Meta.__subclasscheck__N)rrrr7rrrrMetasr8c@s eZdZdS)z6ExceptionTests.test_badisinstance..MyExceptionNrrrrrrsr) metaclassz%exception should not be a MyExceptionzShould have raised KeyErrorcs(zWSty"tYS0dSr)r;rHrrrrrrs z,ExceptionTests.test_badisinstance..gz maximum recursion depth exceeded) rrrr2rRrr;rnr%)rr8rrrWvrrrrtest_badisinstances   (  z!ExceptionTests.test_badisinstancecCsPdt}tdd|\}}}||d|d||d||d|dS)Naif 1: import sys from _testinternalcapi import get_recursion_depth class MyException(Exception): pass def setrecursionlimit(depth): while 1: try: sys.setrecursionlimit(depth) return depth except RecursionError: # sys.setrecursionlimit() raises a RecursionError if # the new recursion limit is too low (issue #25274). depth += 1 def recurse(cnt): cnt -= 1 if cnt: recurse(cnt) else: generator.throw(MyException) def gen(): f = open(%a, mode='rb', buffering=0) yield generator = gen() next(generator) recursionlimit = sys.getrecursionlimit() depth = get_recursion_depth() try: # Upon the last recursive invocation of recurse(), # tstate->recursion_depth is equal to (recursion_limit - 1) # and is equal to recursion_limit when _gen_throw() calls # PyErr_NormalizeException(). recurse(setrecursionlimit(depth + 2) - depth - 1) finally: sys.setrecursionlimit(recursionlimit) print('Done.') z-Wd-cr8sRecursionErrorsResourceWarningDone.)__file__r assert_python_failurer&rnrrrcoutr'rrr$test_recursion_normalizing_exceptions ()   z3ExceptionTests.test_recursion_normalizing_exceptioncCs>d}td|\}}}||d|d||d|dS)Nzif 1: import _testcapi try: raise _testcapi.RecursingInfinitelyError finally: print('Done.') r<r8sORecursionError: maximum recursion depth exceeded while normalizing an exceptionr=)r r?r&rnr@rrr-test_recursion_normalizing_infinite_exception#s z0YdS)Na+if 1: import _testcapi class C(): pass def recurse(cnt): cnt -= 1 if cnt: recurse(cnt) else: _testcapi.set_nomemory(0) C() recurse(16) r<smFatal Python error: _PyErr_NormalizeException: Cannot recover from MemoryErrors while normalizing exceptions.)r r r?rnr@rrr)test_recursion_normalizing_with_no_memory5s  z8ExceptionTests.test_recursion_normalizing_with_no_memorycs@ddlddlmfdd}|}|}||dS)Nrraise_memoryerrorc sJz Wn*ty4}z|j}WYd}~nd}~00d|S)Nz Should have raises a MemoryError)r5rrR format_tb)rWrrGr tracebackrr raiseMemErrorSs   z6ExceptionTests.test_MemoryError..raiseMemError)rJrrGr&)rrKZtb1Ztb2rrIrtest_MemoryErrorMs   zExceptionTests.test_MemoryErrorc Cs"ddl}d}d}|t|jd|d}|t|t|t|t| |j |d|}| |j ||jd|d}|t||Gd d d t }|d |||f}|t|||t||| |j ||d d |ddi} |t| || | j d| | j d dS)NrzThis is a test docstring.zThis is another test docstring.error1z_testcapi.error1z_testcapi.error2z_testcapi.error3)basec@s eZdZdS)z1ExceptionTests.test_exception_with_doc..CNrrrrrCysrOz_testcapi.error4z_testcapi.error5r1rr8)rrFr@Zmake_exception_with_docrrr issubclassrr__doc__r&rr) rrZdoc2Zdoc4rMZerror2Zerror3rOZerror4Zerror5rrrtest_exception_with_doc`s:    z&ExceptionTests.test_exception_with_docc sddlmGddddfdd}z |Wn2tyj}z|dWYd}~nd}~00|d|ddS)NrrFc@s eZdZdS)z3ExceptionTests.test_memory_error_cleanup..CNrrrrrrOsrOcs}t|dSrrrcrOrGrrrinners z7ExceptionTests.test_memory_error_cleanup..innerzMemoryError not raised)rrGr5assertNotEqualrRr&)rrWrWrrVrtest_memory_error_cleanups  $ z(ExceptionTests.test_memory_error_cleanupc s|Gddddfddz Wn2ty^}z|dWYd}~nd}~00|d|ddS)Nc@s eZdZdS)z6ExceptionTests.test_recursion_error_cleanup..CNrrrrrrOsrOcs}t|dSrrSrTrOrWrrrrWs z:ExceptionTests.test_recursion_error_cleanup..innerzRecursionError not raised)r;rXrRr&rrrZrtest_recursion_error_cleanups $ z+ExceptionTests.test_recursion_error_cleanupcCsN|t}ttWdn1s*0Y||jjtj|jdSr) rFr.oslistdirr>r&rbrENOTDIR)rrprrrtest_errno_ENOTDIRs (z!ExceptionTests.test_errno_ENOTDIRcCsbGddd}|}t2}~||jj|j||jjWdn1sT0YdS)Nc@seZdZddZdS)z1ExceptionTests.test_unraisable..BrokenDelcSstd}|dS)Nz del is brokenrrr"rrrr+sz9ExceptionTests.test_unraisable..BrokenDel.__del__Nr,rrrr BrokenDelsra)r Zcatch_unraisable_exceptionr&Z unraisablerr+ZassertIsNotNone exc_traceback)rrarrprrrtest_unraisables  zExceptionTests.test_unraisablec CsttfD]}||z|d}|WnF|ynt}tjtWdn1s`0YYn0|}|d||d|||j ||tur|d|n |d|| | dWdq1s0YqdS)Nz test messagerz raise exczri) rCrr$rrH__excepthook__rgetvaluernrrr)rrr"rreportrrrtest_unhandleds    2   zExceptionTests.test_unhandledcCsFd}tddD]2}td||\}}}||d|d|qdS)Nzif 1: import _testcapi class C(): pass _testcapi.set_nomemory(0, %d) C() r8rvr<)r8xs MemoryError)rPr r?rn)rrrrArBr'rrr"test_memory_error_in_PyErr_PrintExs   z1ExceptionTests.test_memory_error_in_PyErr_PrintExcsxGdddtGdddtfdd}|}|d||Wdn1sj0YdS)Nc@s eZdZdS)zBExceptionTests.test_yield_in_nested_try_excepts..MainErrorNrrrrr MainErrorsrjc@s eZdZdS)zAExceptionTests.test_yield_in_nested_try_excepts..SubErrorNrrrrrSubErrorsrkc 3sBz Wn2y<z dVWny4Yn0Yn0dSrrrrjrkrrmains    z=ExceptionTests.test_yield_in_nested_try_excepts..main)rr(rFr)rrmcororrlr test_yield_in_nested_try_exceptss   z/ExceptionTests.test_yield_in_nested_try_exceptscsRfdd}|}ztWn"ty<t|dYn0t|ddS)Nc3s<ztWnty dVYn0tddVdS)Nr8r rs)rCr&rHrrrrrrs   z?ExceptionTests.test_generator_doesnt_retain_old_exc2..gr8rs)r0r&r )rrrrrr%test_generator_doesnt_retain_old_exc2s  z4ExceptionTests.test_generator_doesnt_retain_old_exc2cCsfdd}|t@|}z ddWnt|t|Yn0Wdn1sX0YdS)NcssdVdVdS)Nr8rsrrrrrrsz1ExceptionTests.test_raise_in_generator..gr8r)rFrDr )rrrrrrtest_raise_in_generators  z&ExceptionTests.test_raise_in_generatorFz Won't work if __debug__ is Falsec Cs\tazWnBtyJ}z*b||t|t|dWYd}~nd}~00b|ddS)NZhellor)rBAssertionErrorrrr&r%rRrrrrtest_assert_shadowing%s &z$ExceptionTests.test_assert_shadowingc CsGdddt}ztWn(ty@}z|}WYd}~n d}~00z|Wnty\Yn0tdD]8}ztWn$ty}z WYd}~n d}~00tqfdS)Nc@s eZdZdS)zBExceptionTests.test_memory_error_subclasses..TestExceptionNrrrrr TestException>srtr1)r5rrPr)rrtr"inst_rrrtest_memory_error_subclasses5s   z+ExceptionTests.test_memory_error_subclassesN)rd)?rrrr(rXrarcrrrrrrunittestZ skipUnlessrHrrrrrrrrrrrrrrrrr rrrrrrr r"r$r'r*r.r0r4r;rCrDrErLrRrYr[r_rcrgrirorprqrsrwrrrrr!"s T A /'  ~    t    %  :    '     r!c@s,eZdZddZddZddZddZd S) ImportErrorTestscCstd}||j||jtddd}||jd||jtddd}||jd||jtdddd}||jd||jdd}|t|tdd d Wdn1s0Y|t|tdd d d Wdn1s0Y|t|tdd d dWdn1s@0Y|t|td d Wdn1sx0Y|t|tdd ddWdn1s0YdS)NtestZ somemodulenamesomepathpathsomename)rr|z8'invalid' is an invalid keyword argument for ImportErrorkeyword)invalidr|)r|rr)rrT)rZanother)r/rr|rr&rrB)rr"rZrrrtest_attributesVs.      *..*z ImportErrorTests.test_attributescCstdddd}||jd||jd||jd||jd|||jd||jd||jd||jddS)Nrzr|rr|rrzr)r/r&rrZr|rrr`rrrtest_reset_attributesxsz&ImportErrorTests.test_reset_attributescCsTtdtfdd0d}t|}|t|t|Wdn1sF0YdS)Nr1T)quietsabc)r BytesWarningr/r&r%)rargr"rrrtest_non_str_argumentsz&ImportErrorTests.test_non_str_argumentcCsttddtddtdddfD]}td i|}ttjdD]R}tt||}||jd||j d||j |j ||j |j qBt j t j fD]H}||}||jd||j d||j |j ||j |j qq"dS) Nrr{r}r~rrzr8r)rz)dictr/rPrrrMrr&rrZr|rcopydeepcopy)rkwargsZorigprotor"rUrrrtest_copy_pickles$ z!ImportErrorTests.test_copy_pickleN)rrrrrrrrrrrryTs"ry__main__)rgcr\rHrxrrrZ test.supportrrrrrrrr r r r rzr rrrrZTestCaser!ryrrmrrrrs44 <M