a
    ze!                     @   s  d dl mZ d dlmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZ d dlZd dlZd dlZd aG dd dejZG dd dejZG dd	 d	ejZe jjG d
d dejZe jjG dd dejZedkre  dS )    )support)assert_python_okassert_python_failureNc                   @   s$   e Zd Zdd Zdd Zdd ZdS )DisplayHookTestc                 C   s   t j}t }|d W d    n1 s,0    Y  | | d | tjd t`t }|d  W d    n1 s~0    Y  | | d | t	td  | 
t| t j}zt `| 
t|d W |t _n|t _0 d S )N*   z42
 _)sys__displayhook__r   Zcaptured_stdoutassertEqualgetvaluebuiltinsr   
assertTruehasattrassertRaises	TypeErrorstdoutRuntimeError)selfZdhoutr    r   #/usr/lib/python3.9/test/test_sys.pytest_original_displayhook   s     
&
&z)DisplayHookTest.test_original_displayhookc                 C   s<   t j}z(t `tddd}| tt| W |t _n|t _0 d S )N42<string>single)r	   displayhookcompiler   r   eval)r   r   coder   r   r   test_lost_displayhook5   s    z%DisplayHookTest.test_lost_displayhookc                 C   sT   dd }t td|* tddd}| tt| W d    n1 sF0    Y  d S )Nc                 S   s   t d S N
ValueError)objr   r   r   baddisplayhook?   s    z?DisplayHookTest.test_custom_displayhook.<locals>.baddisplayhookr   r   r   r   )r   	swap_attrr	   r   r   r#   r   )r   r%   r   r   r   r   test_custom_displayhook>   s    z'DisplayHookTest.test_custom_displayhookN)__name__
__module____qualname__r   r    r'   r   r   r   r   r      s   	r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ExceptHookTestc                 C   s   zt dW nZ t yf } zBt }tjt   W d    n1 sH0    Y  W Y d }~n
d }~0 0 | | d | 	t
tj d S )Nr   ValueError: 42
)r#   r   captured_stderrr	   __excepthook__exc_infor   r   endswithr   r   r   excerrr   r   r   test_original_excepthookI   s    
Bz'ExceptHookTest.test_original_excepthookc                 C   s   t   t dt ztddW nZ ty~ } zBt }tjt	   W d    n1 s`0    Y  W Y d }~n
d }~0 0 W d    n1 s0    Y  |
 }| d| | d| | |d d S )Nignoremsg)s   bytes_filename{   r   textz%  File "b'bytes_filename'", line 123
z	    text
zSyntaxError: msg
)warningscatch_warningssimplefilterBytesWarningSyntaxErrorr   r-   r	   r.   r/   r   assertInr   r0   r1   r   r   r   test_excepthook_bytes_filenameT   s    

`z-ExceptHookTest.test_excepthook_bytes_filenamec                 C   sP   t jd}tddd W d    n1 s00    Y  | d| v  d S )Nstderr   1zETypeError: print_exception(): Exception expected for value, str found)testr   captured_outputr	   
excepthookr   r   )r   r@   r   r   r   test_excepthooke   s
    ,zExceptHookTest.test_excepthookN)r(   r)   r*   r4   r?   rF   r   r   r   r   r+   G   s   r+   c                   @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
jdd Zdd Zdd Zeeedddd Ze	j
jdd Zdd Ze	j
jdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Z d-d. Z!e	j
jd/d0 Z"d1d2 Z#ee	j
j$d3ee% e&'d4kd5d6d7 Z(e)ej*ej+kd8d9d: Z,dbd<d=Z-d>d? Z.dcd@dAZ/dBdC Z0dDdE Z1dFdG Z2dHdI Z3e	j
jdJdK Z4eeedLdMdNdO Z5dPdQ Z6dRdS Z7dTdU Z8eeedVdWdXdY Z9dZd[ Z:d\d] Z;eeed^d_d`da Z<d;S )dSysModuleTestc                 C   s   t j  d S r!   )rC   r   reap_childrenr   r   r   r   tearDownq   s    zSysModuleTest.tearDownc                    s     ttjdd   t}t  W d    n1 s:0    Y   |jj tdd\}}} 	|d  	|d  	|d   t}td W d    n1 s0    Y   	|jjd   t}td W d    n1 s0    Y   	|jjd   t}td W d    n1 s:0    Y   	|jjd   t}td W d    n1 s0    Y   	|jjd t
dd	\}}} 	|d
  	|d  	|d  fdd}|dd |dd |dddd d S )Nr   -czimport sys; sys.exit()r       )r   exit)      zraise SystemExit(47)/   c                    sV   t d| fi |\}}} |d  |d  ||dt|t|f  d S )NrK   rA   rL   z%s doesn't start with %s)r   r   r   
startswithascii)r   expectedZenv_varsrcr   r3   rI   r   r   check_exit_message   s    z3SysModuleTest.test_exit.<locals>.check_exit_messagez?import sys; sys.stderr.write("unflushed,"); sys.exit("message")s   unflushed,messagez)import sys; sys.exit("surrogates:\uDCFF")s   surrogates:\udcffzimport sys; sys.exit("h\xe9")s   hzlatin-1)PYTHONIOENCODING)r   r   r	   rM   
SystemExitassertIsNone	exceptionr   r   r   r   )r   cmrT   r   r3   rU   r   rI   r   	test_exitt   sJ    &((**	zSysModuleTest.test_exitc                 C   s$   |  ttjd | t t d S )Nr   )r   r   r	   getdefaultencodingassertIsInstancestrrI   r   r   r   test_getdefaultencoding   s    z%SysModuleTest.test_getdefaultencodingc              	   C   s   |  ttj |  ttjd |  ttjd |  ttjd t }| |dk | z:ddd|fD ]}t| | t | qdW t| nt| 0 d S )Nag      g        g      ?gh㈵>g?g      @)r   r   r	   setswitchintervalr#   getswitchintervalr   ZassertAlmostEqual)r   Zorignr   r   r   test_switchinterval   s    
z!SysModuleTest.test_switchintervalc                 C   s^   |  ttjd t }|  ttj |  ttjd td | t d t| d S )Nr   ii'  )r   r   r	   getrecursionlimitsetrecursionlimitr#   r   )r   oldlimitr   r   r   test_recursionlimit   s    
z!SysModuleTest.test_recursionlimitc              	      s   t tdrt r| d t } fdd zVdD ]B}zt| W n ty^   Y q6Y n0 | t  | t  q6W t| nt| 0 d S )Ngettracez(fatal error if run with a trace functionc                      s
      d S r!   r   r   fr   r   rk      s    z5SysModuleTest.test_recursionlimit_recovery.<locals>.f)
      2   K   d        )r   r	   ri   ZskipTestre   rf   RecursionErrorr   )r   rg   depthr   rj   r   test_recursionlimit_recovery   s    

z*SysModuleTest.test_recursionlimit_recoveryc              	      sb   ddl m   fddt }z,td dD ]}|| q4W t| nt| 0 d S )Nr   )get_recursion_depthc                    sd     }|| krV t}t| W d    n1 s80    Y  t|jd n
| | d S )Nz\cannot set the recursion limit to [0-9]+ at the recursion depth [0-9]+: the limit is too low)r   rs   r	   rf   ZassertRegexr^   rY   )rt   limitrecursion_depthrZ   rv   r   set_recursion_limit_at_depthr   r   rz      s    (zZSysModuleTest.test_setrecursionlimit_recursion_depth.<locals>.set_recursion_limit_at_depthrr   )rl   rm   rn   ro   rp         )_testinternalcapirv   r	   re   rf   )r   rg   rw   r   ry   r   &test_setrecursionlimit_recursion_depth   s    
z4SysModuleTest.test_setrecursionlimit_recursion_depthc                 C   sv  t jtd t }| t|d | |d t | |d t | |d t | |d t | |d t	 | 
ttj|d | |jt | |jt | |jt | |jt | |jt	 | |jt | |jt | |jt | |jt | |d |j | |d |j | |d |j | |d |j | |d |j t \}}}}}d S )Ngetwindowsversion   r   rA            )rC   r   get_attributer	   r   r   lenr]   intr^   r   
IndexErroroperatorgetitemmajorminorZbuildplatformZservice_packZservice_pack_minorZservice_pack_majorZ
suite_maskZproduct_type)r   vZmajminZbuildnoZplatZcsdr   r   r   test_getwindowsversion  s0    z$SysModuleTest.test_getwindowsversionc                 C   s   |  ttjtd d S )Nr   )r   r   r	   call_tracingtyperI   r   r   r   test_call_tracing)  s    zSysModuleTest.test_call_tracingsetdlopenflagsztest needs sys.setdlopenflags()c                 C   sf   |  ttd | ttjd t }| ttj t|d  | t |d  t| d S )Ngetdlopenflagsr   rA   )r   r   r	   r   r   r   r   r   )r   Zoldflagsr   r   r   test_dlopenflags,  s    zSysModuleTest.test_dlopenflagsc                 C   sd   |  ttj td }d a| td |d  b| td | ttdr`| t t	 d S )NrA   gettotalrefcount)
r   r   r	   getrefcountrc   r   r   r]   r   r   )r   cr   r   r   test_refcount7  s    

zSysModuleTest.test_refcountc                 C   s>   |  ttjdd |  ttjd | tjjt j	u  d S )Nr   i 5w)
r   r   r	   	_getframer#   r   rG   test_getframe__code__f_coderI   r   r   r   r   G  s    zSysModuleTest.test_getframec                    sr  dd l dd l}   g fdd} fddj|d}|     | td d }t	 }|D ]}| 
|t | |d q }| || | || ||}| |t u  ||}||}	t|	D ]\}
\}}}}|dkr q"q| d	 | |d
 |	|
d  \}}}}| |d | |ddg   |  d S )Nr   c                      s
      d S r!   r   r   )g456r   r   f123\  s    z/SysModuleTest.test_current_frames.<locals>.f123c                      s"           d S r!   )append	get_identsetwaitr   )	entered_gleave_gthread_info	threadingr   r   r   _  s    z/SysModuleTest.test_current_frames.<locals>.g456)targetrA   r   z)didn't find f123() on thread's call stackzg456()r   zleave_g.wait()zentered_g.set())r   	tracebackEventThreadstartr   r   r   r	   _current_framesr]   r   assertGreaterr   r>   popr   r   extract_stack	enumerateZfailr   join)r   r   r   tZ	thread_iddtidZmain_idframestackifilenamelinenofuncnameZ
sourceliner   )r   r   r   r   r   r   test_current_framesP  sB    



z!SysModuleTest.test_current_framesc                 C   s|  |  tjt |  tjt | tjd |  tjt	 |  tj
t |  tjt |  tjt |  tjt | ttjd | tjjd | ttjd | tjjd dk | tjjdk | tjjd | tjjd	 | tjjtjj | ttjjt | ttjjt |  tjjt |  tjjt |  tjt | ttjd
 | tjjdtjj   t!dd	D ]2}| t"|tjjd tjjdd#tjj q|  tjj$t |  tjj%t |  tjj&t t'(d}tjj)dv r|| tjj*ddh | tjj+h d |dkrJ| tjj)d n0|dkrf| tjj)d n| tjj)ddh n| |d | tjj,d | tjj,d |  tj-t |  tj.t | tj.d |  tj/t |  tj0t |  tj1t |  tj2t |  tj3t tj4}|  |d d  t	 | t|d |  |d t |  |d t |  |d t | |d d |  |d t |  |j5t |  |j6t |  |j7t | |j8d |  |j9t | |d |j5 | |d |j6 | |d |j7 | |d |j8 | |d |j9 | |dk |  tj:t | tj:d tj/;dsx|  tj<t d S )N)littlebig   r   r   r   r   rA   i  rp   	   z'sys.hash_info.modulus {} is a non-primeZPy_HASH_ALGORITHM>   fnv	siphash24    @   >   r   r      r   r      i r   alphabeta	candidatefinal)rA   r   r   )Zshortlegacywin)=r]   r	   api_versionr   argvlistr>   	byteorderbuiltin_module_namestuple	copyrightr^   exec_prefixbase_exec_prefix
executabler   r   
float_inforadixint_infor   bits_per_digitsizeof_digitassertGreaterEqualdefault_max_str_digitsstr_digits_check_thresholdr   r   
hexversion	hash_info
assertLessmoduluswidthrangepowformatinfnanimag	sysconfigZget_config_var	algorithm	hash_bits	seed_bitscutoffmaxsize
maxunicoder   prefixbase_prefix
platlibdirversionversion_infor   r   microreleaselevelserialfloat_repr_stylerQ   abiflags)r   xZalgovir   r   r   test_attributes  s    



zSysModuleTest.test_attributesc                 C   s6   t j}| t|d | |jd | |jd d S )Nr   )ntZpthreadZsolarisN)	semaphorez
mutex+condN)r	   r   r   r   r>   namelock)r   infor   r   r   test_thread_info  s    zSysModuleTest.test_thread_infoc                 C   s   |  tjjtjj d S r!   )r   r	   
__stdout__encoding
__stderr__rI   r   r   r   
test_43581  s    zSysModuleTest.test_43581c                 C   s~   t d7 a | ttj dtt  }| t||u  |  }| t||u  G dd dt}| ttj|d d S )NrA   znever interned beforec                   @   s   e Zd Zdd ZdS )z$SysModuleTest.test_intern.<locals>.Sc                 S   s   dS )Nr7   r   rI   r   r   r   __hash__  s    z-SysModuleTest.test_intern.<locals>.S.__hash__N)r(   r)   r*   r	  r   r   r   r   S  s   r
  abc)INTERN_NUMRUNSr   r   r	   internr^   r   swapcase)r   ss2r
  r   r   r   test_intern  s    zSysModuleTest.test_internc                 C   s   |  tj d}|D ]B}|  ttj|| |dkr8tnt}| tttj||| q|  t	tj | t
tjt
| | tjjh d d S )N)debuginspectinteractiveoptimizedont_write_bytecodeno_user_siteno_siteignore_environmentverbosebytes_warningquiethash_randomizationisolateddev_mode	utf8_modeint_max_str_digitsr  >   r   rA   r   )r   r	   flagsr   boolr   r   r   getattrreprr   r>   r   )r   attrsattr	attr_typer   r   r   test_sys_flags  s    zSysModuleTest.test_sys_flagsc                 C   sp   t |}| t |  W d    n1 s.0    Y  | t || W d    n1 sb0    Y  d S r!   )r   r   r   __new__)r   Zsys_attrr(  r   r   r   assert_raise_on_new_sys_type  s
    $z*SysModuleTest.assert_raise_on_new_sys_typec                 C   s   |  tj d S r!   )r+  r	   r"  rI   r   r   r   test_sys_flags_no_instantiation"  s    z-SysModuleTest.test_sys_flags_no_instantiationc                 C   s   |  tj d S r!   )r+  r	   r   rI   r   r   r   &test_sys_version_info_no_instantiation%  s    z4SysModuleTest.test_sys_version_info_no_instantiationc                 C   s    t jtd | t  d S )Nr   )rC   r   r   r	   r+  r   rI   r   r   r   +test_sys_getwindowsversion_no_instantiation(  s    z9SysModuleTest.test_sys_getwindowsversion_no_instantiationc                 C   s   t   d S r!   )r	   _clear_type_cacherI   r   r   r   test_clear_type_cache-  s    z#SysModuleTest.test_clear_type_cachec                 C   s  t tj}d|d< tjtjddgtj|d}| d 	 }dtj
 d}| || d|d< tjtjddgtj|d}| d 	 }| |d	 d
|d< tjtjddgtjtj|d}| \}}| |d | d| | d| d|d< tjtjddgtjtj|d}| \}}| |d | d| | d| d|d< tjtjddgtj|d}| d 	 }| |d d S )Ncp424rV   rK   zprint(chr(0xa2))r   envr      ¢zascii:replace   ?rR   )r   r@   r3  rL   s   UnicodeEncodeError:s   '\xa2'zascii:z:surrogateescapezprint(chr(0xdcbd))   )dictosenviron
subprocessPopenr	   r   PIPEcommunicatestriplinesepencoder   r>   )r   r3  pr   rS   r3   r   r   r   test_ioencoding1  sL    
zSysModuleTest.test_ioencodingz*requires OS support of non-ASCII encodingsFz$requires FS encoding to match localec                 C   s^   t tj}d|d< tjtjddtjj	 gtj
|d}| d  }| |ttjj	 d S )Nr   rV   rK   z	print(%a)r2  r   )r7  r8  r9  r:  r;  r	   r   rC   r   FS_NONASCIIr<  r=  r>  r   fsencode)r   r3  rA  r   r   r   r   test_ioencoding_nonascii\  s    


z&SysModuleTest.test_ioencoding_nonasciizTest is not venv-compatiblec              	   C   s   |  tjtjtj tjtjtj}tj	g dtjtj
|d}| d }| d}|  | |dttjddg d S )N)ZnonexistentrK   zEimport sys; print(sys.executable.encode("ascii", "backslashreplace")))r   r   cwdr   ASCIIzb''rR   backslashreplace)r   r8  pathabspathr	   r   dirnamerealpathr:  r;  r<  r=  r>  decoder   r>   r%  r@  )r   Z
python_dirrA  r   r   r   r   r   test_executablej  s    
zSysModuleTest.test_executableNc                 C   s(   |  | t| |r$| || d S r!   )ZassertIsNotNonecodecslookupr   r   Zfs_encodingrS   r   r   r   check_fsencoding  s    

zSysModuleTest.check_fsencodingc                 C   s,   t  }t jdkrd}nd }| || d S )Ndarwinzutf-8)r	   getfilesystemencodingr   rR  rQ  r   r   r   test_getfilesystemencoding  s
    
z(SysModuleTest.test_getfilesystemencodingc           
      C   s   t j }||d< d|d< dd}tjddd|g}|rB|d	 |d urT||d
< n|d
d  tj	|tj
tj|dd}| \}}	|S )NLC_ALL0ZPYTHONCOERCECLOCALE
)
import syszdef dump(name):z    std = getattr(sys, name)z(    print("%s: %s" % (name, std.errors))zdump("stdin")zdump("stdout")zdump("stderr")z-Xzutf8=0rK   z-IrV   T)r   r@   r3  universal_newlines)r8  r9  copyr   r	   r   r   r   r:  r;  r<  STDOUTr=  )
r   localer  r  r3  r   argsrA  r   r@   r   r   r   c_locale_get_error_handler  s$    

	

z(SysModuleTest.c_locale_get_error_handlerc                 C   s   | j |dd}| |d | j |dd}| |d | j |dd}| |d | j |d	d}| |d | j |d
d}| |d | j |dd}| |d d S )NT)r  zHstdin: surrogateescape
stdout: surrogateescape
stderr: backslashreplace
z:ignore)r  z6stdin: ignore
stdout: ignore
stderr: backslashreplace
z	iso8859-1z6stdin: strict
stdout: strict
stderr: backslashreplace
z
iso8859-1::r   )r_  r   )r   r]  r   r   r   r   check_locale_surrogateescape  s0    z*SysModuleTest.check_locale_surrogateescapec                 C   s   |  d d S )NCra  rI   r   r   r   test_c_locale_surrogateescape  s    z+SysModuleTest.test_c_locale_surrogateescapec                 C   s   |  d d S )NZPOSIXrc  rI   r   r   r   !test_posix_locale_surrogateescape  s    z/SysModuleTest.test_posix_locale_surrogateescapec                 C   s   ddddd}|  ttjd |  ttjd |  ttjd |  ttjd	 tjj}| |d d
 |j|jf |jd> |jd> B |jd> B ||j	 d> B |j
d> B }| tjj| | tjjtjj  d S )Nrl   r         r   r  r   r   	cache_tagr         r   r   r   )r   r   r	   implementationr   r   r   r   r   r   r   r   r  lower)r   Zlevelsr   r   r   r   r   test_implementation  s$    

z!SysModuleTest.test_implementationc                 C   sB   ddl m} ddg}|| \}}}| d| | ttjd d S )Nr   )r   rK   z#import sys; sys._debugmallocstats()s   free PyDictObjectsT)test.support.script_helperr   r>   r   r   r	   _debugmallocstats)r   r   r^  retr   r3   r   r   r   test_debugmallocstats  s
    z#SysModuleTest.test_debugmallocstatsgetallocatedblocksz0sys.getallocatedblocks unavailable on this buildc              
   C   s  zdd l }W n ty&   t }Y n@0 z| }W n( ty\ } zd}W Y d }~nd }~0 0 |dv }t }| t	|t
 |r| |d n| |d z| |t  W n ty   Y n0 t  t }| || t  t }| |t|d |d  d S )Nr   T)ZpymallocZpymalloc_debugrn   )	_testcapiImportErrorr   with_pymallocZpymem_getallocatorsnamer   r	   rr  assertIsr   r   r   r   r   r   AttributeErrorgcZcollectZassertLessEqualr>   r   )r   rs  ru  Z
alloc_namer2   r`   br   r   r   r   test_getallocatedblocks  s0    z%SysModuleTest.test_getallocatedblocksc                 C   s8   |  t d d}td|\}}}| | d d S )NFa  if 1:
            import sys

            class AtExit:
                is_finalizing = sys.is_finalizing
                print = print

                def __del__(self):
                    self.print(self.is_finalizing(), flush=True)

            # Keep a reference in the __main__ module namespace, so the
            # AtExit destructor will be called at Python exit
            ref = AtExit()
        rK   s   True)rv  r	   is_finalizingr   r   rstripr   r   rT   r   r@   r   r   r   test_is_finalizing!  s    z SysModuleTest.test_is_finalizingc                 C   s@   d}t d|\}}}| }| d|d  | d|d  d S )Nzif 1:
            import sys
            class A:
                def __del__(self, sys=sys):
                    print(sys.flags)
                    print(sys.float_info)
            a = A()
            rK   s	   sys.flagsr   s   sys.float_inforA   )r   
splitlinesr>   r   r   rT   r   r3   r   r   r   test_issue206026  s
    zSysModuleTest.test_issue20602c                 C   sD   d}t d|\}}}| |d | | d | | d d S )Nzif 1:
            import struct, sys

            class C:
                def __init__(self):
                    self.pack = struct.pack
                def __del__(self):
                    self.pack('I', -42)

            sys.x = C()
            rK   r   rL   )r   r   r|  r}  r   r   r   &test_sys_ignores_cleaning_up_user_dataE  s
    z4SysModuleTest.test_sys_ignores_cleaning_up_user_datagetandroidapilevelzneed sys.getandroidapilevel()c                 C   s$   t  }| |t | |d d S )Nr   )r	   r  r]   r   r   )r   levelr   r   r   test_getandroidapilevelV  s    z%SysModuleTest.test_getandroidapilevelc                    s   d  fdd}g d}|d| |d| |d|d d |dd    |d|d d |dd    |d	|d
 g |d
|d
 g |dd> | |d
d> |d
 g |d | d S )Nzif 1:
            import sys
            def f1():
                1 / 0
            def f2():
                f1()
            sys.tracebacklimit = %r
            f2()
        c                    s<   t jtjd |  gt jd}| d }| | d S )NrK   )r@   rA   )r:  r;  r	   r   r<  r=  r   r  )tracebacklimitrS   rA  r   r   r   r   r   checkg  s
    z4SysModuleTest.test_sys_tracebacklimit.<locals>.check)s"   Traceback (most recent call last):s&     File "<string>", line 8, in <module>s      File "<string>", line 6, in f2s      File "<string>", line 4, in f1s#   ZeroDivisionError: division by zerorl   r   r   rA   r   rr   r   )r   r  r   r   r  r   test_sys_tracebacklimit]  s    	

z%SysModuleTest.test_sys_tracebacklimitc                 C   s    |  ttjtttj d S r!   )r   r   r	   	meta_pathr   rI   r   r   r   test_no_duplicates_in_meta_path~  s    z-SysModuleTest.test_no_duplicates_in_meta_pathZ_enablelegacywindowsfsencodingz*needs sys._enablelegacywindowsfsencoding()c                 C   s:   d}t dd|\}}}|dd }| |d d S )N)rY  z$sys._enablelegacywindowsfsencoding()zCprint(sys.getfilesystemencoding(), sys.getfilesystemencodeerrors())rK   z; rR   replacezmbcs replace)r   r   rM  r|  r   r  r   r   r   #test__enablelegacywindowsfsencoding  s    z1SysModuleTest.test__enablelegacywindowsfsencoding)N)FN)=r(   r)   r*   rJ   r[   r_   rd   rh   ru   rC   r   cpython_onlyr~   r   r   unittestZ
skipUnlessr   r	   r   Zrefcount_testr   r   Zreap_threadsr   r   r  r  r  r)  r+  r,  r-  r.  r0  rB  rC  rT  r]  getpreferredencodingrE  ZskipIfr   r   rN  rR  rU  r_  ra  rd  re  rm  rq  rz  r~  r  r  r  r  r  r  r   r   r   r   rG   o   s   B	

	
	
@X	
+




&


'
!rG   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )UnraisableHookTestc                 C   sT   dd l }dd l}d| }z.|||| |jt|||j||dW d }S d }0 d S )Nr   Exception ignored )exc_type	exc_valueexc_tracebackerr_msgobject)rs  typeswrite_unraisable_excSimpleNamespacer   __traceback__)r   r2   r  r$   rs  r  Zerr_msg2r   r   r   r    s    

z'UnraisableHookTest.write_unraisable_excc                 C   s  dD ]}| j |d d}tjdT}tjtdtj" | td|| W d    n1 sb0    Y  W d    n1 s0    Y  |	 }|d ur| 
d| d|d	| n| 
d
|d	| | 
d| | 
d| W d    q1 s0    Y  qd S )N)Nzoriginal hook)r  z	an objectr@   unraisablehookr   r  z: rX  zException ignored in: #Traceback (most recent call last):
r,   )subTestrC   r   rD   r&   r	   __unraisablehook__r  r#   r   r>   )r   r  r$   r@   r3   r   r   r   test_original_unraisablehook  s    
Nz/UnraisableHookTest.test_original_unraisablehookc                    s\  G dd d}G dd dt  G  fddd}||fD ]}| | | }tj D}tjtdtj ~W d    n1 s0    Y  W d    n1 s0    Y  | }| 	d| | 	|j
j| | 	d	| | 	d
| ||u r| 	d| | 	d| n| 	d| | 	d| | |d W d    q81 sL0    Y  q8d S )Nc                   @   s   e Zd Zdd ZdS )zFUnraisableHookTest.test_original_unraisablehook_err.<locals>.BrokenDelc                 S   s   t d}|d S )Ndel is brokenr"   r   r2   r   r   r   __del__  s    zNUnraisableHookTest.test_original_unraisablehook_err.<locals>.BrokenDel.__del__Nr(   r)   r*   r  r   r   r   r   	BrokenDel  s   r  c                   @   s   e Zd Zdd ZdS )zOUnraisableHookTest.test_original_unraisablehook_err.<locals>.BrokenStrExceptionc                 S   s   t dd S )Nzstr() is broken	ExceptionrI   r   r   r   __str__  s    zWUnraisableHookTest.test_original_unraisablehook_err.<locals>.BrokenStrException.__str__N)r(   r)   r*   r  r   r   r   r   BrokenStrException  s   r  c                       s   e Zd Z fddZdS )zOUnraisableHookTest.test_original_unraisablehook_err.<locals>.BrokenExceptionDelc                    s     }|d S r!   r   r  r  r   r   r    s    zWUnraisableHookTest.test_original_unraisablehook_err.<locals>.BrokenExceptionDel.__del__Nr  r   r  r   r   BrokenExceptionDel  s   r  r  zException ignoredztest_sys.pyz	raise excz<exception str() failed>r#   r  rX  )r  r  rC   r   r-   r&   r	   r  r   r>   r  r*   r   r0   )r   r  r  Z
test_classr$   r@   reportr   r  r    test_original_unraisablehook_err  s.    
>
z3UnraisableHookTest.test_original_unraisablehook_errc              	   C   s   G dd d}t j V}t jtdtj$ | |j dd}W d    n1 sV0    Y  W d    n1 st0    Y  |	 }d}| 
| d| d S )Nc                   @   s   e Zd ZG dd dZdS )zMUnraisableHookTest.test_original_unraisablehook_exception_qualname.<locals>.Ac                   @   s   e Zd ZG dd deZdS )zOUnraisableHookTest.test_original_unraisablehook_exception_qualname.<locals>.A.Bc                   @   s   e Zd ZdS )zQUnraisableHookTest.test_original_unraisablehook_exception_qualname.<locals>.A.B.XNr(   r)   r*   r   r   r   r   X  s   r  N)r(   r)   r*   r  r  r   r   r   r   B  s   r  N)r(   r)   r*   r  r   r   r   r   A  s   r  r  r6   r$   /test_original_unraisablehook_exception_qualnamez.<locals>.A.B.X)rC   r   r-   r&   r	   r  r  r  r  r   r>   )r   r  r@   rS   r  ZtestNamer   r   r   r    s    
@zBUnraisableHookTest.test_original_unraisablehook_exception_qualnamec              	   C   sr   t d}tjtdtjD | t t| W d    n1 sF0    Y  W d    n1 sd0    Y  d S )Nr   r  )	r#   rC   r   r&   r	   r  r   r   r  r  r   r   r   'test_original_unraisablehook_wrong_type  s    
z:UnraisableHookTest.test_original_unraisablehook_wrong_typec                    s   d   fdd}t  }zztjtd|N | tdd|}d D ]"}| t	 |t	|| |f qDW d    n1 s|0    Y  W d }d  n
d }d  0 d S )Nc                    s   |  d S r!   r   r^  Z	hook_argsr   r   	hook_func  s    z@UnraisableHookTest.test_custom_unraisablehook.<locals>.hook_funcr  r   zcustom hookz/exc_type exc_value exc_traceback err_msg object)
r  rC   r   r&   r	   r  r#   splitr   r$  )r   r  r$   rS   r'  r   r  r   test_custom_unraisablehook  s"    
&z-UnraisableHookTest.test_custom_unraisablehookc              	   C   s   dd }t jdR}t jtd|" | tddd  W d    n1 sN0    Y  W d    n1 sl0    Y  | }| d|d| | d	| | d
| d S )Nc                  W   s   t dd S )Nzhook_func failedr  r  r   r   r   r    s    zEUnraisableHookTest.test_custom_unraisablehook_fail.<locals>.hook_funcr@   r  r   zcustom hook failz)Exception ignored in sys.unraisablehook: rX  r  zException: hook_func failed
)	rC   r   rD   r&   r	   r  r#   r   r>   )r   r  r@   r3   r   r   r   test_custom_unraisablehook_fail  s    
@z2UnraisableHookTest.test_custom_unraisablehook_failN)
r(   r)   r*   r  r  r  r  r  r  r  r   r   r   r   r    s   (r  c                       sl   e Zd Zdd ZejjZdd Zdd Zdd Z	 fd	d
Z
dd Zdd Zdd Zdd Zdd Z  ZS )
SizeofTestc                 C   s*   t d| _tjj| _dd l}|j| _	d S )NPr   )
structcalcsizer  r	   r   r   	longdigitr}   ZSIZEOF_PYGC_HEADgc_headsize)r   r}   r   r   r   setUp  s    
zSizeofTest.setUpc                 C   sH   t jj}| j}| td|d| j  | tg |d|  d S )NTr   Pn)rC   r   calcvobjsizer  r   r	   	getsizeofr  )r   vsizeZgc_header_sizer   r   r   test_gc_head_size  s    zSizeofTest.test_gc_head_sizec                 C   s  G dd d}|  ttj|  G dd d}|  ttj|  dg}| t| || G dd d}|  ttj|  | t| || G dd	 d	t}| t|tjtj| j	  |  t
$ t|tjd
  W d    n1 s0    Y  |  t t|d W d    n1 s00    Y  |  tt
f& t|tj d
  W d    n1 sv0    Y  d S )Nc                   @   s   e Zd Zdd ZdS )z)SizeofTest.test_errors.<locals>.BadSizeofc                 S   s   t d S r!   r"   rI   r   r   r   
__sizeof__)  s    z4SizeofTest.test_errors.<locals>.BadSizeof.__sizeof__Nr(   r)   r*   r  r   r   r   r   	BadSizeof(  s   r  c                   @   s   e Zd Zdd ZdS )z-SizeofTest.test_errors.<locals>.InvalidSizeofc                 S   s   d S r!   r   rI   r   r   r   r  .  s    z8SizeofTest.test_errors.<locals>.InvalidSizeof.__sizeof__Nr  r   r   r   r   InvalidSizeof-  s   r  sentinelc                   @   s   e Zd Zdd ZdS )z+SizeofTest.test_errors.<locals>.FloatSizeofc                 S   s   dS )Ng      @r   rI   r   r   r   r  5  s    z6SizeofTest.test_errors.<locals>.FloatSizeof.__sizeof__Nr  r   r   r   r   FloatSizeof4  s   r  c                   @   s   e Zd Zdd ZdS )z.SizeofTest.test_errors.<locals>.OverflowSizeofc                 S   s   t | S r!   )r   rI   r   r   r   r  ;  s    z9SizeofTest.test_errors.<locals>.OverflowSizeof.__sizeof__Nr  r   r   r   r   OverflowSizeof:  s   r  rA   r  )r   r#   r	   r  r   rv  r   r   r   r  OverflowError)r   r  r  r  r  r  r   r   r   test_errors'  s&    
2.zSizeofTest.test_errorsc                 C   sF   t jj}| td|d| j  | tdd|d| j  d S )NTr   r  )rC   r   r  r   r	   r  r  )r   sizer   r   r   test_defaultF  s    zSizeofTest.test_defaultc           '   	      sJ	  t j}tjjtjj}j  d|dj   td ddd g}|D ]"}t	|} ||d|
   qN tt	 d  d|d	d
   d|d	d  dd } | jd d fdd}|| jd ||jd dd }||jd|d	   tdd
d  tjd dd l}	 |	jjd dd l}
 |
jjd  tjd  i jd  i d  dd
id|d d d|d    d
d!d"d#dd$d%dd&} |d|d d' d(|d     i  d  i  d  i  d  ti d)  ti  d)  ti  d)  ti  d) G d*d+ d+t} |jd  t d,  t dddddd-  t!dddddd-  t"ddd
dd-  t#d dd l$}dd l%}|&|j'j(} |d.  t)g d/  t*dd  t+dd0  t,j-|dj.tt,j-   dd l/}d1}|0 }t|j1j2}t|j1j3}|j1j4|j1j5 | | d
 } ||d2|d3  d |d   d4d5 } |d6 G  fd7d8d8}d9d: } | d;  td<d= dd l6} |7ddd g g d>g d?g}|D ]&} t8||d@t|j.   q tg d=  t*g d  d|d  d
|dj   dA|dj  d!t,j9j: } t||dd!j    t|d! d
 |dd!j    t|d! |dd"j    t;dB  d d  t<d  t d G  fdCd+d+t} tt=d
dD  t*dd  t=d
dE  t=dFdE d}g t=d(t=dGg}dH|d  dI }|D ]}t|}|dkr\d
}|d! }|}||kr~|d
> }qh|dkr t>||  t?|| n4 t>||||d    t?||||d   qB tt> dJ  t@ddK  tAtdK  dL|d  d>|dd"j.   dM}||} t| ||dN }G dOdP dPt} |||dQ d d|d     | jddj.   | } d
 | _B | _C | _D | _E | _F | _G | _H| _I |||dQ d' d(|d     | jdd(j.   g dR}dS}!|!dT }"|"d }#|D ]}tJtK|}$|$dUk rp|!t| d
 }%n\|$dVk r|"t| d
 }%n<|$dWk r|"d!t|d
   }%n|"d#t|d
   }% ||% q@tLdX} ||"d#  tM|dYdZ  ||"d# d#  dd lN}& |&Otd[  |&Ptd[ d S )\NTr   Z5PrL      ui Zn2PiZnPrc   rA   s
   xxxxxxxxxxr   c                     s   d  fdd} | S )Nr   c                      s    S r!   r   r   r   r   r   innerd  s    z<SizeofTest.test_objecttypes.<locals>.get_cell.<locals>.innerr   )r  r   r  r   get_cellb  s    z-SizeofTest.test_objecttypes.<locals>.get_cellr   r  c                    s     t| | d S r!   )r   r	   r  )r`   Zexpected_sizerI   r   r   check_code_sizei  s    z4SizeofTest.test_objecttypes.<locals>.check_code_sizeZ6i13Pc                    s    fdd}|S )Nc                      s    S r!   r   r   r  r   r   r  n  s    z=SizeofTest.test_objecttypes.<locals>.get_cell2.<locals>.innerr   )r   r  r   r  r   	get_cell2m  s    z.SizeofTest.test_objecttypes.<locals>.get_cell2Z2dZ3PPPZ3PPZ3P2PZ2PZnQ2Pr`   Z2nP2nr   r   Zn2Pr   r   r         )rA   r   r   r   r   r  r  r   rj  rl   ZP2nPnc                   @   s   e Zd ZdS )&SizeofTest.test_objecttypes.<locals>.CNr  r   r   r   r   rb    rL   rb  Z5Pbz	5Pb 2P2nPZ32B2iBZn3Pr      Z	5P2c4P3icZ3ic                   S   s   d S r!   r   r   r   r   r   func  rL   z)SizeofTest.test_objecttypes.<locals>.funcZ13Pc                       s@   e Zd Zedd Zedd Z ed  ed dS )z&SizeofTest.test_objecttypes.<locals>.cc                   S   s   d S r!   r   r   r   r   r   foo  s    z*SizeofTest.test_objecttypes.<locals>.c.fooc                 S   s   d S r!   r   )clsr   r   r   bar  s    z*SizeofTest.test_objecttypes.<locals>.c.barZPPN)r(   r)   r*   staticmethodr  classmethodr  r   r  r  r   r   r     s   

r   c                   s   s
   dV  d S )NrA   r   r   r   r   r   get_gen  rL   z,SizeofTest.test_objecttypes.<locals>.get_genZPb2PPP4Pr  ZlP)rA   r   r   )rB   23r  r  ZPnPPPc                       s@   e Zd Zdd Zdd Zdd ZeeeedZ ed d	S )
r  c                 S   s   | j S r!   Z_C__xrI   r   r   r   getx  rL   z+SizeofTest.test_objecttypes.<locals>.C.getxc                 S   s
   || _ d S r!   r  )r   valuer   r   r   setx  rL   z+SizeofTest.test_objecttypes.<locals>.C.setxc                 S   s   | ` d S r!   r  rI   r   r   r   delx  rL   z+SizeofTest.test_objecttypes.<locals>.C.delxr   Z4PiN)r(   r)   r*   r  r  r  propertyr   r   r  r   r   rb    s
   Z4lZ4Pi rn   Z3nPZ2nPZP3n3Pr   ZP2nPI13Pl4Pn9Pn11PIPPZ3P36P3P10P2P5Pc                   @   s   e Zd ZdS )z2SizeofTest.test_objecttypes.<locals>.newstyleclassNr  r   r   r   r   newstyleclass$  rL   r  Z2nP2n0P)Zd1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111ud   ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿuP   ĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀĀu,  ￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿ux   𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀𐀀u  􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿􏿿ZnnbPZnPnr      i   i @  z<stdin>r   Z2Pn2P)Qr  r  rC   r   calcobjsizer  check_sizeofr  r   	bytearray	__alloc__iter__closure__r   complexr^   rl  datetimeZ	timedeltaZdayscollectionsdefaultdictdefault_factoryr   __add____iter__keysvaluesitemsr  __dict__BaseExceptionUnicodeEncodeErrorUnicodeDecodeErrorUnicodeTranslateErrorEllipsisrO  Zencodings.iso8859_3charmap_build	iso8859_3Zdecoding_tabler   reversedfloatr	   r   r  r  currentframer   co_cellvarsco_freevarsco_stacksize
co_nlocalsrefinditerr   r   r   r  NotImplementedr   r   	frozensetslicesuperr`   ry  r   r   erk   ghordmaxchrr   weakrefrefproxy)'r   r  r  Zsamplessampler   r  r  r  r  r  Zlongdictrb  rO  	encodingsr  ZCO_MAXBLOCKSZncellsZnfreesextrasr  r   r  r  ZPyLong_BASEZPySet_MINSIZEr  ZminusedtmpZnewsizefmtr  oZasciifieldsZcompactfieldsZunicodefieldsZmaxcharLr  	__class__)r  r   r  r   test_objecttypesK  s*   *& "$$ 	


"0"


zSizeofTest.test_objecttypesc                 C   sH   t |t| }t|r2t|s2|| j7 }| t || d S r!   )r	   r  r  r  rx  Z
is_trackedr  r   )r   r$   baseextrarS   r   r   r   check_slotsU  s    
zSizeofTest.check_slotsc           	      C   s   | j }G dd dt}|| t d G dd dt}||g ddg id G dd	 d	t}|| g d G d
d dt}|| t d G dd dt}|| t d ddlm} G dd d|}||g d|g dd d S )Nc                   @   s   e Zd ZdZdS )z!SizeofTest.test_slots.<locals>.BAr`   ry  r   Nr(   r)   r*   	__slots__r   r   r   r   BA_  s   r-  r  c                   @   s   e Zd ZdZdS )z SizeofTest.test_slots.<locals>.Dr*  Nr+  r   r   r   r   Db  s   r.  r  r   c                   @   s   e Zd ZdZdS )z SizeofTest.test_slots.<locals>.Lr*  Nr+  r   r   r   r   r#  e  s   r#  c                   @   s   e Zd ZdZdS )z SizeofTest.test_slots.<locals>.Sr*  Nr+  r   r   r   r   r
  h  s   r
  c                   @   s   e Zd ZdZdS )z!SizeofTest.test_slots.<locals>.FSr*  Nr+  r   r   r   r   FSk  s   r/  r   )OrderedDictc                   @   s   e Zd ZdZdS )z!SizeofTest.test_slots.<locals>.ODr*  Nr+  r   r   r   r   ODo  s   r1  )r)  r  r7  r   r   r  r  r0  )	r   r  r-  r.  r#  r
  r/  r0  r1  r   r   r   
test_slots[  s    zSizeofTest.test_slotsc                 C   s   t jj}t jj}| j}dd l}|| |d ztW n4 tyl   t	 d }|d urh|||d Y n0 |tj
|d| jttj
   d S )Nr   r  r   Z2P2ir   )rC   r   r  r  r  _astZASTr   r	   r/   r"  r  r   )r   r  r  r  r3  tbr   r   r   test_pythontypess  s    zSizeofTest.test_pythontypesc                 C   s   t  }| |j | |j dd }t j|d t  }| |j| | |d | | |jd  | |d d  dd }t j|d t  }| |j| | |d | | |j| | |d | t j|  t  }| |j | |j d S )Nc                  W   s   d S r!   r   r`   r   r   r   <lambda>  rL   z0SizeofTest.test_asyncgen_hooks.<locals>.<lambda>)	firstiterr   rA   c                  W   s   d S r!   r   r6  r   r   r   r7    rL   )	finalizer)r	   get_asyncgen_hooksrX   r8  r9  set_asyncgen_hooksrv  )r   oldr8  Zhooksr9  Zcurr   r   r   test_asyncgen_hooks  s*    
zSizeofTest.test_asyncgen_hooksc                 C   s6   t d}td|\}}}| |d | |d d S )Nz
            import sys
            class MyStderr:
                def write(self, s):
                    sys.stderr = None
            sys.stderr = MyStderr()
            1/0
        rK   rL   )textwrapdedentr   r   r  r   r   r   /test_changing_sys_stderr_and_removing_reference  s    
z:SizeofTest.test_changing_sys_stderr_and_removing_reference)r(   r)   r*   r  rC   r   r  r  r  r  r&  r)  r2  r5  r=  r@  __classcell__r   r   r$  r   r    s   	  r  __main__)rC   r   rn  r   r   r   rO  rx  r]  r   r8  r  r:  r	   r   Ztest.supportr>  r  r9   r  ZTestCaser   r+   rG   r  r  r  r(   mainr   r   r   r   <module>   sF   /(      #     
