
    daf$@                        d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ ddlmZ  ej$                  d	      ZdZ G d
 de      Z G d dej.                        Z G d dej.                        Z G d dej.                        Z G d deej.                        Zedk(  r ej:                          yy)    N)support)import_helper)TESTFNTESTFN_UNDECODABLE)assert_python_failure)ExceptionIsLikeMixin   )decode_stderr	_testcapic                       e Zd Zy)CustomErrorN__name__
__module____qualname__     9/root/Python-3.12.4/Lib/test/test_capi/test_exceptions.pyr   r      s    r   r   c                       e Zd Zd Zd Zy)Test_Exceptionsc                    t        d      }t        d      }	 |# t         $ r}t        j                         }t
        j                  |      }t        j                         }t
        j                  |      }t        j                         }| j                  ||       | j                  ||       | j                  ||       | j                  ||       | j                  ||       | j                  ||       Y d }~y d }~ww xY w)N5TEST)
ValueError	TypeErrorfailsys	exceptionr   set_exceptionassertEqual)	selfraised_exceptionnew_exceorig_sys_exceptionorig_exceptionnew_sys_exceptionnew_exceptionreset_sys_exceptions	            r   test_exceptionzTest_Exceptions.test_exception   s    %c?F#	."" 	?!$&44W=N #%33NCM"%--/^Q/^-=>/@0.A]G4.>>	?s    	C?CC::C?c                 j   t        d      }t        d      }	 |# t         $ r}|j                  }t	        j
                         }t        j                  |j                  |d       }t	        j
                         }t        j                  | }t	        j
                         }	| j                  |d   |       | j                  ||j                  ||f       | j                  ||       | j                  |	|       | j                  ||j                  |d f       | j                  ||       Y d }~y d }~ww xY w)Nr   r   r	   )r   r   
assertTrue__traceback__r   exc_infor   set_exc_info	__class__r    assertSequenceEqual)
r!   r"   r#   r$   tborig_sys_exc_infoorig_exc_infonew_sys_exc_infonew_exc_inforeset_sys_exc_infos
             r   test_exc_infozTest_Exceptions.test_exc_info0   s   %c?F#	#"" 	EB #%2273D3DgtTM"||~$11=AL!$]1-q1$$]5E5O5OQace4fg$$%6F$$%7G$$\G4E4EwPT3UV$$%5|DD	Es    
D2DD--D2N)r   r   r   r*   r8   r   r   r   r   r      s    .,#r   r   c                   B    e Zd ZddZ ej
                         d        Zy)Test_FatalErrorc                 f   t        j                         5  t        dd|      \  }}}d d d        t              }| j	                  d|       t        j                  d|t
        j                        }|s| j                  d|       t        |j                  d      j                         j                  d            }t        |j                  d            }	|D ]  }
| j	                  |
|        |D ]  }
| j                  |
|        | j                  t!        |      |	       y # 1 sw Y   xY w)	Nz-sSIz-cz8Fatal Python error: _testcapi_fatal_error_impl: MESSAGE
z,^Extension modules:(.*) \(total: ([0-9]+)\)$z$Cannot find 'Extension modules:' in r	   z,    )r   SuppressCrashReportr   r
   assertInresearch	MULTILINEr   setgroupstripsplitintassertNotInr    len)r!   codeexpectednot_expectedrcouterrmatchmodulestotalnames              r   check_fatal_errorz!Test_FatalError.check_fatal_errorJ   s   ((* 	E0tDLBS	E C Q	 		Ir||-II<SGDEekk!n**,22489EKKN# 	)DMM$(	)  	,DT7+	,Wu-%	E 	Es   D&&D0c                 f    d}d}d}| j                  |||       d}d}d}| j                  ||       y )N)r   )r   z8import _testcapi, sys; _testcapi.fatal_error(b"MESSAGE")zif True:
            import _testcapi, sys
            sys.stdlib_module_names = frozenset({"_testcapi"})
            _testcapi.fatal_error(b"MESSAGE")
        )rS   )r!   rJ   rK   rI   s       r   test_fatal_errorz Test_FatalError.test_fatal_error_   sJ     "ItX|< %
 	tX.r   N)r   )r   r   r   rS   r   requires_subprocessrU   r   r   r   r:   r:   H   s&    .* !W  "/ #/r   r:   c                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
Test_ErrSetAndRestorec                 "   | j                  t              5  t        j                  t                      d d d        t               }	 t        j                  |       y # 1 sw Y   *xY w# t        $ r}| j	                  ||       Y d }~y d }~ww xY wN)assertRaisesr   r   err_set_raisedassertIs)r!   vexs      r   test_err_set_raisedz)Test_ErrSetAndRestore.test_err_set_raisedu   sq    z* 	3$$Z\2	3L	!$$Q'		3 	3
  	!MM!R  	!s#   AA) A&)	B2B		Bc                 .   | j                  t              5  t        j                  t               d d d        | j                  t              5  t        j                  t        d       d d d        | j                  t              5  t        j                  t        dd        d d d        | j                  t              5  t        j                  t        t                      d d d        	 t        j                  t        d       	 ddz   | j                  t              5  t        j                  t        d       d d d        | j                  t              5  t        j                  t        dd       d d d        	 t        j                  t        d       y # 1 sw Y   kxY w# 1 sw Y   @xY w# 1 sw Y   xY w# 1 sw Y   xY w# t        $ r)}| j                  d|j                  d          Y d }~d }~ww xY w# t        $ r}|j                  }Y d }~d }~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# t        $ rO}| j                  d|j                  d          | j                  |j                  j                         Y d }~y d }~ww xY w)Nr	   hir   )r[   r   r   err_restoreKeyErrorr    args	Exceptionr-   r   r]   tb_next)r!   kr$   r2   r^   s        r   test_err_restorez&Test_ErrSetAndRestore.test_err_restore~   s   z* 	.!!*-	.z* 	1!!*a0	1z* 	7!!*a6	7z* 	<!!*jl;	<	.!!(D1	!aC z* 	5!!*a4	5y) 	4!!*a3	4	7!!*a4+	. 	.	1 	1	7 	7	< 	<  	.T166!9--	.  	!B	!	5 	5	4 	4  	7Qq	*MM"aoo5566	7s   FF&F3>#G *G H H$H0=H< F#&F03F= G		G>G99G>	H!
HH!$H-0H9<	JAJJc           	         | j                  t              5 }t        j                  t        d       d d d        | j	                  j
                  j                  d       | j                  t              5 }t        j                  t        t        ddd             d d d        |j
                  j                  d   }| j                  |t               | j	                  |j                  d       | j                  t              5 }t        j                  t        t        d             d d d        | j	                  |j
                  j                  d	        G d
 dt              } G d dt        |      }| j                  t              5 }t        j                  | |              d d d        y # 1 sw Y   xY w# 1 sw Y   #xY w# 1 sw Y   xY w# 1 sw Y   y xY w)N*   )rk   r	   r<      r   )r	   r<   rl      )rm   c                       e Zd Zd Zy)3Test_ErrSetAndRestore.test_set_object.<locals>.Metac                     ddz   y )Nr	   r   r   )clssubs     r   __subclasscheck__zETest_ErrSetAndRestore.test_set_object.<locals>.Meta.__subclasscheck__   s	    !r   N)r   r   r   rs   r   r   r   Metaro      s    r   rt   c                       e Zd Zy)5Test_ErrSetAndRestore.test_set_object.<locals>.BrokenNr   r   r   r   Brokenrv      s    r   rw   )	metaclass)r[   r   r   exc_set_objectr    r   re   r   assertIsInstancePermissionErrorOSErrortyperf   ZeroDivisionError)r!   r$   wrappedrt   rw   s        r   test_set_objectz%Test_ErrSetAndRestore.test_set_object   s    z* 	5a$$Z4	5))51 z* 	Ca$$Z1Qq1AB	C++""1%gy1y1 / 	C1$$Wob.AB	C))51	4 		Y$ 	 01 	7Q$$VVX6	7 	7/	5 	5
	C 	C	C 	C	7 	7s/   F.4&F;$G	G.F8;GGGc                 l    G d dt               }t        j                  |d      }| j                  |t               | j                  |j                  d   d        G d d      }t        j                  | |             }| j                  |t               | j                  |j                  d   d       y )	Nc                       e Zd Zd Zy)?Test_ErrSetAndRestore.test_set_object_and_fetch.<locals>.Brokenc                     t        d      )NzBroken __init__)r   )r!   args     r   __init__zHTest_ErrSetAndRestore.test_set_object_and_fetch.<locals>.Broken.__init__   s     !233r   N)r   r   r   r   r   r   r   rw   r      s    4r   rw   abcdr   z-Normalization failed: type=Broken args='abcd'c                       e Zd Zd Zy)?Test_ErrSetAndRestore.test_set_object_and_fetch.<locals>.BadArgc                     t        d      )NzBroken arg type)r   r!   s    r   __repr__zHTest_ErrSetAndRestore.test_set_object_and_fetch.<locals>.BadArg.__repr__   s     122r   N)r   r   r   r   r   r   r   BadArgr      s    3r   r   z0Normalization failed: type=Broken args=<unknown>)rf   r   exc_set_object_fetchrz   r   r    	__notes__)r!   rw   excr   s       r   test_set_object_and_fetchz/Test_ErrSetAndRestore.test_set_object_and_fetch   s    	4Y 	4 ,,VV<c:.q)H	J	3 	3 ,,VVX>c:.q)K	Mr   c                 Z   t         j                  }| j                  t              5 } |t        d       ddd       | j	                  j
                  j                  d       | j                  t              5 } |t        dj                                ddd       | j	                  |j
                  j                  d       | j                  t              5   |t        d       ddd       | j                  t        |t        d       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   >xY w)zTest PyErr_SetString()   errorN)error   помилкаr      )r   err_setstringr[   r~   r    r   re   encodeUnicodeDecodeErrorSystemErrorlist)r!   	setstringr$   s      r   test_set_stringz%Test_ErrSetAndRestore.test_set_string   s    ++	01 	3Q'2	3)):601 	DQ')9)@)@)BC	D))+>?12 	2'1	2+y$A	3 	3	D 	D	2 	2s#   D	7DD!	DD!D*c           	      L   t        j                  d       ddlm}m}m}m} d}t        ||      }||f|_        ||_	        | j                  t              5 } |t        dd |d             ddd       | j                  j                  j                  d	       | j                  t              5 } |t        d
dj                                ddd       | j                  |j                  j                  d       | j!                  t"        d      5   |t        d |d             ddd       | j!                  t$        d      5   |t        d       ddd       | j                  t&        |t(        d       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   kxY w# 1 sw Y   KxY w)zTest PyErr_Format()ctypesr   )	pythonapi	py_objectc_char_pc_intPyErr_Formats   %s %dr   rk   N)zerror 42s   %sr   r   znot in ranges   %czformat stringr   )r   import_moduler   r   r   r   r   getattrargtypesrestyper[   r~   r    r   re   r   assertRaisesRegexOverflowErrorr   r   r   )r!   r   r   r   r   rR   r   r$   s           r   test_formatz!Test_ErrSetAndRestore.test_format   si   ##H-@@y$/!*H 6(01 	KQ*Hhb	J	K))=901 	NQ*E3C3J3J3LM	N))+>?##M>B 	>*E59=	>##J@ 	5*G4	5+|T8D	K 	K	N 	N	> 	>	5 	5s0   E5.FFF5E?FFF#c                 n   t         j                  }t        j                  }| j	                  t
              5 } ||t        d       ddd       | j                  j                  j                  |df       | j                  |j                  j                  |       | j                  |j                  j                  d       | j	                  t
              5 } ||t        t        j                  t                     ddd       | j                  |j                  j                  t               t        rm| j	                  t
              5 } ||t        t               ddd       | j                  |j                  j                  t        j                  t                     | j	                  t
              5 } ||t        t                ddd       | j#                  |j                  j                         | j	                  t              5 } |dt        d       ddd       | j                  |j                  j                  d       | j                  |j                  j                  d       | j                  |j                  j                  d       | j	                  t$              5 } ||t$        d       ddd       | j                  |j                  j                  |ddf       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   JxY w# 1 sw Y   xY w# 1 sw Y   txY w)z%Test PyErr_SetFromErrnoWithFilename()s   fileNzNo such file or directoryfiler   )r   Error)r   err_setfromerrnowithfilenameerrnoENOENTr[   FileNotFoundErrorr|   r    r   re   filenameosfsencoder   r   fsdecodeNULLassertIsNoner~   )r!   setfromerrnowithfilenamer   r$   s       r   test_setfromerrnowithfilenamez3Test_ErrSetAndRestore.test_setfromerrnowithfilename   s   #,#I#I 01 	?Q$VWg>	?)) "=>	@**F3--v601 	KQ$VWbkk&6IJ	K--v6""#45 N(:LMNQ[[11[[);<> 01 	<Q$VWd;	<!++../w' 	:1$Q9	:))<8**A.--v601 	IQ$V->H	I)) "=vF	H;	? 	?	K 	K
N N
	< 	<	: 	:	I 	IsG   K*&K7	L6LL)L+*K47LLLL(+L4c                    t         j                  }| j                  j                  j                  }t        j                         5 } |t        d      t               | j                  |j                  j                  t               | j                  t        |j                  j                        d       | j                  |j                  j                  j                  |dz          | j!                  |j                  j"                         | j                  |j                  j$                  t               d d d        t        j                         5 } |t        d      t&               | j                  |j                  j                  t               | j                  t        |j                  j                        d       | j                  |j                  j                  j                  |dz          | j!                  |j                  j"                         | j!                  |j                  j$                         d d d        t        j(                  t*        dd       5  t        j,                         5 } |t        d      t               d d d        d d d        j/                         j1                         }| j                  |d   dt               | j                  |d   d       | j                  |d	   t2         d
       t        j(                  t*        dd       5  t        j,                         5 } |t        d      t&               d d d        d d d        |j/                         j1                         }| j                  |d   d       | j                  |d	   t2         d
       y # 1 sw Y   |xY w# 1 sw Y   xY w# 1 sw Y   >xY w# 1 sw Y   CxY w# 1 sw Y   xY w# 1 sw Y   xY w)Nzoops!      unraisablehookr   zException ignored in: r	   z"Traceback (most recent call last):r   z.CustomError: oops!)r   err_writeunraisabletest_err_writeunraisable__code__co_firstlinenor   catch_unraisable_exceptionr   hexr    
unraisableexc_typestr	exc_valueexc_traceback	tb_linenor   err_msgobjectr   	swap_attrr   captured_stderrgetvalue
splitlinesr   )r!   writeunraisable	firstlinecmstderrliness         r   r   z.Test_ErrSetAndRestore.test_err_writeunraisable  s   #7711::II	//1 	8RK0#6R]]33[AS!8!897CR]]88BB&],bmm334R]]1137	8 //1 	4RK0$7R]]33[AS!8!897CR]]88BB&^-bmm334bmm223	4 %5t< 	7%%'	7+1K0#6	7 	7 !,,.q%;C7#CDq#GHrxj0C$DE%5t< 	8%%'	8+1K0$7	8 	8 !,,.q#GHrxj0C$DE?	8 	8	4 	4	7 	7 	7 	7	8 	8 	8 	8sb   C2N1C-N>#O8OO%O1:O%O11N;>OO	OO"%O.	*O11O:N)r   r   r   r`   ri   r   r   r   r   r   r   r   r   r   rX   rX   s   s0    !767:M&B E."HJ$Fr   rX   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )#Test_PyUnstable_Exc_PrepReraiseStarc                     t         |           	 t        dt        d      t	        d      g      # t        $ r}|| _        Y d }~y d }~ww xY w)Negzbad typerk   )supersetUpExceptionGroupr   r   orig)r!   r$   r0   s     r   r   z)Test_PyUnstable_Exc_PrepReraiseStar.setUpA  sE    	 	*(=z"~'NOO 	DII	s    1 	AAAc           	      2   | j                  t        d      5  t        j                  dd g       d d d        | j                  t        d      5  t        j                  | j                  d       d d d        | j                  t        d      5  t        j                  | j                  t        d      dg       d d d        | j                  t
        d      5  t        j                  t        d      t        d      g       d d d        | j                  t
        d      5  t        j                  t        dt        d      g      t        d      g       d d d        y # 1 sw Y   /xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nzorig must be an exceptionrk   zexcs must be a listznot an exceptionzorig must be a raised exceptionr   )r   r   r   unstable_exc_prep_reraise_starr   r   r   r   s    r   test_invalid_argsz5Test_PyUnstable_Exc_PrepReraiseStar.test_invalid_argsH  sj   ##I/JK 	A44R$@	A ##I/DE 	D44TYYC	D ##I/AB 	U44TYY2PR@ST	U ##J0QR 	V44Z^iPRm_U	V ##J0QR 	F44^D:VX>JZ5[6?m_F	F 	F	A 	A	D 	D	U 	U	V 	V	F 	Fs;   E!E),E5*F5FE&)E25E>F
Fc                     | j                  t        j                  | j                  d g      d        	 t	        d      # t        $ r}|}Y d }~nd }~ww xY w| j                  t        j                  |d g      d        y )Nrk   )r    r   r   r   r   )r!   r$   r   s      r   test_nothing_to_reraisez;Test_PyUnstable_Exc_PrepReraiseStar.test_nothing_to_reraiseZ  sp    44TYYG	O	R.  	D	44TD6BD	Js   > 	AAAc                 n    | j                   }t        j                  ||g      }| j                  ||       y rZ   )r   r   r   assertExceptionIsLike)r!   r   ress      r   test_reraise_origz5Test_PyUnstable_Exc_PrepReraiseStar.test_reraise_orige  s/    yy66tdVD""3-r   c                 ,   | j                   }|j                  t              \  }}||g|f||g|f|g|f|g|fg d fg}|D ]H  \  }}| j                  |      5  t        j                  ||      }| j                  ||       d d d        J y # 1 sw Y   UxY w)Ninput)r   rE   r   subTestr   r   r   )r!   r   rO   rest
test_casesr   rJ   r   s           r   test_raise_orig_partsz9Test_PyUnstable_Exc_PrepReraiseStar.test_raise_orig_partsj  s    yyjj+t T]D!E]D!WeVTNJ

  * 	:OE8E* :>>tUK**39: :	:: :s   )B

B	c                    | j                   }|j                  t              \  }}t        d      }t	        d      }|||gt        d||g      f|||gt        d||g      f|||gt        d||g      f||||gt        d|||g      f||||gt        d|||g      f||||gt        d|||g      f||||gt        d|||g      f|||gt        d|||g      f|||gt        d|||g      f|||gt        d|||g      f||gt        d||g      f||gt        d||g      fg}|D ]H  \  }}| j                  |      5  t        j                  ||      }	| j                  |	|       d d d        J y # 1 sw Y   UxY w)Nzbad filezbad runtime r   )
r   rE   r   r|   RuntimeErrorr   r   r   r   r   )
r!   r   rO   r   new1new2r   r   rJ   r   s
             r   test_raise_with_new_exceptionszBTest_PyUnstable_Exc_PrepReraiseStar.test_raise_with_new_exceptions|  s   yyjj+tz"M* E4 .dD\"BCT4 .dD\"BCT4 .dD\"BCD%&rD$;M(NOE4&rD$;M(NOD%&rD$;M(NOD%&rD$;M(NO D$T44F!GHE4 .dD%5H"IJD$T44F!GHD\>"tTl;<D\>"tTl;<

$ ", 	:UHE* :>>tUK**39: :	:: :s   8)E,,E5	)
r   r   r   r   r   r   r   r   r   __classcell__)r0   s   @r   r   r   ?  s#    F$	J.
:$:r   r   __main__)r   r   r?   r   unittesttestr   test.supportr   test.support.os_helperr   r   test.support.script_helperr   test.support.testcaser   	test_miscr
   r   r   r   rf   r   TestCaser   r:   rX   r   r   mainr   r   r   <module>r     s     	 	 
   & = < 6 $ (M''4		) 	-#h'' -#`(/h'' (/VFFH-- FFXY:*>@Q@Q Y:x zHMMO r   