U
    e+                     @   s   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	 e j
dkrhd dlZdd Znd dlZd	d Ze j
d
krejZnG dd dejZG dd deZdZdd Zdadd Zdd ZdddZdd Zdd ZdS )    N   )__version_verifier_modules__)ffiplatform)VerificationError)   r   c                   C   s   t jjd d  S N)	importlib	machineryEXTENSION_SUFFIXES r   r   ,lib/python3.8/site-packages/cffi/verifier.py_extension_suffixes   s    r   c                   C   s   dd t  D S )Nc                 S   s    g | ]\}}}|t jkr|qS r   )impC_EXTENSION).0suffix_typer   r   r   
<listcomp>   s    
z'_extension_suffixes.<locals>.<listcomp>)r   Zget_suffixesr   r   r   r   r      s    r   c                       s   e Zd Z fddZ  ZS )NativeIOc                    s(   t |tr|d}tt| | d S )Nascii)
isinstanceZunicodeencodesuperr   write)selfs	__class__r   r   r      s    

zNativeIO.write)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s   r   c                   @   sz   e Zd ZdddZ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d!ddZdd Zdd ZdS )"VerifierN F.cc                 K   sz  |j jrtd|j jf || _|| _|s4t|}t||}|| | _| j	| |	| _
| ||
| _|rz|rxtdnddtjd d  t||g|j }tjdkr|d}tt|dd d d	@ }|d
d}tt|dd d d	@ }|dd}d|| jj||f }t d }|p4t | _tj| j|| | _tj| j|| | _ || _!d| _"d| _#d S )NzKfeature not supported with ffi.verify(), but only with ffi.set_source(): %sz)can't specify both 'modulename' and 'tag' z%d.%d   r   zutf-8r   l    Z0xLr   0z_cffi_%s_%s%s%sF)$Z_parserZ_uses_new_featurer   ffipreambler   Zflatten_locate_engine_class_vengineZpatch_extension_kwdsflagsmake_relative_tokwds	TypeErrorjoinsysversion_infor   Z_cdefsourcesr   hexbinasciiZcrc32lstriprstripZ
_class_key_get_so_suffixes_caller_dir_pycachetmpdirospathsourcefilenamemodulefilenameext_package_has_source_has_module)r   r+   r,   r<   Z
modulenamerA   tagforce_generic_engineZsource_extensionr/   relative_tor1   Zflattened_kwdsZvengine_classkeyZk1Zk2r   r   r   r   __init__    sT    



 


 
zVerifier.__init__c              	   C   s8   | j j& | jr |dkr td| | W 5 Q R X dS )zkWrite the C source code.  It is produced in 'self.sourcefilename',
        which can be tweaked beforehand.Nzsource code already written)r+   _lockrB   r   _write_sourcer   filer   r   r   write_sourceI   s    
zVerifier.write_sourcec              	   C   s<   | j j* | jrtd| js&|   |   W 5 Q R X dS )zWrite the C source code (if not done already) and compile it.
        This produces a dynamic link library in 'self.modulefilename'.zmodule already compiledN)r+   rI   rC   r   rB   rJ   _compile_moduler   r   r   r   compile_moduleR   s    
zVerifier.compile_modulec              
   C   sR   | j j@ | js4|   | js4| js,|   |   |  W  5 Q R  S Q R X dS )a"  Get a C module from this Verifier instance.
        Returns an instance of a FFILibrary class that behaves like the
        objects returned by ffi.dlopen(), but that delegates all
        operations to the C module.  If necessary, the C code is written
        and compiled first.
        N)r+   rI   rC   _locate_modulerB   rJ   rN   _load_libraryrO   r   r   r   load_library\   s    
zVerifier.load_libraryc                 C   sB   t j| j}|ddd }|dr>ttdr>|d d }|S )N.r   r   Z_dZgettotalrefcount)r=   r>   basenamer@   splitendswithhasattrr4   )r   rV   r   r   r   get_module_namel   s
    zVerifier.get_module_namec              	   C   sN   | j s(| jj | j s|   W 5 Q R X t| j}|  }tj||f| j	S r   )
rB   r+   rI   rJ   r   Zmaybe_relative_pathr?   rZ   get_extensionr1   )r   Z
sourcenamemodnamer   r   r   r[   w   s    
zVerifier.get_extensionc                 C   s   | j jS r   )r.   Z_gen_python_modulerO   r   r   r   generates_python_module   s    z Verifier.generates_python_modulec                    sz   |rvt j|rvt j| | }tjD ]J}||kr*|| }t|ttfsZt	d|f  fdd|D }|||< q*|S )Nz&keyword '%s' should be a list or tuplec                    s   g | ]}t j |qS r   )r=   r>   r3   )r   fndirnamer   r   r      s     z-Verifier.make_relative_to.<locals>.<listcomp>)
r=   r>   r`   copyr   ZLIST_OF_FILE_NAMESr   listtupler2   )r   r1   rF   rG   Zlstr   r_   r   r0      s    

zVerifier.make_relative_toc                 C   s   t j| jsv| jrJzt| jd d dg}W n tk
r@   Y d S X |j}nd }| j	| 
 |t }|d krpd S || _| j  d| _d S )N__doc__T)r=   r>   isfiler@   rA   
__import__ImportError__path__r.   find_modulerZ   r:   Zcollect_typesrC   )r   pkgr>   filenamer   r   r   rQ      s     
zVerifier._locate_modulec                 C   s$   || j _z| j   W 5 | j `X d S r   )r.   Z_fZwrite_source_to_frK   r   r   r   _write_source_to   s    zVerifier._write_source_toc              	   C   s   |d k	r|  | nt }|  | | }tj| jrbt| jd}| |k }W 5 Q R X nd}|rt	| j t| jd}|
| W 5 Q R X d| _d S )NrTw)rl   r   getvaluer=   r>   existsr?   openread_ensure_dirr   rB   )r   rL   fZsource_datafpZneeds_writtenr   r   r   rJ      s    

zVerifier._write_sourcec                 C   sp   t j| j}t||  }zt|| j}W n t	k
rH   d}Y nX |sft
| j t|| j d| _d S )NFT)r=   r>   r`   r?   r   compiler[   samefiler@   OSErrorrs   shutilZmoverC   )r   r<   ZoutputfilenameZsamer   r   r   rN      s    

zVerifier._compile_modulec                 C   s0   | j s
t| jd k	r"| j| jS | j S d S r   )rC   AssertionErrorr/   r.   rS   rO   r   r   r   rR      s    

zVerifier._load_library)NNNr%   Fr&   NN)N)N)r    r!   r"   rH   rM   rP   rS   rZ   r[   r]   r0   rQ   rl   rJ   rN   rR   r   r   r   r   r$      s(              
)
	
	
r$   Fc                 C   s|   t rd}|sPdtjkrd}n4zdd l}W n tk
r@   d}Y nX | j|k	rPd}|rfddlm} |jS ddlm	} |j
S d S )NTZ__pypy__r   ?r   )vengine_gen)vengine_cpy)_FORCE_GENERIC_ENGINEr4   builtin_module_names_cffi_backendrg   Z_backendr%   r|   ZVGenericEnginer}   ZVCPythonEngine)r+   rE   r   r|   r}   r   r   r   r-      s     


r-   c                  C   sH   t rt S tjd} | r| S tdjj}tj	tj
tj|dS )NZCFFI_TMPDIRr(   __pycache__)_TMPDIRr=   environgetr4   	_getframef_codeco_filenamer>   abspathr3   r`   )resultrk   r   r   r   r;      s    r;   c                 C   s   | a dS )z:Set the temporary directory to use instead of __pycache__.N)r   r_   r   r   r   
set_tmpdir   s    r   c              	   C   s$  | pt  } zt| }W n tk
r.   Y dS X |r:d}nt d  }|D ]\}| drL| |sz| drLzttj	
| | W qL tk
r   Y qLX qLtj	
| dg}|D ]`}zDt|D ]4}tj	
||}tj	|r|| qt| qW q tk
r   Y qX qdS )z}Clean up the temporary directory by removing all files in it
    called `_cffi_*.{c,so}` as well as the `build` subdirectory.Nr&   r   Z_cffi_Zbuild)r;   r=   listdirrx   r:   lower
startswithrX   unlinkr>   r3   isdirappend)r<   Zkeep_soZfilelistr   r^   Z	clean_dirdirr   r   r   cleanup_tmpdir  s8    
r   c                  C   s&   t  } | s"tjdkrdg} ndg} | S )NZwin32z.pydz.so)r   r4   platform)suffixesr   r   r   r:   $  s    
r:   c                 C   s*   t j| }|r&t j|s&t | d S r   )r=   r>   r`   r   makedirs)rk   r`   r   r   r   rs   /  s    rs   )NF)r4   r=   r7   ry   ior%   r   r   errorr   r5   importlib.machineryr   r   r   StringIOr   BytesIOobjectr$   r~   r-   r   r;   r   r   r:   rs   r   r   r   r   <module>   s*   (


 ?

