???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
MZ+                 @   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   c               C   s   t jjd d  S )N)	importlib	machineryEXTENSION_SUFFIXES r
   r
    /usr/lib64/python3.6/verifier.py_extension_suffixes   s    r   c               C   s   dd t j D S )Nc             S   s    g | ]\}}}|t jkr|qS r
   )impZC_EXTENSION).0suffix_typer
   r
   r   
<listcomp>   s    z'_extension_suffixes.<locals>.<listcomp>)r   Zget_suffixesr
   r
   r
   r   r      s    c                   s   e Zd Z fddZ  ZS )NativeIOc                s(   t |tr|jd}tt| j| 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   sv  |j jrtd|j jf || _|| _|s4tj|}t||}|| | _| jj	| |	| _
| j||
| _|rz|rxtdndjtjd d t||g|j }tjdkr|jd}ttj|dd d d@ }|jd	jd
}ttj|dd d d@ }|jdjd
}d|| jj||f }t d }|p0t | _tjj| j|| | _ tj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' r   zutf-8r      l    Z0xLr   0z_cffi_%s_%s%s%sF)r   )%Z_parserZ_uses_new_featurer   ffipreambler   Zflatten_locate_engine_class_vengineZpatch_extension_kwdsflagsmake_relative_tokwds	TypeErrorjoinsysversionr   Z_cdefsourcesversion_infor   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(   r9   Z
modulenamer>   tagforce_generic_engineZsource_extensionr+   relative_tor-   Zflattened_kwdsZvengine_classkeyZk1Zk2r   r
   r
   r   __init__    sD    









zVerifier.__init__c          	   C   s8   | j j& | jr |dkr td| j| W d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'   _lockr?   r   _write_source)r   filer
   r
   r   write_sourceH   s
    
zVerifier.write_sourcec          	   C   s<   | j j* | jrtd| js&| j  | j  W d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'   rF   r@   r   r?   rG   _compile_module)r   r
   r
   r   compile_moduleQ   s    
zVerifier.compile_modulec          	   C   sF   | j j4 | js4| j  | js4| js,| j  | j  | j 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'   rF   r@   _locate_moduler?   rG   rJ   _load_library)r   r
   r
   r   load_library[   s    
zVerifier.load_libraryc             C   sB   t jj| j}|jddd }|jdr>ttdr>|d d }|S )N.r   r   Z_dgettotalrefcountr$   )r:   r;   basenamer=   splitendswithhasattrr0   )r   rR   r
   r
   r   get_module_namek   s
    zVerifier.get_module_namec          
   C   sV   t j  | js0| jj | js&| j  W d Q R X t j| j}| j }t j	||f| j
S )N)r   Z_hack_at_distutilsr?   r'   rF   rG   Zmaybe_relative_pathr<   rV   get_extensionr-   )r   Z
sourcenamemodnamer
   r
   r   rW   v   s    
zVerifier.get_extensionc             C   s   | j jS )N)r*   Z_gen_python_module)r   r
   r
   r   generates_python_module   s    z Verifier.generates_python_modulec                s~   |rzt jj|rzt jj| |j }xTtjD ]J}||kr,|| }t|ttfs\t	d|f  fdd|D }|||< q,W |S )Nz&keyword '%s' should be a list or tuplec                s   g | ]}t jj |qS r
   )r:   r;   r/   )r   fn)dirnamer
   r   r      s    z-Verifier.make_relative_to.<locals>.<listcomp>)
r:   r;   r[   copyr   ZLIST_OF_FILE_NAMESr   listtupler.   )r   r-   rC   rD   Zlstr
   )r[   r   r,      s    
zVerifier.make_relative_toc             C   s   t jj| jst| jrHyt| jd d dg}W n tk
r>   d S X |j}nd }| jj	| j
 |t }|d krnd S || _| jj  d| _d S )N__doc__T)r:   r;   isfiler=   r>   
__import__ImportError__path__r*   find_modulerV   r7   Zcollect_typesr@   )r   Zpkgr;   filenamer
   r
   r   rL      s    
zVerifier._locate_modulec          
   C   s$   || j _z| j j  W d | j `X d S )N)r*   Z_fZwrite_source_to_f)r   rH   r
   r
   r   _write_source_to   s    zVerifier._write_source_toc             C   s   |d k	r| j | nt }| j | |j }tjj| jrbt| jd}|j |k }W d Q R X nd}|rt	| j t| jd}|j
| W d Q R X d| _d S )NrTw)rf   r   getvaluer:   r;   existsr<   openread_ensure_dirr   r?   )r   rH   fZsource_datafpZneeds_writtenr
   r
   r   rG      s    

zVerifier._write_sourcec             C   sp   t jj| j}tj|| j }ytj|| j}W n t	k
rH   d}Y nX |sft
| j tj|| j d| _d S )NFT)r:   r;   r[   r<   r   compilerW   samefiler=   OSErrorrm   shutilZmover@   )r   r9   ZoutputfilenameZsamer
   r
   r   rJ      s    

zVerifier._compile_modulec             C   s0   | j s
t| jd k	r"| jj| jS | jj S d S )N)r@   AssertionErrorr+   r*   rN   )r   r
   r
   r   rM      s    

zVerifier._load_library)NNNr!   Fr"   NN)N)N)r   r   r   rE   rI   rK   rN   rV   rW   rY   r,   rL   rf   rG   rJ   rM   r
   r
   r
   r   r       s     
&
	


r    Fc             C   s|   t rd}|sPdtjkrd}n4y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_ENGINEr0   builtin_module_names_cffi_backendrb   Z_backendr!   rv   ZVGenericEnginerw   ZVCPythonEngine)r'   rB   rz   rv   rw   r
   r
   r   r)      s     


r)   c              C   sH   t rt S tjjd} | r| S tjdjj}tjj	tjj
tjj|dS )NZCFFI_TMPDIRr$   __pycache__)_TMPDIRr:   environgetr0   	_getframef_codeco_filenamer;   abspathr/   r[   )resultre   r
   r
   r   r8      s    r8   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   s0  | pt  } ytj| }W n tk
r,   dS X |r8d}nt d j }xd|D ]\}|j jdrL|j j|sz|j jdrLytjtj	j
| | W qL tk
r   Y qLX qLW tj	j
| dg}xn|D ]f}yJxDtj|D ]6}tj	j
||}tj	j|r |j| qtj| qW W q tk
r&   Y qX qW dS )z}Clean up the temporary directory by removing all files in it
    called `_cffi_*.{c,so}` as well as the `build` subdirectory.Nz.cr   Z_cffi_Zbuild)r8   r:   listdirrr   r7   lower
startswithrT   unlinkr;   r/   isdirappend)r9   Zkeep_soZfilelistr   rZ   Z	clean_dirdirr
   r
   r   cleanup_tmpdir  s2    



r   c              C   s&   t  } | s"tjdkrdg} ndg} | S )NZwin32z.pydz.so)r   r0   platform)suffixesr
   r
   r   r7   $  s    
r7   c             C   s,   t jj| }|r(t jj| r(t j| d S )N)r:   r;   r[   r   makedirs)re   r[   r
   r
   r   rm   /  s    rm   )r   r   )r   )NF)r0   r:   r4   rs   ior!   r   r   errorr   r2   Zimportlib.machineryr   r   r   StringIOr   BytesIOobjectr    rx   r)   r|   r8   r   r   r7   rm   r
   r
   r
   r   <module>   s*   (


 ?

