3
rY]I^                 @   s  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 yd dlZW n ek
r   dZY nX yd dlZW n ek
r   dZY nX ejdZeedZdd ZG d	d
 d
ZG dd de	jZe	jedG dd de	jZG dd de	jZG dd de	jZe	jed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	jej$ddkdG dd  d e"Z%e	jed!G d"d# d#e"Z&e'd$kre	j(  dS )%    N)support)MISSING_C_DOCSTRINGS)assert_python_failureassert_python_ok	_testcapigettotalrefcountc             C   s   | S )zsome doc )selfr   r   lib/python3.6/test/test_capi.pytestfunction    s    r   c               @   s    e Zd ZejeZejeZdS )InstanceMethodN)__name__
__module____qualname__r   Zinstancemethodidr   r   r   r   r
   r   $   s   
r   c               @   s   e Zd Zdd Zejeddd Zdd Zdd	 Z	eje
d
dd Zeje
d
dd Zejeddd Zdd Zdd Zdd Zdd Zdd ZdS )CAPITestc             C   s~   t  }| jt||j  | j|j |k | j|jjtj | jt jjtj dt j_| jtjd | jtt	|jdd d S )Ntest	attribute)
r   assertEqualr   
assertTruer   __doc__r   assertRaisesAttributeErrorsetattr)r	   Zinstr   r   r
   test_instancemethod*   s    zCAPITest.test_instancemethodz!Threading required for this test.c             C   s`   t j " tjtjddgtjtjd}W d Q R X |j \}}| j|d | j	|j
 jd d S )Nz-cz4import _testcapi;_testcapi.crash_no_current_thread())stdoutstderr    s8   Fatal Python error: PyThreadState_Get: no current thread)r   SuppressCrashReport
subprocessPopensys
executablePIPEcommunicater   r   rstrip
startswith)r	   pouterrr   r   r
    test_no_FatalError_infinite_loop5   s    

z)CAPITest.test_no_FatalError_infinite_loopc             C   s   | j ttj d S )N)r   
ValueErrorr   Z!make_memoryview_from_NULL_pointer)r	   r   r   r
   !test_memoryview_from_NULL_pointerD   s    z*CAPITest.test_memoryview_from_NULL_pointerc       
      C   s   t d}td}y|W n t k
r } z|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	|| W Y d d }~X nX | j
d d S )N5ZTEST   F)r+   	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_infoG   s$    
zCAPITest.test_exc_infoz(_posixsubprocess required for this test.c             C   s   G dd dt }| jttjd| dddddd	d
dddddddd G dd dt }| jttj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 )z1CAPITest.test_seq_bytes_to_charp_array.<locals>.Zc             S   s   dS )Nr.   r   )r	   r   r   r
   __len__b   s    z9CAPITest.test_seq_bytes_to_charp_array.<locals>.Z.__len__N)r   r   r   r7   r   r   r   r
   Za   s   r8   r.                     	   
                        c               @   s   e Zd Zdd Zdd ZdS )z1CAPITest.test_seq_bytes_to_charp_array.<locals>.Zc             S   s   t jS )N)r!   maxsize)r	   r   r   r
   r7   h   s    z9CAPITest.test_seq_bytes_to_charp_array.<locals>.Z.__len__c             S   s   dS )N   xr   )r	   ir   r   r
   __getitem__j   s    z=CAPITest.test_seq_bytes_to_charp_array.<locals>.Z.__getitem__N)r   r   r   r7   rK   r   r   r   r
   r8   g   s   )r.   r:   )r.   r:   )objectr   r/   _posixsubprocess	fork_execMemoryError)r	   r8   r   r   r
   test_seq_bytes_to_charp_array^   s    
(
z&CAPITest.test_seq_bytes_to_charp_arrayc             C   sH   G dd dt }| jttj| dg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 )Nr.   r   )r	   r   r   r
   r7   r   s    z5CAPITest.test_subprocess_fork_exec.<locals>.Z.__len__N)r   r   r   r7   r   r   r   r
   r8   q   s   r8      1r9   r.   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   )r.   r:   )rL   r   r/   rM   rN   )r	   r8   r   r   r
   test_subprocess_fork_execo   s    
z"CAPITest.test_subprocess_fork_execz6Signature information for builtins requires docstringsc             C   s  | j tjjd  | j tjjd  | j tjjd  | j tjjd  | j tjjd | j tjjd  | j tjjd | j tjjd  | j tjjd | j tjjd  | j tj	jd | j tj	jd | j tj
jd  | j tj
jd | j tjjd | j 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_newlines)r	   r   r   r
    test_docstring_signature_parsingy   s.    






z)CAPITest.test_docstring_signature_parsingc             C   s   t j}| }| }| j|| d||f | j|d d|df | jd| dd|f |}|| }| j|d||f |}|d }| j|d|df d}|| }| j|dd|f d S )Nmatmul*   imatmul)r   Z
matmulTyper   )r	   Mm1m2or   r   r
   &test_c_type_with_matrix_multiplication   s    z/CAPITest.test_c_type_with_matrix_multiplicationc          
   C   sh   t r4tjd}td|\}}}| j|jddd n0| jt}tj	  W d Q R X | jt
|jd d S )Nz
                import _testcapi
                from test import support

                with support.SuppressCrashReport():
                    _testcapi.return_null_without_error()
            z-c   r   s   Fatal Python error: a function returned NULL without setting an error\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   sh   t r4tjd}td|\}}}| j|jddd n0| jt}tj	  W d Q R X | jt
|jd d S )Nz
                import _testcapi
                from test import support

                with support.SuppressCrashReport():
                    _testcapi.return_result_with_error()
            z-cr]   r   s+  Fatal Python error: a function returned a result with an error set\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)r^   r_   r`   r   ra   rb   r   rc   r   Zreturn_result_with_errorrd   re   )r	   rf   rg   r(   r)   rh   r   r   r
   test_return_result_with_error   s    z&CAPITest.test_return_result_with_errorc             C   s   t j  d S )N)r   test_buildvalue_N)r	   r   r   r
   rk      s    zCAPITest.test_buildvalue_Nc             C   s<   d}t d|\}}}| jd| | jd| | j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
        z-cs   MemoryError 1 10s   MemoryError 2 20s   MemoryError 3 30)r   assertIn)r	   rf   rg   r(   r)   r   r   r
   test_set_nomemory   s
    zCAPITest.test_set_nomemoryN)r   r   r   r   unittest
skipUnless	threadingr*   r,   r6   rM   rP   rR   ZskipIfr   rT   r\   ri   rj   rk   rm   r   r   r   r
   r   (   s   
* r   z!Threading required for this test.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                sF    fdd}x4t |D ](}tjtj d  xtj|r.P q.W qW d S )Nc                  s    j d  d S )N)appendr   )lr   r
   callback  s    z6TestPendingCalls.pendingcalls_submit.<locals>.callbackg{Gz?)rangetimesleeprandomr   Z_pending_threadfunc)r	   rs   nrt   rJ   r   )rs   r
   pendingcalls_submit  s    
z$TestPendingCalls.pendingcalls_submitNc             C   s   d}x|t ||krdr.tjr.tdt |f  xtdD ]}|| }q8W |rZ|jj  rZq|d7 }| j|dk d|t |f  qW drtjrtdt |f  d S )Nr   Fz(%i)i  r.   i'  z(timeout waiting for %i callbacks, got %i)lenr   verboseprintru   eventZis_setr   )r	   rs   ry   contextcountrJ   ar   r   r
   pendingcalls_wait'  s    


z"TestPendingCalls.pendingcalls_waitc                s   d}g }G dd dt }|  g  _d _| j  _d _tj  _tj  _	 fddt
 jD }tj| j j|  W d Q R X d S )N    c               @   s   e Zd ZdS )z8TestPendingCalls.test_pendingcalls_threaded.<locals>.fooN)r   r   r   r   r   r   r
   foo?  s    r   r:   r   c                s   g | ]}t jj fd qS ))targetargs)rp   Threadpendingcalls_thread).0rJ   )r   r	   r   r
   
<listcomp>H  s   z?TestPendingCalls.test_pendingcalls_threaded.<locals>.<listcomp>)rL   rs   ry   nThreads	nFinishedrp   ZLocklockZEventr~   ru   r   Zstart_threadsr   )r	   ry   Zthreadsr   r   )r   r	   r
   test_pendingcalls_threaded:  s    

z+TestPendingCalls.test_pendingcalls_threadedc             C   sj   z| j |j|j W d |j. | jd7  _|j}drFtjrFtd| W d Q R X ||jkrd|j	j
  X d S )Nr.   Fzfinished threads: )rz   rs   ry   r   r   r   r|   r}   r   r~   set)r	   r   r   r   r   r
   r   N  s    

z$TestPendingCalls.pendingcalls_threadc             C   s$   g }d}| j || | j|| d S )N@   )rz   r   )r	   rs   ry   r   r   r
   test_pendingcalls_non_threadedZ  s    z/TestPendingCalls.test_pendingcalls_non_threaded)N)r   r   r   rz   r   r   r   r   r   r   r   r
   rq     s
   
rq   c               @   s   e Zd Zdd ZdS )SubinterpreterTestc             C   s|   dd l }tj \}}dj|}t|dJ}tj|}| j|d | jt	j
|ttj | jt	j
|t| W d Q R X d S )Nr   zif 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   Zrun_in_subinterpr   ZassertNotEqualpickleloadr   r!   modules)r	   r   rwrf   fZretr   r   r
   test_subinterpsg  s    

z"SubinterpreterTest.test_subinterpsN)r   r   r   r   r   r   r   r
   r   e  s   r   c               @   sB   e Zd Zdd Zdd ZddddZd	d
 Zdd Zdd ZdS )EmbeddingTestsc             C   s   t jjt}t jjt jjt jj|}d}tjjdrfdtjkrFdndd }||7 }t jjtj}nt jj	|d}t jj	|| | _
}t jj|s| jd|  t j | _t j| d S )NZ
_testembedwinZ_d z.exeZProgramsz%r doesn't exist)r   pathabspath__file__dirnamer!   platformr&   r"   jointest_exeexistsZskipTestgetcwdoldcwdchdir)r	   hereZbasepathZexenameZextZexepathZexer   r   r
   setUpx  s    
zEmbeddingTests.setUpc             C   s   t j| j d S )N)r   r   r   )r	   r   r   r
   tearDown  s    zEmbeddingTests.tearDownN)envc            G   s   | j g}|j| |dk	r:tjdkr:|j }tjd |d< tj|tj	tj	d|d}|j
 \}}| j|jdd|j|f  ||fS )z'Runs a test in the embedded interpreterNwin32Z
SYSTEMROOTT)r   r   Zuniversal_newlinesr   r   zbad returncode %d, stderr is %r)r   extendr!   r   copyr   environr   r    r#   r$   r   
returncode)r	   r   r   cmdr'   r(   r)   r   r   r
   run_embedded_interpreter  s    

z'EmbeddingTests.run_embedded_interpreterc             C   s.   | j d\}}tjr*t  t| t| d S )NZ!repeated_init_and_subinterpreters)r   r   r|   r}   )r	   r(   r)   r   r   r
   &test_repeated_init_and_subinterpreters  s
    z5EmbeddingTests.test_repeated_init_and_subinterpretersc             C   s   t tjdd}| jd|d\}}tjr<t  t| t| d}d}djdd	d
ddddd	ddddddd
dddddddddg}|j|||d}d | _	| j
|j | d S )Nzutf-8:surrogateescape)ZPYTHONIOENCODINGZforced_io_encoding)r   zutf-8surrogateescape
z--- Use defaults ---zExpected encoding: defaultzExpected errors: defaultzstdin: {in_encoding}:{errors}zstdout: {out_encoding}:{errors}z'stderr: {out_encoding}:backslashreplacez--- Set errors only ---zExpected errors: ignorezstdin: {in_encoding}:ignorezstdout: {out_encoding}:ignorez--- Set encoding only ---zExpected encoding: latin-1zstdin: latin-1:{errors}zstdout: latin-1:{errors}z stderr: latin-1:backslashreplacez--- Set encoding and errors ---zExpected errors: replacezstdin: latin-1:replacezstdout: latin-1:replace)Zin_encodingZout_encodingerrors)dictr   r   r   r   r|   r}   r   r   ZmaxDiffr   strip)r	   r   r(   r)   Zexpected_stream_encodingZexpected_errorsZexpected_outputr   r   r
   test_forced_io_encoding  sN    z&EmbeddingTests.test_forced_io_encodingc             C   sF   t tjtjjtjd}| jd|d\}}| j|d | j|d dS )z~
        Checks the few parts of the C-API that work before the runtine
        is initialized (via Py_Initialize()).
        )
PYTHONPATHZpre_initialization_api)r   r   N)	r   r   r   pathsepr   r!   r   r   r   )r	   r   r(   r)   r   r   r
   test_pre_initialization_api  s    z*EmbeddingTests.test_pre_initialization_api)	r   r   r   r   r   r   r   r   r   r   r   r   r
   r   w  s   +r   c               @   s   e Zd Zejdd ZdS )TestThreadStatec                s2    fdd}|  t j|d}|j  |j  d S )Nc                 sH   g   fdd} t j|  |  }}tjd j jtj dd d S )Nc                  s    j tj  d S )N)rr   rp   	get_identr   )identsr   r
   rt     s    zCTestThreadState.test_thread_state.<locals>.target.<locals>.callbackr.   r9   z/Couldn't find main thread correctly in the list)r   Z_test_thread_staterv   rw   r   r   rp   r   )rt   r   b)r	   )r   r
   r     s    

z1TestThreadState.test_thread_state.<locals>.target)r   )rp   r   startr   )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 jdd eeD  dS )Test_testcapic             c   s2   | ]*}|j d r|jd r|tt|fV  qdS )Z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               @   sP   e Zd ZdZdZdd Zdd Zdd Zej	e
d	d
d Zdd Zdd ZdS )PyMemDebugTestsdebugz(?:0x)?[0-9a-fA-F]+c             C   s6   t j  td|| jd}W d Q R X |j}|jddS )Nz-c)PYTHONMALLOCasciirb   )r   r   r   r   r)   decode)r	   rf   r(   r   r   r   r
   check  s
    
zPyMemDebugTests.checkc             C   s<   | j d}d}|j| jd}tj|tjd}| j|| d S )Nz3import _testcapi; _testcapi.pymem_buffer_overflow()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 not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n        at tail\+0: 0x78 \*\*\* OUCH\n        at tail\+1: 0xfb\n        at tail\+2: 0xfb\n        .*\n    The block was made by call #[0-9]+ to debug malloc/realloc.\n    Data at p: cb cb cb .*\n\nEnable tracemalloc to get the memory block allocation traceback\n\nFatal Python error: bad trailing pad byte)ptr)flags)r   r   	PTR_REGEXrecompileDOTALLra   )r	   r(   regexr   r   r
   test_buffer_overflow  s
    
z$PyMemDebugTests.test_buffer_overflowc             C   s,   | j d}d}|j| jd}| j|| 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: cb cb cb .*\n\nEnable tracemalloc to get the memory block allocation traceback\n\nFatal Python error: bad ID: Allocated using API 'm', verified using API 'r'\n)r   )r   r   r   ra   )r	   r(   r   r   r   r
   test_api_misuse   s    

zPyMemDebugTests.test_api_misusez$Test requires a GIL (multithreading)c             C   s   | j |}d}| j|| d S )NzJFatal Python error: Python memory allocator called without holding the GIL)r   rl   )r	   rf   r(   Zexpectedr   r   r
   check_malloc_without_gil/  s    
z(PyMemDebugTests.check_malloc_without_gilc             C   s   d}| j | d S )Nz6import _testcapi; _testcapi.pymem_malloc_without_gil())r   )r	   rf   r   r   r
   test_pymem_malloc_without_gil6  s    z-PyMemDebugTests.test_pymem_malloc_without_gilc             C   s   d}| j | d S )Nz9import _testcapi; _testcapi.pyobject_malloc_without_gil())r   )r	   rf   r   r   r
    test_pyobject_malloc_without_gil<  s    z0PyMemDebugTests.test_pyobject_malloc_without_gilN)r   r   r   r   r   r   r   r   rn   ro   rp   r   r   r   r   r   r   r
   r      s   r   c               @   s   e Zd ZdZdS )PyMemMallocDebugTestsZmalloc_debugN)r   r   r   r   r   r   r   r
   r   C  s   r   WITH_PYMALLOCr.   zneed pymallocc               @   s   e Zd ZdZdS )PyMemPymallocDebugTestsZpymalloc_debugN)r   r   r   r   r   r   r   r
   r   G  s   r   zneed Py_DEBUGc               @   s   e Zd ZdZdS )PyMemDefaultTestsr   N)r   r   r   r   r   r   r   r
   r   M  s   r   __main__))r   r   rx   r   r   r!   	sysconfigr_   rv   rn   r   r   Ztest.supportr   Ztest.support.script_helperr   r   rM   ImportErrorrp   import_moduler   hasattrr^   r   r   ZTestCaser   ro   rq   r   r   r   r   r   r   get_config_varr   r   r   mainr   r   r   r
   <module>   sT   



 n
Oj
C
