a
    äze™“  ã                   @   sä  d dl 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mZ d dlmZ d dlmZmZ zd dlZW n ey¾   dZY n0 e d¡Zd dlZeedƒZdd	„ ZG d
d„ dƒZG dd„ dej ƒZ!G dd„ dej ƒZ"G dd„ dej ƒZ#G dd„ dej ƒZ$G dd„ dej ƒZ%G dd„ dej ƒZ&G dd„ dej ƒZ'G dd„ de'ƒZ(e )e *¡ d¡G dd„ de'ƒƒZ+e )ed¡G d d!„ d!e'ƒƒZ,G d"d#„ d#ej ƒZ-e.d$kràe /¡  dS )%é    )ÚOrderedDictN)Úsupport)ÚMISSING_C_DOCSTRINGS)Úassert_python_failureÚassert_python_okÚ	_testcapiZgettotalrefcountc                 C   s   | S )zsome doc© ©Úselfr   r   ú$/usr/lib/python3.9/test/test_capi.pyÚtestfunction#   s    r   c                   @   s    e Zd Ze e¡Ze e¡ZdS )ÚInstanceMethodN)Ú__name__Ú
__module__Ú__qualname__r   ZinstancemethodÚidr   r   r   r   r   r   (   s   
r   c                   @   sf  e Zd Zdd„ Zdd„ Zdd„ Zdd„ Ze e	d	¡d
d„ ƒZ
e e	d	¡dd„ ƒZe ed¡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 eed!ƒd"¡d#d$„ ƒZd%d&„ Ze d'¡d(d)„ ƒZe d'¡d*d+„ ƒZd,d-„ Zd.d/„ Z d0d1„ Z!d2d3„ Z"d4d5„ Z#d6d7„ Z$d8d9„ Z%d:d;„ Z&d<d=„ Z'd>d?„ Z(d@dA„ Z)dBdC„ Z*dDdE„ Z+dFdG„ Z,dHdI„ Z-dJS )KÚCAPITestc                 C   s~   t ƒ }|  t|ƒ| ¡ ¡ |  | ¡ |u ¡ |  |jjtj¡ |  t jjtj¡ dt j_|  tjd¡ |  tt	|jdd¡ d S )NÚtestÚ	attribute)
r   ÚassertEqualr   Ú
assertTruer   Ú__doc__r   ÚassertRaisesÚAttributeErrorÚsetattr©r
   Úinstr   r   r   Útest_instancemethod.   s    zCAPITest.test_instancemethodc                 C   sv   t  ¡ , tjtjddgtjtjd}W d   ƒ n1 s:0    Y  | ¡ \}}|  |d¡ |  	| 
¡  d¡|¡ d S )Nú-cz4import _testcapi;_testcapi.crash_no_current_thread())ÚstdoutÚstderró    s—   Fatal Python error: PyThreadState_Get: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL))r   ÚSuppressCrashReportÚ
subprocessÚPopenÚsysÚ
executableÚPIPEÚcommunicater   r   ÚrstripÚ
startswith)r
   ÚpÚoutÚerrr   r   r   Ú test_no_FatalError_infinite_loop9   s    

ÿü$ÿúz)CAPITest.test_no_FatalError_infinite_loopc                 C   s   |   ttj¡ d S ©N)r   Ú
ValueErrorr   Z!make_memoryview_from_NULL_pointerr	   r   r   r   Ú!test_memoryview_from_NULL_pointerK   s    z*CAPITest.test_memoryview_from_NULL_pointerc           
   
   C   sà   t dƒ}tdƒ}z|‚W n¸ t yÐ } z |j}t ¡ }t |j|d ¡}t ¡ }tj|Ž }t ¡ }	|  |d |¡ |  	||j||f¡ |  	||¡ |  	|	|¡ |  	||j|d f¡ |  	||¡ W Y d }~nd }~0 0 |  
d¡ d S )NÚ5ZTESTé   F)r0   Ú	TypeErrorÚ__traceback__r%   Úexc_infor   Zset_exc_infoÚ	__class__r   ZassertSequenceEqualr   )
r
   Zraised_exceptionÚnew_excÚeÚtbZorig_sys_exc_infoZorig_exc_infoZnew_sys_exc_infoZnew_exc_infoZreset_sys_exc_infor   r   r   Útest_exc_infoN   s$    
"zCAPITest.test_exc_infoz(_posixsubprocess required for this test.c                 C   s˜   G dd„ dt ƒ}|  ttjd|ƒ dddddd	d
dddddddddddd¡ G dd„ dt ƒ}|  ttjd|ƒ dddddd	d
dddddddddddd¡ d S )Nc                   @   s   e Zd Zdd„ ZdS )ú1CAPITest.test_seq_bytes_to_charp_array.<locals>.Zc                 S   s   dS ©Nr3   r   r	   r   r   r   Ú__len__i   s    ú9CAPITest.test_seq_bytes_to_charp_array.<locals>.Z.__len__N©r   r   r   r>   r   r   r   r   ÚZh   s   rA   r3   é   ©r3   é   é   é   é   é   é	   é
   é   é   é   é   é   é   é   é   é   é   é   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r<   c                 S   s   t jS r/   )r%   Úmaxsizer	   r   r   r   r>   o   s    r?   c                 S   s   dS )Nó   xr   )r
   Úir   r   r   Ú__getitem__q   s    z=CAPITest.test_seq_bytes_to_charp_array.<locals>.Z.__getitem__N)r   r   r   r>   rY   r   r   r   r   rA   n   s   )Úobjectr   r4   Ú_posixsubprocessÚ	fork_execÚMemoryError©r
   rA   r   r   r   Útest_seq_bytes_to_charp_arraye   s    
,ÿ
,ÿz&CAPITest.test_seq_bytes_to_charp_arrayc                 C   sP   G dd„ dt ƒ}|  ttj|ƒ dgdddddd	d
dddddddddddd¡ d S )Nc                   @   s   e Zd Zdd„ ZdS )z-CAPITest.test_subprocess_fork_exec.<locals>.Zc                 S   s   dS r=   r   r	   r   r   r   r>   y   s    z5CAPITest.test_subprocess_fork_exec.<locals>.Z.__len__Nr@   r   r   r   r   rA   x   s   rA   ó   1rB   rC   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   )rZ   r   r4   r[   r\   r^   r   r   r   Útest_subprocess_fork_execv   s    
.ÿz"CAPITest.test_subprocess_fork_execz6Signature information for builtins requires docstringsc                 C   s  |   tjjd ¡ |   tjjd ¡ |   tjjd ¡ |   tjjd ¡ |   tjjd¡ |   tjjd ¡ |   tjjd¡ |   tjjd ¡ |   tjjd¡ |   tjjd ¡ |   tj	jd¡ |   tj	jd¡ |   tj
jd ¡ |   tj
jd¡ |   tjjd¡ |   tjjd¡ d S )Nz This docstring has no signature.z[docstring_with_invalid_signature($module, /, boo)

This docstring has an invalid signature.zedocstring_with_invalid_signature2($module, /, boo)

--

This docstring also has an invalid signature.z%This docstring has a valid signature.z($module, /, sig)z>
This docstring has a valid signature and some extra newlines.z($module, /, parameter))r   r   Zno_docstringr   Ú__text_signature__Zdocstring_emptyZdocstring_no_signatureZ docstring_with_invalid_signatureZ!docstring_with_invalid_signature2Zdocstring_with_signatureZ#docstring_with_signature_but_no_docZ+docstring_with_signature_and_extra_newlinesr	   r   r   r   Ú test_docstring_signature_parsing€   s<    
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ
ÿz)CAPITest.test_docstring_signature_parsingc                 C   s²   t j}|ƒ }|ƒ }|  || d||f¡ |  |d d|df¡ |  d| dd|f¡ |}|| }|  |d||f¡ |}|d }|  |d|df¡ d}|| }|  |dd|f¡ d S )NÚmatmulé*   Úimatmul)r   Z
matmulTyper   )r
   ÚMZm1Zm2Úor   r   r   Ú&test_c_type_with_matrix_multiplication«   s    z/CAPITest.test_c_type_with_matrix_multiplicationc                 C   s2   t  ¡ }|  | d¡d¡ |  | dd¡d¡ d S )Nr3   )r3   NrD   )rD   rD   )r   ZipowTyper   Ú__ipow__)r
   rh   r   r   r   Útest_c_type_with_ipow¼   s    zCAPITest.test_c_type_with_ipowc                 C   s|   t r4t d¡}td|ƒ\}}}|  | dd¡d¡ nD|  t¡}t 	¡  W d   ƒ n1 s\0    Y  |  t
|jƒd¡ d S )NzÆ
                import _testcapi
                from test import support

                with support.SuppressCrashReport():
                    _testcapi.return_null_without_error()
            r   ó   r!   s  Fatal Python error: _Py_CheckFunctionResult: a function returned NULL without setting an error\nPython runtime state: initialized\nSystemError: <built-in function return_null_without_error> returned NULL without setting an error\n\nCurrent thread.*:\n  File .*", line 6 in <module>zBreturn_null_without_error.* returned NULL without setting an error)ÚPy_DEBUGÚtextwrapÚdedentr   ÚassertRegexÚreplacer   ÚSystemErrorr   Zreturn_null_without_errorÚstrÚ	exception©r
   ÚcodeÚrcr,   r-   Úcmr   r   r   Útest_return_null_without_errorÃ   s    
ÿ&ÿz'CAPITest.test_return_null_without_errorc                 C   s|   t r4t d¡}td|ƒ\}}}|  | dd¡d¡ nD|  t¡}t 	¡  W d   ƒ n1 s\0    Y  |  t
|jƒd¡ d S )NzÅ
                import _testcapi
                from test import support

                with support.SuppressCrashReport():
                    _testcapi.return_result_with_error()
            r   rl   r!   sg  Fatal Python error: _Py_CheckFunctionResult: a function returned a result with an error set\nPython runtime state: initialized\nValueError\n\nThe above exception was the direct cause of the following exception:\n\nSystemError: <built-in function return_result_with_error> returned a result with an error set\n\nCurrent thread.*:\n  File .*, line 6 in <module>z>return_result_with_error.* returned a result with an error set)rm   rn   ro   r   rp   rq   r   rr   r   Zreturn_result_with_errorrs   rt   ru   r   r   r   Útest_return_result_with_errorá   s    
ÿ&ÿz&CAPITest.test_return_result_with_errorc                 C   s   t  ¡  d S r/   )r   Útest_buildvalue_Nr	   r   r   r   r{     s    zCAPITest.test_buildvalue_Nc                 C   s<   d}t d|ƒ\}}}|  d|¡ |  d|¡ |  d|¡ d S )Na*  if 1:
            import _testcapi

            class C(): pass

            # The first loop tests both functions and that remove_mem_hooks()
            # can be called twice in a row. The second loop checks a call to
            # set_nomemory() after a call to remove_mem_hooks(). The third
            # loop checks the start and stop arguments of set_nomemory().
            for outer_cnt in range(1, 4):
                start = 10 * outer_cnt
                for j in range(100):
                    if j == 0:
                        if outer_cnt != 3:
                            _testcapi.set_nomemory(start)
                        else:
                            _testcapi.set_nomemory(start, start + 1)
                    try:
                        C()
                    except MemoryError as e:
                        if outer_cnt != 3:
                            _testcapi.remove_mem_hooks()
                        print('MemoryError', outer_cnt, j)
                        _testcapi.remove_mem_hooks()
                        break
        r   s   MemoryError 1 10s   MemoryError 2 20s   MemoryError 3 30)r   ÚassertIn©r
   rv   rw   r,   r-   r   r   r   Útest_set_nomemory  s
    zCAPITest.test_set_nomemoryc              	   C   sª   G dd„ dt ƒ}G dd„ dt ƒ}ddddœ}i tƒ |ƒ |ƒ |t|ƒ||ƒ||ƒfD ]R}|  t |¡t| ¡ ƒ¡ |  t |¡t| ¡ ƒ¡ |  t 	|¡t| 
¡ ƒ¡ qRd S )	Nc                       s4   e Zd Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )z9CAPITest.test_mapping_keys_values_items.<locals>.Mapping1c                    s   t tƒ  ¡ ƒS r/   )ÚlistÚsuperÚkeysr	   ©r7   r   r   r   (  s    z>CAPITest.test_mapping_keys_values_items.<locals>.Mapping1.keysc                    s   t tƒ  ¡ ƒS r/   )r   r€   Úvaluesr	   r‚   r   r   rƒ   *  s    z@CAPITest.test_mapping_keys_values_items.<locals>.Mapping1.valuesc                    s   t tƒ  ¡ ƒS r/   )r   r€   Úitemsr	   r‚   r   r   r„   ,  s    z?CAPITest.test_mapping_keys_values_items.<locals>.Mapping1.items©r   r   r   r   rƒ   r„   Ú__classcell__r   r   r‚   r   ÚMapping1'  s   r‡   c                       s4   e Zd Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )z9CAPITest.test_mapping_keys_values_items.<locals>.Mapping2c                    s   t tƒ  ¡ ƒS r/   )Útupler€   r   r	   r‚   r   r   r   /  s    z>CAPITest.test_mapping_keys_values_items.<locals>.Mapping2.keysc                    s   t tƒ  ¡ ƒS r/   )rˆ   r€   rƒ   r	   r‚   r   r   rƒ   1  s    z@CAPITest.test_mapping_keys_values_items.<locals>.Mapping2.valuesc                    s   t tƒ  ¡ ƒS r/   )rˆ   r€   r„   r	   r‚   r   r   r„   3  s    z?CAPITest.test_mapping_keys_values_items.<locals>.Mapping2.itemsr…   r   r   r‚   r   ÚMapping2.  s   r‰   r3   rD   rB   )ÚfooZbarZspam)Údictr   ZassertListEqualr   Úget_mapping_keysr   r   Úget_mapping_valuesrƒ   Úget_mapping_itemsr„   )r
   r‡   r‰   Zdict_objÚmappingr   r   r   Útest_mapping_keys_values_items&  s     þ
ÿ
ÿ
ÿz'CAPITest.test_mapping_keys_values_itemsc                 C   sx   |   ttjd ¡ |   ttjd ¡ |   ttjd ¡ G dd„ dƒ}|ƒ }|   ttj|¡ |   ttj|¡ |   ttj|¡ d S )Nc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )zCCAPITest.test_mapping_keys_values_items_bad_arg.<locals>.BadMappingc                 S   s   d S r/   r   r	   r   r   r   r   G  s    zHCAPITest.test_mapping_keys_values_items_bad_arg.<locals>.BadMapping.keysc                 S   s   d S r/   r   r	   r   r   r   rƒ   I  s    zJCAPITest.test_mapping_keys_values_items_bad_arg.<locals>.BadMapping.valuesc                 S   s   d S r/   r   r	   r   r   r   r„   K  s    zICAPITest.test_mapping_keys_values_items_bad_arg.<locals>.BadMapping.itemsN)r   r   r   r   rƒ   r„   r   r   r   r   Ú
BadMappingF  s   r‘   )r   r   r   rŒ   r   rŽ   r4   )r
   r‘   Zbad_mappingr   r   r   Ú&test_mapping_keys_values_items_bad_argA  s    z/CAPITest.test_mapping_keys_values_items_bad_argZnegative_refcountz need _testcapi.negative_refcountc                 C   s*   t  d¡}td|ƒ\}}}|  |d¡ d S )Nzª
            import _testcapi
            from test import support

            with support.SuppressCrashReport():
                _testcapi.negative_refcount()
        r   s`   _testcapimodule\.c:[0-9]+: _Py_NegativeRefcount: Assertion failed: object has negative ref count)rn   ro   r   rp   r}   r   r   r   Útest_negative_refcountR  s
    
ÿzCAPITest.test_negative_refcountc                 C   s,   ddl m} d }tdƒD ]}||fƒ}qd S )Nr   ©ÚMyListéè  )r   r•   Úrange)r
   r•   ÚLrX   r   r   r   Útest_trashcan_subclassd  s    zCAPITest.test_trashcan_subclassZcpuc                 C   s   |   t¡ d S r/   )Údo_test_trashcan_python_classr   r	   r   r   r   Útest_trashcan_python_class1l  s    z$CAPITest.test_trashcan_python_class1c                 C   s   ddl m} |  |¡ d S )Nr   r”   )r   r•   rš   )r
   r•   r   r   r   Útest_trashcan_python_class2p  s    z$CAPITest.test_trashcan_python_class2c                 C   sh   G dd„ d|ƒ}dD ]N}d }t dƒD ]}||fƒ}||_q$|rD|f}|  |jd¡ ~|  |jd¡ qd S )Nc                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )z6CAPITest.do_test_trashcan_python_class.<locals>.PyListr   c                    s   ˆ  j d7  _ tƒ j|Ž  d S r=   )Únumr€   Ú__init__)r
   Úargsr‚   r   r   rž   }  s    z?CAPITest.do_test_trashcan_python_class.<locals>.PyList.__init__c                    s   ˆ  j d8  _ d S r=   )r   r	   r‚   r   r   Ú__del__€  s    z>CAPITest.do_test_trashcan_python_class.<locals>.PyList.__del__)r   r   r   r   rž   r    r†   r   r   r‚   r   ÚPyListy  s   r¡   )r   r3   i   r   )r—   ÚattrZassertGreaterr   r   )r
   Úbaser¡   Zparityr˜   rX   r   r   r   rš   u  s    

z&CAPITest.do_test_trashcan_python_classc                 C   sZ   G dd„ dt jƒ}|ƒ }t |¡}|  |jd¡ |  |jd¡ ~|  |d t |¡¡ d S )Nc                       s   e Zd Z‡ fdd„Z‡  ZS )z`CAPITest.test_subclass_of_heap_gc_ctype_with_tpdealloc_decrefs_once.<locals>.HeapGcCTypeSubclassc                    s   d| _ tƒ  ¡  d S ©NrT   ©Úvalue2r€   rž   r	   r‚   r   r   rž   “  s    ziCAPITest.test_subclass_of_heap_gc_ctype_with_tpdealloc_decrefs_once.<locals>.HeapGcCTypeSubclass.__init__©r   r   r   rž   r†   r   r   r‚   r   ÚHeapGcCTypeSubclass’  s   r¨   rJ   rT   r3   )r   ÚHeapGcCTyper%   Úgetrefcountr   Úvaluer¦   )r
   r¨   Úsubclass_instanceÚtype_refcntr   r   r   Ú:test_subclass_of_heap_gc_ctype_with_tpdealloc_decrefs_once‘  s    
zCCAPITest.test_subclass_of_heap_gc_ctype_with_tpdealloc_decrefs_oncec                    s°   G dd„ dt jƒ‰ G ‡ ‡fdd„dˆ ƒ‰ˆƒ }t ˆ¡}t ˆ ¡}|  |jd¡ |  |jd¡ ~|  |d ˆj¡ |  |d ˆ j¡ |  |d t ˆ¡¡ |  |t ˆ ¡¡ d S )Nc                       s   e Zd Z‡ fdd„Z‡  ZS )zdCAPITest.test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once.<locals>.Ac                    s   d| _ tƒ  ¡  d S r¤   r¥   r	   r‚   r   r   rž   ¤  s    zmCAPITest.test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once.<locals>.A.__init__r§   r   r   r‚   r   ÚA£  s   r¯   c                       s*   e Zd Z‡ fdd„Z‡‡fdd„Z‡  ZS )zdCAPITest.test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once.<locals>.Bc                    s   t ƒ  ¡  d S r/   )r€   rž   r	   r‚   r   r   rž   ©  s    zmCAPITest.test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once.<locals>.B.__init__c                    s"   ˆ | _ t ˆ ¡ˆ _t ˆ¡ˆ_d S r/   )r7   r%   rª   Úrefcnt_in_delr	   ©r¯   ÚBr   r   r    ¬  s    zlCAPITest.test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once.<locals>.B.__del__)r   r   r   rž   r    r†   r   r±   r‚   r   r²   ¨  s   r²   rJ   rT   r3   )r   r©   r%   rª   r   r«   r¦   r°   ©r
   r¬   r­   Znew_type_refcntr   r±   r   ÚPtest_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once¢  s    	

zYCAPITest.test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_oncec                 C   sX   t  ¡ }d|_|  |jd¡ |  |j|j¡ |  |jddi¡ t  ¡ }|  i |j¡ d S ©Nre   rŠ   )r   ZHeapCTypeWithDictrŠ   r   ÚdictobjÚ__dict__r   r   r   r   Útest_heaptype_with_dictÅ  s    z CAPITest.test_heaptype_with_dictc                 C   sX   t  ¡ }d|_|  |jd¡ |  |j|j¡ |  |jddi¡ t  ¡ }|  i |j¡ d S rµ   )r   ZHeapCTypeWithNegativeDictrŠ   r   r¶   r·   r   r   r   r   Ú test_heaptype_with_negative_dictÏ  s    z)CAPITest.test_heaptype_with_negative_dictc                 C   s2   t  ¡ }t |¡}|  |ƒ |¡ |  |j|¡ d S r/   )r   ZHeapCTypeWithWeakrefÚweakrefÚrefr   Zweakreflist)r
   r   r»   r   r   r   Útest_heaptype_with_weakrefÙ  s    
z#CAPITest.test_heaptype_with_weakrefc                 C   s    t  ¡ }t|ƒ}|  |d¡ d S )Ns   1234)r   ZHeapCTypeWithBufferÚbytesr   )r
   r   Úbr   r   r   Útest_heaptype_with_bufferß  s    z"CAPITest.test_heaptype_with_bufferc                 C   sN   t  ¡ }t t j¡}|  |jd¡ |  |jd¡ ~|  |d t t j¡¡ d S ©NrJ   rT   r3   )r   ÚHeapCTypeSubclassr%   rª   r   r«   r¦   )r
   r¬   r­   r   r   r   Ú9test_c_subclass_of_heap_ctype_with_tpdealloc_decrefs_onceä  s    zBCAPITest.test_c_subclass_of_heap_ctype_with_tpdealloc_decrefs_oncec                 C   s–   t  ¡ }t t j¡}t t j¡}|  |jd¡ |  |jd¡ ~|  |d t jj¡ |  |d t jj¡ |  |d t t j¡¡ |  |t t j¡¡ d S rÀ   )	r   ZHeapCTypeSubclassWithFinalizerr%   rª   rÁ   r   r«   r¦   r°   r³   r   r   r   ÚOtest_c_subclass_of_heap_ctype_with_del_modifying_dunder_class_only_decrefs_onceð  s    zXCAPITest.test_c_subclass_of_heap_ctype_with_del_modifying_dunder_class_only_decrefs_oncec                 C   s@   t  ¡ }|  |jd¡ d|_|  |jd¡ |`|  |jd¡ d S )NrJ   rL   r   )r   ZHeapCTypeSetattrr   Zpvaluer«   )r
   Úobjr   r   r   Útest_heaptype_with_setattro  s    z$CAPITest.test_heaptype_with_setattroc                 C   sÐ   ddl m} |  |ddƒd¡ |  |ddƒd¡ |  |ddƒd	¡ |  |dd
ƒd¡ |  |ddƒd¡ |  |ddƒd¡ |  |ddƒd¡ |  |dd
ƒd¡ |  t|dd¡ |  t|d	d¡ |  t|dd¡ d S )Nr   )Úpynumber_tobaseé{   rD   Z	0b1111011rH   Z0o173rJ   Ú123rP   Z0x7bi…ÿÿÿz
-0b1111011z-0o173z-123z-0x7bg     À^@)r   rÆ   r   r   r4   rr   )r
   rÆ   r   r   r   Útest_pynumber_tobase  s    zCAPITest.test_pynumber_tobasec                 C   s   t  ¡ }|  |d¡ d S ©Nz<NULL>)r   Zpyobject_repr_from_nullr   ©r
   Úsr   r   r   Útest_pyobject_repr_from_null  s    z%CAPITest.test_pyobject_repr_from_nullc                 C   s   t  ¡ }|  |d¡ d S rÊ   )r   Zpyobject_str_from_nullr   rË   r   r   r   Útest_pyobject_str_from_null   s    z$CAPITest.test_pyobject_str_from_nullc                 C   s   t  ¡ }|  |d¡ d S )Ns   <NULL>)r   Zpyobject_bytes_from_nullr   rË   r   r   r   Útest_pyobject_bytes_from_null$  s    z&CAPITest.test_pyobject_bytes_from_nullc                 C   s2   t j}d}||ƒ}t|ddƒ}|  |j|j¡ d S )Ns%   # -*- coding: latin1 -*-
print('Â¤')
z<string>Úexec)r   ZPy_CompileStringÚcompiler   Ú	co_consts)r
   Ú_compilerv   ÚresultÚexpectedr   r   r   Útest_Py_CompileString(  s
    zCAPITest.test_Py_CompileStringN).r   r   r   r   r.   r1   r;   ÚunittestÚ
skipUnlessr[   r_   ra   ZskipIfr   rc   ri   rk   ry   rz   r{   r~   r   r’   Úhasattrr   r“   r™   r   Zrequires_resourcer›   rœ   rš   r®   r´   r¸   r¹   r¼   r¿   rÂ   rÃ   rÅ   rÉ   rÍ   rÎ   rÏ   rÖ   r   r   r   r   r   ,   sX   



	ÿ
)" ÿ


#

r   c                   @   s6   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ ZdS )ÚTestPendingCallsc                    s>   ‡ fdd„}t |ƒD ]$}t t ¡ d ¡ t |¡r*qq*qd S )Nc                      s   ˆ   d ¡ d S r/   )Úappendr   ©Úlr   r   Úcallback4  s    z6TestPendingCalls.pendingcalls_submit.<locals>.callbackg{®Gáz”?)r—   ÚtimeÚsleepÚrandomr   Z_pending_threadfunc)r
   rÝ   ÚnrÞ   rX   r   rÜ   r   Úpendingcalls_submit3  s
    
z$TestPendingCalls.pendingcalls_submitNc                 C   s˜   d}t |ƒ|krxdr,tjr,tdt |ƒf ƒ tdƒD ]}|| }q4|rR|j ¡ sRq|d7 }|  |dk d|t |ƒf ¡ qdr”tjr”tdt |ƒf ƒ d S )Nr   Fz(%i)r–   r3   i'  z(timeout waiting for %i callbacks, got %i)Úlenr   ÚverboseÚprintr—   ÚeventÚis_setr   )r
   rÝ   râ   ÚcontextÚcountrX   Úar   r   r   Úpendingcalls_waitB  s    


ÿ
z"TestPendingCalls.pendingcalls_waitc                    s¨   d}g }G dd„ dt ƒ}|ƒ ‰ g ˆ _dˆ _|ˆ j ˆ _dˆ _t ¡ ˆ _t ¡ ˆ _	‡ ‡fdd„t
ˆ jƒD ƒ}t |¡  ˆ ˆ j|ˆ ¡ W d   ƒ n1 sš0    Y  d S )Né    c                   @   s   e Zd ZdS )z8TestPendingCalls.test_pendingcalls_threaded.<locals>.fooN©r   r   r   r   r   r   r   rŠ   Z  r!   rŠ   rD   r   c                    s   g | ]}t jˆjˆ fd ‘qS ))ÚtargetrŸ   )Ú	threadingÚThreadÚpendingcalls_thread)Ú.0rX   ©ré   r
   r   r   Ú
<listcomp>c  s   þÿz?TestPendingCalls.test_pendingcalls_threaded.<locals>.<listcomp>)rZ   rÝ   râ   ÚnThreadsÚ	nFinishedrð   ÚLockÚlockÚEventrç   r—   r   Zstart_threadsrì   )r
   râ   ÚthreadsrŠ   r   rô   r   Útest_pendingcalls_threadedU  s    

þz+TestPendingCalls.test_pendingcalls_threadedc                 C   sà   zv|   |j|j¡ W |j8 | jd7  _|j}drDtjrDtd|ƒ W d   ƒ n1 sX0    Y  ||jkrÜ|j	 
¡  nd|j8 | jd7  _|j}dr¨tjr¨td|ƒ W d   ƒ n1 s¼0    Y  ||jkrÚ|j	 
¡  0 d S )Nr3   Fzfinished threads: )rã   rÝ   râ   rù   r÷   r   rå   ræ   rö   rç   Úset)r
   ré   r÷   r   r   r   rò   i  s     
(
ú
(
z$TestPendingCalls.pendingcalls_threadc                 C   s$   g }d}|   ||¡ |  ||¡ d S )Né@   )rã   rì   )r
   rÝ   râ   r   r   r   Útest_pendingcalls_non_threadedu  s    z/TestPendingCalls.test_pendingcalls_non_threaded)N)r   r   r   rã   rì   rü   rò   rÿ   r   r   r   r   rÚ   1  s
   
rÚ   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚSubinterpreterTestc                 C   s   dd l }t ¡ \}}d |¡}t|dƒT}t |¡}|  |d¡ |  t	 
|¡ttjƒ¡ |  t	 
|¡t|ƒ¡ W d   ƒ n1 s‚0    Y  d S )Nr   zÀif 1:
            import sys, builtins, pickle
            with open({:d}, "wb") as f:
                pickle.dump(id(sys.modules), f)
                pickle.dump(id(builtins), f)
            Úrb)ÚbuiltinsÚosÚpipeÚformatÚopenr   Úrun_in_subinterpr   ZassertNotEqualÚpickleÚloadr   r%   Úmodules)r
   r  ÚrÚwrv   ÚfÚretr   r   r   Útest_subinterps‚  s    û
z"SubinterpreterTest.test_subinterpsc                 C   sr   t  ¡ \}}d |¡}t|dƒ>}t |¡}|  |d¡ |  t |¡dddœ¡ W d   ƒ n1 sd0    Y  d S )Naa  if 1:
            import pickle
            with open({:d}, "wb") as f:

                @(lambda x:x)  # Py 3.9
                def noop(x): return x

                a = (b := f'1{{2}}3') + noop('x')  # Py 3.8 (:=) / 3.6 (f'')

                async def foo(arg): return await arg  # Py 3.5

                pickle.dump(dict(a=a, b=b), f)
            r  r   Z123xrÈ   )rë   r¾   )	r  r  r  r  r   r  r   r  r	  )r
   r  r  rv   r  r  r   r   r   Ú(test_subinterps_recent_language_features‘  s    ô
z;SubinterpreterTest.test_subinterps_recent_language_featuresc                 C   s(   ddl }t d¡ |  t|jdƒ¡ dS )zê
        Exceptions saved in global module state get shared between
        individual module instances. This test checks whether or not
        a change in one interpreter's module gets reflected into the
        other ones.
        r   Nz1import binascii; binascii.Error.foobar = 'foobar'Zfoobar)Úbinasciir   r  ZassertFalserÙ   ÚError)r
   r  r   r   r   Útest_mutate_exception¦  s    
z(SubinterpreterTest.test_mutate_exceptionc                 C   s€   t  ¡ \}}|  t j|¡ |  t j|¡ t d|› d¡}t|ƒ t  |d¡}t 	|¡}|  
|d¡ t  |d¡}|  
||¡ dS )z‹
        bpo-44050: Extension module state should be shared between interpreters
        when it doesn't support sub-interpreters.
        aö  
            import importlib.machinery
            import importlib.util
            import os

            fullname = '_test_module_state_shared'
            origin = importlib.util.find_spec('_testmultiphase').origin
            loader = importlib.machinery.ExtensionFileLoader(fullname, origin)
            spec = importlib.util.spec_from_loader(fullname, loader)
            module = importlib.util.module_from_spec(spec)
            attr_id = str(id(module.Error)).encode()

            os.write(z, attr_id)
            éd   r   N)r  r  Z
addCleanupÚclosern   ro   rÐ   Úreadr   r  r   )r
   r  r  ZscriptZmain_attr_idr  Zsubinterp_attr_idr   r   r   Ú"test_module_state_shared_in_global³  s    ô

z5SubinterpreterTest.test_module_state_shared_in_globalN)r   r   r   r  r  r  r  r   r   r   r   r   €  s   r   c                   @   s   e Zd Zejdd„ ƒZdS )ÚTestThreadStatec                    s2   ‡ fdd„}|ƒ  t j|d}| ¡  | ¡  d S )Nc                     sH   g ‰ ‡ fdd„} t  | ¡ |  }}t d¡ ˆ ˆ  t ¡ ¡dd¡ d S )Nc                      s   ˆ   t ¡ ¡ d S r/   )rÛ   rð   Ú	get_identr   ©Zidentsr   r   rÞ   Û  s    zCTestThreadState.test_thread_state.<locals>.target.<locals>.callbackr3   rB   z/Couldn't find main thread correctly in the list)r   Z_test_thread_staterß   rà   r   rê   rð   r  )rÞ   rë   r¾   r	   r  r   rï   Ø  s    

ÿz1TestThreadState.test_thread_state.<locals>.target)rï   )rð   rñ   ÚstartÚjoin)r
   rï   Útr   r	   r   Útest_thread_stateÕ  s
    z!TestThreadState.test_thread_stateN)r   r   r   r   Zreap_threadsr  r   r   r   r   r  Ó  s   r  c                   @   s&   e Zd Zeƒ  dd„ eeƒD ƒ¡ dS )ÚTest_testcapic                 c   s0   | ](}|  d ¡r| d¡s|tt|ƒfV  qdS )Útest_Ú_codeN)r*   ÚendswithÚgetattrr   ©ró   Únamer   r   r   Ú	<genexpr>ì  s   þzTest_testcapi.<genexpr>N)r   r   r   ÚlocalsÚupdateÚdirr   r   r   r   r   r  ë  s   ÿr  c                   @   s&   e Zd Zeƒ  dd„ eeƒD ƒ¡ dS )ÚTest_testinternalcapic                 c   s&   | ]}|  d ¡r|tt|ƒfV  qdS )r   N)r*   r#  Ú_testinternalcapir$  r   r   r   r&  ò  s   
þzTest_testinternalcapi.<genexpr>N)r   r   r   r'  r(  r)  r+  r   r   r   r   r*  ñ  s   ÿr*  c                   @   sl   e Zd Zd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d„ Zdd„ Zdd„ Zdd„ ZdS )ÚPyMemDebugTestsÚdebugz(?:0x)?[0-9a-fA-F]+c                 C   sL   t  ¡ " td|| jdd}W d   ƒ n1 s00    Y  |j}| dd¡S )Nr   ú
junk:false©ÚPYTHONMALLOCZMALLOC_CONFÚasciirq   )r   r"   r   r0  r-   Údecode)r
   rv   r,   r    r   r   r   Úcheckü  s    
û$zPyMemDebugTests.checkc                 C   s<   |   d¡}d}|j| jd}tj|tjd}|  ||¡ d S )Nz3import _testcapi; _testcapi.pymem_buffer_overflow()a6  Debug memory block at address p={ptr}: API 'm'\n    16 bytes originally requested\n    The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n    The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n        at tail\+0: 0x78 \*\*\* OUCH\n        at tail\+1: 0xfd\n        at tail\+2: 0xfd\n        .*\n(    The block was made by call #[0-9]+ to debug malloc/realloc.\n)?    Data at p: cd cd cd .*\n\nEnable tracemalloc to get the memory block allocation traceback\n\nFatal Python error: _PyMem_DebugRawFree: bad trailing pad byte©Zptr)Úflags)r3  r  Ú	PTR_REGEXÚrerÑ   ÚDOTALLrp   ©r
   r,   Zregexr   r   r   Útest_buffer_overflow  s
    
z$PyMemDebugTests.test_buffer_overflowc                 C   s,   |   d¡}d}|j| jd}|  ||¡ d S )Nz.import _testcapi; _testcapi.pymem_api_misuse()aå  Debug memory block at address p={ptr}: API 'm'\n    16 bytes originally requested\n    The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n    The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n(    The block was made by call #[0-9]+ to debug malloc/realloc.\n)?    Data at p: cd cd cd .*\n\nEnable tracemalloc to get the memory block allocation traceback\n\nFatal Python error: _PyMem_DebugRawFree: bad ID: Allocated using API 'm', verified using API 'r'\nr4  )r3  r  r6  rp   r9  r   r   r   Útest_api_misuse  s    

zPyMemDebugTests.test_api_misusec                 C   s   |   |¡}d}|  ||¡ d S )Nz^Fatal Python error: _PyMem_DebugMalloc: Python memory allocator called without holding the GIL)r3  r|   )r
   rv   r,   rÕ   r   r   r   Úcheck_malloc_without_gil+  s    
z(PyMemDebugTests.check_malloc_without_gilc                 C   s   d}|   |¡ d S )Nz6import _testcapi; _testcapi.pymem_malloc_without_gil()©r<  ©r
   rv   r   r   r   Útest_pymem_malloc_without_gil1  s    z-PyMemDebugTests.test_pymem_malloc_without_gilc                 C   s   d}|   |¡ d S )Nz9import _testcapi; _testcapi.pyobject_malloc_without_gil()r=  r>  r   r   r   Ú test_pyobject_malloc_without_gil7  s    z0PyMemDebugTests.test_pyobject_malloc_without_gilc                 C   s(   t  d|› d¡}td|| jdd d S )Nz¬
            import gc, os, sys, _testcapi
            # Disable the GC to avoid crash on GC collection
            gc.disable()
            try:
                _testcapi.zÓ()
                # Exit immediately to avoid a crash while deallocating
                # the invalid object
                os._exit(0)
            except _testcapi.error:
                os._exit(1)
        r   r.  r/  )rn   ro   r   r0  )r
   Ú	func_namerv   r   r   r   Úcheck_pyobject_is_freed=  s    û
ýz'PyMemDebugTests.check_pyobject_is_freedc                 C   s   |   d¡ d S )NZcheck_pyobject_null_is_freed©rB  r	   r   r   r   Útest_pyobject_null_is_freedP  s    z+PyMemDebugTests.test_pyobject_null_is_freedc                 C   s   |   d¡ d S )NZ%check_pyobject_uninitialized_is_freedrC  r	   r   r   r   Ú$test_pyobject_uninitialized_is_freedS  s    z4PyMemDebugTests.test_pyobject_uninitialized_is_freedc                 C   s   |   d¡ d S )NZ'check_pyobject_forbidden_bytes_is_freedrC  r	   r   r   r   Ú&test_pyobject_forbidden_bytes_is_freedV  s    z6PyMemDebugTests.test_pyobject_forbidden_bytes_is_freedc                 C   s   |   d¡ d S )NZcheck_pyobject_freed_is_freedrC  r	   r   r   r   Útest_pyobject_freed_is_freedY  s    z,PyMemDebugTests.test_pyobject_freed_is_freedN)r   r   r   r0  r6  r3  r:  r;  r<  r?  r@  rB  rD  rE  rF  rG  r   r   r   r   r,  ÷  s   r,  c                   @   s   e Zd ZdZdS )ÚPyMemMallocDebugTestsZmalloc_debugN©r   r   r   r0  r   r   r   r   rH  ]  s   rH  zneed pymallocc                   @   s   e Zd ZdZdS )ÚPyMemPymallocDebugTestsZpymalloc_debugNrI  r   r   r   r   rJ  a  s   rJ  zneed Py_DEBUGc                   @   s   e Zd ZdZdS )ÚPyMemDefaultTestsÚ NrI  r   r   r   r   rK  f  s   rK  c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚTest_ModuleStateAccessz%Test access to module start (PEP 573)c                 C   sN   d}t j d¡j}t j ||¡}t j ||¡}t j |¡}| |¡ || _	d S )NZ!_testmultiphase_meth_state_accessZ_testmultiphase)
Ú	importlibÚutilÚ	find_specÚoriginÚ	machineryÚExtensionFileLoaderÚspec_from_loaderÚmodule_from_specÚexec_moduleÚmodule)r
   ÚfullnamerQ  ÚloaderÚspecrW  r   r   r   ÚsetUps  s    
zTest_ModuleStateAccess.setUpc                 C   s0   G dd„ d| j jƒ}|ƒ }|  | ¡ | j ¡ dS )z#PyType_GetModule for defining_classc                   @   s   e Zd ZdS )zQTest_ModuleStateAccess.test_subclass_get_module.<locals>.StateAccessType_SubclassNrî   r   r   r   r   ÚStateAccessType_Subclass~  s   r\  N©rW  ÚStateAccessTypeZassertIsÚget_defining_module©r
   r\  Úinstancer   r   r   Útest_subclass_get_module|  s    z/Test_ModuleStateAccess.test_subclass_get_modulec                 C   s0   G dd„ d| j jƒ}|ƒ }|  | ¡ | j ¡ d S )Nc                       s   e Zd Z‡ fdd„Z‡  ZS )z\Test_ModuleStateAccess.test_subclass_get_module_with_super.<locals>.StateAccessType_Subclassc                    s
   t ƒ  ¡ S r/   )r€   r_  r	   r‚   r   r   r_  †  s    zpTest_ModuleStateAccess.test_subclass_get_module_with_super.<locals>.StateAccessType_Subclass.get_defining_module)r   r   r   r_  r†   r   r   r‚   r   r\  …  s   r\  r]  r`  r   r   r   Ú#test_subclass_get_module_with_super„  s    z:Test_ModuleStateAccess.test_subclass_get_module_with_superc              
   C   s€  | j  ¡ }| j  ¡ }|j|jdœ}| ¡ D ]N\}}|  |¡( |  | ¡ | ¡ ¡ |  | ¡ d¡ |ƒ  |  | ¡ | ¡ ¡ |  | ¡ d¡ |dƒ |  | ¡ | ¡ ¡ |  | ¡ d¡ |ddd |  | ¡ | ¡ ¡ |  | ¡ d¡ |  t	¡ |dd	 W d
  ƒ n1 s0    Y  |  t	¡ |dddƒ W d
  ƒ n1 sP0    Y  W d
  ƒ q*1 sp0    Y  q*d
S )z²Checks methods defined with and without argument clinic

        This tests a no-arg method (get_count) and a method with
        both a positional and keyword argument.
        )ZclinicZnoclinicr   r3   rB   é   éþÿÿÿT)Ztwice)ZthriceNrD   )
rW  r^  Zincrement_count_clinicZincrement_count_noclinicr„   ZsubTestr   Z	get_countr   r4   )r
   rë   r¾   Úmethodsr%  Zincrement_countr   r   r   Útest_state_accessŒ  s,    

þ*z(Test_ModuleStateAccess.test_state_accessN)r   r   r   r   r[  rb  rc  rg  r   r   r   r   rM  l  s
   	rM  Ú__main__)0Úcollectionsr   r  r  rá   r7  r#   r%   rn   rð   rß   r×   rº   Zimportlib.machineryrN  Úimportlib.utilr   r   Ztest.supportr   Ztest.support.script_helperr   r   r[   ÚImportErrorÚimport_moduler   r+  rÙ   rm   r   r   ZTestCaser   rÚ   r   r  r  r*  r,  rH  rØ   Zwith_pymallocrJ  rK  rM  r   Úmainr   r   r   r   Ú<module>   sX   


    	OSf
G
