Your IP : 3.133.122.95


Current Path : /usr/lib64/python3.6/site-packages/cffi/__pycache__/
Upload File :
Current File : //usr/lib64/python3.6/site-packages/cffi/__pycache__/recompiler.cpython-36.opt-1.pyc

3

M��Z���@sddlZddlZddlZddlmZmZddlmZddlTdZ	dZ
dZGd	d
�d
�ZGdd�d�Z
Gd
d�d�ZGdd�d�ZGdd�d�ZGdd�d�Zejd0kr�ejZnGdd�dej�Zdd�Zd1dd�Zd2dd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd3d,d-�Zd.d/�ZdS)4�N�)�ffiplatform�model)�VerificationError)�*i&i'i(c@s&eZdZd	dd�Zdd�Zdd�ZdS)
�
GlobalExprrcCs"||_||_||_||_||_dS)N)�name�address�type_op�size�check_value)�selfrr	r
rr�r�"/usr/lib64/python3.6/recompiler.py�__init__s
zGlobalExpr.__init__cCsd|j|j|jj�|jfS)Nz'  { "%s", (void *)%s, %s, (void *)%s },)rr	r
�	as_c_exprr)r
rrrrszGlobalExpr.as_c_exprcCsd|jj�|j|jfS)Nz
b'%s%s',%d)r
�as_python_bytesrr)r
rrr�as_python_exprszGlobalExpr.as_python_exprN)rr)�__name__�
__module__�__qualname__rrrrrrrrs
rc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�	FieldExprcCs"||_||_||_||_||_dS)N)r�field_offset�
field_size�fbitsize�
field_type_op)r
rrrrrrrrrs
zFieldExpr.__init__cCs>dt|j�}d|j|jfd||jfd||jj�fS)N� z  { "%s", %s,
z     %s   %s,
z     %s   %s },)�lenrrrrr)r
Zspacesrrrr#szFieldExpr.as_c_exprcCst�dS)N)�NotImplementedError)r
rrrr)szFieldExpr.as_python_exprcCsD|jjtkrd}n|jjtkr*t|j�}nt�d|jj�||jfS)N�z	b'%s%s%s')	r�op�OP_NOOP�OP_BITFIELD�format_four_bytesrrrr)r
Z	size_exprrrr�as_field_python_expr,s
zFieldExpr.as_field_python_exprN)rrrrrrr$rrrrrsrc@s$eZdZdd�Zdd�Zdd�ZdS)�StructUnionExprc		Cs4||_||_||_||_||_||_||_||_dS)N)r�
type_index�flagsr�	alignment�comment�first_field_index�c_fields)	r
rr&r'rr(r)r*r+rrrr8szStructUnionExpr.__init__cCsPd|j|j|jfd|j|jfd|jt|j�f|jrFd|jnddS)Nz  { "%s", %d, %s,z
    %s, %s, z%d, %d z	/* %s */ rz},)	rr&r'rr(r*rr+r))r
rrrrCszStructUnionExpr.as_c_exprcCs>t|jt�}dd�|jD�}dt|j�t|�|jdj|�fS)NcSsg|]}|j��qSr)r$)�.0Zc_fieldrrr�
<listcomp>Lsz2StructUnionExpr.as_python_expr.<locals>.<listcomp>z(b'%s%s%s',%s)�,)�evalr'ZG_FLAGSr+r#r&r�join)r
r'Zfields_exprrrrrJs
zStructUnionExpr.as_python_exprN)rrrrrrrrrrr%7sr%c@s$eZdZdd�Zdd�Zdd�ZdS)�EnumExprcCs"||_||_||_||_||_dS)N)rr&r�signed�allenums)r
rr&rr2r3rrrrUs
zEnumExpr.__init__cCsd|j|j|j|j|jfS)Nz1  { "%s", %d, _cffi_prim_int(%s, %s),
    "%s" },)rr&rr2r3)r
rrrr\szEnumExpr.as_c_exprcCsNdtdtd	td
tdtdtd
tdti|j|j	f}dt
|j�t
|�|j|j
fS)Nrr���zb'%s%s%s\x00%s')rr)rr)r4r)r4r)r5r)r5r)r6r)r6r)Z
PRIM_UINT8Z	PRIM_INT8ZPRIM_UINT16Z
PRIM_INT16ZPRIM_UINT32Z
PRIM_INT32ZPRIM_UINT64Z
PRIM_INT64rr2r#r&rr3)r
�
prim_indexrrrras

zEnumExpr.as_python_exprN)rrrrrrrrrrr1Tsr1c@s$eZdZdd�Zdd�Zdd�ZdS)�TypenameExprcCs||_||_dS)N)rr&)r
rr&rrrrmszTypenameExpr.__init__cCsd|j|jfS)Nz  { "%s", %d },)rr&)r
rrrrqszTypenameExpr.as_c_exprcCsdt|j�|jfS)Nzb'%s%s')r#r&r)r
rrrrtszTypenameExpr.as_python_exprN)rrrrrrrrrrr8lsr8c@s�eZdZdZd�dd�Zdd�Zdd�Zd	d
�Zdd�Zd
ddddgZ	dd�Z
d�dd�Zdd�Zdd�Z
dd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Zd=d>�Zd?d@�Z d�dBdC�Z!dDdE�Z"dFdG�Z#dHdI�Z$e$Z%dJdK�Z&dLdM�Z'e'Z(dNdO�Z)e)Z*dPdQ�Z+dRdS�Z,dTdU�Z-d�dWdX�Z.dYdZ�Z/d[d\�Z0d]d^�Z1d_d`�Z2d�dadb�Z3dcdd�Z4dedf�Z5dgdh�Z6didj�Z7dkdl�Z8dmdn�Z9dodp�Z:dqdr�Z;dsdt�Z<dudv�Z=e=Z>Z?dwdx�Z@dydz�ZAd{d|�ZBd}d~�ZCdd��ZDeDZEZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMeMZNeMZOd�d��ZPd�d��ZQd�d��ZReRZSd�d��ZTdAS)��
RecompilerrFcCs||_||_||_t|_dS)N)�ffi�module_name�target_is_python�VERSION_BASE�_version)r
r:r;r<rrrr~szRecompiler.__init__cCst|j|�|_dS)N)�maxr>)r
Zverrrr�
needs_version�szRecompiler.needs_versioncCs�i|_|jd�t|jtd�}g|_xt|D]l}|jr*t|j�|j|<|jj|�x8|jD].}|j|dkrzt|j�|j|<|jj|�qXW|jjd�q*WxZ|D]R}|jr�|j|dkr�t|j�|j|<|jj|�|j	r�|j
dk	r�|jjd�q�Wi|_i|_x@|D]8}t
|tj��r&d|j|<nt
|tj��rd|j|<�qWx.tt|jdd�d��D]\}}||j|<�q\Wx.tt|jdd�d��D]\}}||j|<�q�Wx0|D](}t|d|jj�}|||j|��q�Wx|jD]}�q�Wt|j�|_dS)	NZcollecttype)�keyZENDZLENcSs|jS)N)r)�tprrr�<lambda>�sz/Recompiler.collect_type_table.<locals>.<lambda>cSs|jS)N)r)rBrrrrC�sZ_emit_bytecode_)�
_typesdict�	_generate�sorted�str�
cffi_typesZis_raw_functionr�append�argsZ
is_array_type�length�_struct_unions�_enums�
isinstancer�
StructOrUnion�EnumType�	enumerate�getattr�	__class__r�tuple)r
Z	all_declsrB�tp1�i�methodr rrr�collect_type_table�sL






zRecompiler.collect_type_tablecCs�t|tj�s2t|t�r.x|D]}|j|�qWdS||jkr�d|j|<t|tj�rb|j|j��ntt|tj�r�|j	dk	r�||j
jjkr�xN|j
�D] \}}}}|j|j|||��q�Wn x|j�D]\}}|j|�q�WdS)N)rNrZBaseTypeByIdentityrT�_do_collect_typerD�FunctionPtrType�as_raw_functionrO�fldtypesr:�_parser�_included_declarations�
enumfields�_field_typeZ
_get_items)r
rB�xZname1rU�_rrrrY�s 




zRecompiler._do_collect_typec
Cs�|jjjj�}x�t|�D]�\}\}}|jdd�\}}yt|d||f�}Wn tk
rjtd|��YnXy||_	|||�Wqt
k
r�}	ztj|	|��WYdd}	~	XqXqWdS)Nrrz_generate_cpy_%s_%sz"not implemented in recompile(): %r)
r:r]Z
_declarations�itemsrF�splitrR�AttributeErrorr�_current_quals�	ExceptionrZattach_exception_info)
r
�	step_name�lstrrB�qualsZkindZrealnamerW�errrrE�szRecompiler._generate�global�field�struct_union�enum�typenamecCs�i|_x|jD]}g|j|<qWt�|_|jd�|j�x>|jD]4}|j|}|dkrh|jdd�d�t|�|j|<qBW|jd}x|jj	�D]\}}q�W|jd}x|j
j	�D]\}}q�WdS)NZctxrmcSs|jS)N)r)�entryrrrrC�sz0Recompiler.collect_step_tables.<locals>.<lambda>)rArnro)�_lsts�	ALL_STEPS�set�_seen_struct_unionsrE�_add_missing_struct_unions�sortrTrLrcrM)r
rhrirBrVrrr�collect_step_tables�s$



zRecompiler.collect_step_tablesrcCs|jj|d�dS)N�
)�_f�write)r
Zwhatrrr�_prntszRecompiler._prntcCs"|jr|j|�n|j||�dS)N)r<�write_py_source_to_f�write_c_source_to_f)r
�f�preamblerrr�write_source_to_fszRecompiler.write_source_to_fcCs0ttjjtjjt�|�d�}|j�}|j�|S)N�r)�open�os�pathr0�dirname�__file__�	readlines�close)r
�filename�g�linesrrr�_rel_readlinesszRecompiler._rel_readlinescCsz||_|j}|jjdk	r |d�|jd�}|jd�}|jd�|||d�<|dj|��|jjd�dI}|jjdk	�r0|d|jf�|d	�|j	|jj�|d
�|d�|d|f�|d
�|d|f�|d�|d|f�|d�|jd�}|jd�}|jd�|||d�<|dj|��|j
t�|d�|�||�|�|d�|�|d�tdd�|j
j�D��}xLt|j�D]>\}}d}	||k�r�d||j�}	|d||j�|	f��q�W|j�s�|d�|d�|�t�|_|jd�i}
xn|jD]d}|j|}t|�|
|<|
|dk�r|d||f�x|D]}
||
j���qBW|d�|��qW|jj�r|d �xr|jjD]f}y|jdd!�\}}Wn(tk
�r�td"|j|f��YnX|dk�r�td#��|d$|f��q�W|d%�|d�|�|d&�|d'�x8|jD].}|
|dk�r@|d(|�n|d)|��q Wx.|jD]$}|d*k�rZ|d+|
||f��qZW|jj�r�|d,�n|d-�|d.t|j�f�d}|j�r�|dO}|d/|�|d�|�|d0�|d1�|d�|�|d�|d2�|d3|f�|d4�|j�rD|d5�|d6�|d7�|d8|j�|d9�|d:�|d;�|d�|d<�|d=�|d>�|d?�|d@|f�|dA�|dB|f�|dC�|dC�|d
�|d2�|dD|f�|d4�|dE|j|jf�|d<�|d�|d2�|dF|f�|d4�|dG|j|jf�|d<�|d�|�|d0�|dH�|d�d|_dS)JNz#define _CFFI_USE_EMBEDDINGz_cffi_include.hz#include "parse_c_type.h"
zparse_c_type.hrr�.z#define _CFFI_MODULE_NAME  "%s"z1static const char _CFFI_PYTHON_STARTUP_CODE[] = {z0 };z#ifdef PYPY_VERSIONz5# define _CFFI_PYTHON_STARTUP_FUNC  _cffi_pypyinit_%sz#elif PY_MAJOR_VERSION >= 3z-# define _CFFI_PYTHON_STARTUP_FUNC  PyInit_%sz#elsez*# define _CFFI_PYTHON_STARTUP_FUNC  init%sz#endifz_embedding.hz#include "_cffi_errors.h"
z_cffi_errors.hz>/************************************************************/zstatic void *_cffi_types[] = {cSsg|]\}}||f�qSrr)r,rBrVrrrr-Gsz2Recompiler.write_c_source_to_f.<locals>.<listcomp>z // z/* %2d */ %s,%sz  0z};�declrz.static const struct _cffi_%s_s _cffi_%ss[] = {z.static const char * const _cffi_includes[] = {r4zQffi object %r includes %r, but the latter has not been prepared with set_source()zMnot implemented yet: ffi.include() of a Python-based ffi inside a C-based ffiz  "%s",z  NULLz?static const struct _cffi_type_context_s _cffi_type_context = {z  _cffi_types,z  _cffi_%ss,z  NULL,  /* no %ss */rmz  %d,  /* num_%ss */z  _cffi_includes,z  NULL,  /* no includes */z  %d,  /* num_types */z  %d,  /* flags */z#ifdef __GNUC__z?#  pragma GCC visibility push(default)  /* for -fvisibility= */ZPyMODINIT_FUNCz"_cffi_pypyinit_%s(const void *p[])�{z%    if (((intptr_t)p[0]) >= 0x0A03) {zQ        _cffi_call_python_org = (void(*)(struct _cffi_externpy_s *, char *))p[1];z    }z    p[0] = (const void *)0x%x;z    p[1] = &_cffi_type_context;z#if PY_MAJOR_VERSION >= 3z    return NULL;�}z#  ifdef _MSC_VERz     PyMODINIT_FUNCz#  if PY_MAJOR_VERSION >= 3z%     PyInit_%s(void) { return NULL; }z#  elsez     init%s(void) { }z#  endifzPyInit_%s(void)z5  return _cffi_init("%s", 0x%x, &_cffi_type_context);zinit%s(void)z.  _cffi_init("%s", 0x%x, &_cffi_type_context);z#  pragma GCC visibility pop���)rzr|r:�
_embeddingr��indexr0r;rd�_print_string_literal_in_arrayr@�VERSION_EMBEDDED�dictrDrcrQrH�_get_c_namerrt�_seen_constantsrErsrrr�_included_ffis�_assigned_sourcerer�
_num_externpyr>)r
rr��prntr�rVZbase_module_name�typeindex2typer r)Znumsrhrirq�ffi_to_include�included_module_name�included_sourcer'rrrr~s
















zRecompiler.write_c_source_to_fcsbt|t�rd|fSt|ttf�rZ�fdd�|D�}t|�dkrJ|jd�ddj|�fS|j�S)Nzb'%s'csg|]}�j|��qSr)�_to_py)r,�item)r
rrr-�sz%Recompiler._to_py.<locals>.<listcomp>rrz(%s)r.)rNrG�listrTrrIr0r)r
raZrepr)r
rr��s


zRecompiler._to_pyc
Cs�||_|j}|d�|d�t|jjp(f�}xzt|�D]n}|jj|}y|jdd�\}}Wn&tk
r�td|j|f��YnX|dk	r�td��|d||f�q6W|�|d|j	f�|d|j
f�d|_
t|j�|_d	d
�|jD�}|d|j
dj|��f�td
d
�|jj�D��}	xH|jD]>}
|j|
}t|�dk�r*|
dk�r*|d|
|j
|�f��q*W|dk�r�|ddjdd
�t|�D��f�|d�dS)Nz# auto-generated filezimport _cffi_backendr4zQffi object %r includes %r, but the latter has not been prepared with set_source()zMnot implemented yet: ffi.include() of a C-based ffi inside a Python-based ffizfrom %s import ffi as _ffi%dzffi = _cffi_backend.FFI('%s',z    _version = 0x%x,cSsg|]}|j��qSr)r)r,r rrrr-�sz3Recompiler.write_py_source_to_f.<locals>.<listcomp>z    _types = %s,rcSsg|]\}}||f�qSrr)r,rBrVrrrr-�srrmz    _%ss = %s,z    _includes = (%s,),z, cSsg|]}d|�qS)z_ffi%dr)r,rVrrrr-�s�))rzr|rr:r��ranger�rerr;r>rTrHr�r0r�rDrcrsrr)r
rr�Znum_includesrVr�r�r�Z	types_lstr�rhrirrrr}�sB

zRecompiler.write_py_source_to_fcCs
|j|S)N)rD)r
�typerrr�_gettypenumszRecompiler._gettypenumc	Cspd}t|tj�r�|j�r�|j�r<|jdkr<d}d|j}nPt|tj�rZd|jd�f}n2|jd�}d||jjdd�f}|dkr�|j	t
�d}n�t|tj�r�|j||||�dSt|tj
�s�t|tj�r�|jd||j|�|f�|jd
|�dSt|tj��r&d|jd�}d|j|�}d}nt|��|jd||||f�|jd||jd�|f�|jd
|�dS)Nr�_BoolZ_cffi_to_c_intz, %sz(%s)_cffi_to_c_doublez(%s)_cffi_to_c_%srrb�char16_t�char32_tz-1z6  if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)z    %s;z(%s)_cffi_to_c_pointerz, _cffi_type(%d)ZNULLz  %s = %s(%s%s);z'  if (%s == (%s)%s && PyErr_Occurred()))r�r�)rNr�BasePrimitiveType�is_complex_type�is_integer_typer�UnknownFloatType�
get_c_name�replacer@�VERSION_CHAR16CHAR32�PointerType�"_convert_funcarg_to_c_ptr_or_arrayZStructOrUnionOrEnumr|r�rZr)	r
rB�fromvar�tovar�errcodeZextraargZ	converter�cnameZerrvaluerrr�_convert_funcarg_to_cs@

z Recompiler._convert_funcarg_to_ccCst|tj�r|jd�dS)NzPy_ssize_t datasize)rNrr��add)r
rB�	localvarsrrr�_extra_local_variables3sz!Recompiler._extra_local_variablescCs�|jd�|jd|j|�||f�|jd�|jd�|jd|�|jd||jd�f�|jd|f�|jd	||j|�|f�|jd|�|jd
�dS)Nz1  datasize = _cffi_prepare_pointer_call_argument(z(      _cffi_type(%d), %s, (char **)&%s);z  if (datasize != 0) {z    if (datasize < 0)z	      %s;z&    %s = (%s)alloca((size_t)datasize);rz,    memset((void *)%s, 0, (size_t)datasize);zL    if (_cffi_convert_array_from_object((char *)%s, _cffi_type(%d), %s) < 0)z  })r|r�r�)r
rBr�r�r�rrrr�7s


z-Recompiler._convert_funcarg_to_c_ptr_or_arraycCsLt|tj�r�|j�r,|jdkr,d||jfSt|tj�rBd|fS|jdkr�|j�r�|jjdd�}|dkrv|jt	�d	||fSd
||j
|�fSn�t|tjtjf�r�d||j
|�fSt|tj
�r�d||j
tj|j��fSt|tj��r |jdk�rtd|j�|f��d
||j
|�fSt|tj��r@d
||j
|�fSt|��dS)Nr�z_cffi_from_c_int(%s, %s)z_cffi_from_c_double(%s)zlong doublerrbr�r�z_cffi_from_c_%s(%s)z/_cffi_from_c_deref((char *)&%s, _cffi_type(%d))z0_cffi_from_c_pointer((char *)%s, _cffi_type(%d))z!'%s' is used as %s, but is opaquez0_cffi_from_c_struct((char *)&%s, _cffi_type(%d)))r�r�)rNrr�r�rr�r�r�r@r�r�r�rZ�	ArrayTyper�rOZfldnames�	TypeErrorr�rPr)r
rB�var�contextr�rrr�_convert_expr_from_cGs8

zRecompiler._convert_expr_from_ccCs|j|d|f�S)Nz
(*(%s *)0))�_global_type)r
rBrrrr�
_typedef_typejszRecompiler._typedef_typecCs|j|j||��dS)N)rYr�)r
rBrrrr�!_generate_cpy_typedef_collecttypemsz,Recompiler._generate_cpy_typedef_collecttypecCsdS)Nr)r
rBrrrr�_generate_cpy_typedef_declpsz%Recompiler._generate_cpy_typedef_declcCs$|j|}|jdjt||��dS)Nrp)rDrrrIr8)r
rBrr&rrr�_typedef_ctxss
zRecompiler._typedef_ctxcCsf|j||�}|j||�t|dd�dkr<|j||jdd�n&t|tj�rb|j|j|jj|j|d�dS)N�originZunknown_type)�
approxname)r��	named_ptr)	r�r�rR�_struct_ctxrrNrZNamedPointerType�totype)r
rBrrrr�_generate_cpy_typedef_ctxwsz$Recompiler._generate_cpy_typedef_ctxcCs*|j|j��|jr&|jr&|j|�dS)N)rYr[�ellipsisr<)r
rBrrrr�"_generate_cpy_function_collecttype�sz-Recompiler._generate_cpy_function_collecttypecCs�|jr|j||�dS|j}t|j�}|dkr4d}n|dkrBd}nd}g}g}d|}x:t|j�D],\}	}
|j|
jd|	|��|jd|	�qbWd	j|�}|p�d
}|j	r�|j	d}nd}d
|||f}
|d|j
j|
�f�|d�d	j|�}d}t|j
tj
��rd}|d|||f�|d�|d�|d�|d||f�|d�d|}x4t|j�D]&\}	}
|
jd|	|�}|d|��q^Wt�}x|jD]}
|j|
|��q�Wx|D]}|d|f��q�Wt|j
tj
��sd}d|}d|j
jd|�}||�nd}d}t|j�dk�r~tt|j��}x|D]}	|d|	��q.W|�|d|t|�t|�d	jdd�|D��f�|d�|�x6t|j�D](\}	}
|j|
d|	d|	d �|��q�W|d!�|d"�d#d�tt|j��D�}d	j|�}|d$|||f�|d%�|d&�|�|d'�|dk�r0|d(�|�rP|d)|j|j
d*d+��n|d,�|d-�|d�|d.�d/d0�}d1}g}g}d|}x^t|j�D]P\}	}
d}||
��r�d2}d3}|
jd4||	f|�}|j|�|jd5||	f��q�W|j
}||��r.d|}|jd6|�}|jd|�tj}d}d7}d3}|�r�d	j|�}|�pFd
}d8|||f}
|d|j|
�f�|d�|�r�||�d	j|�}|d$|||f�|�r�|d9�|d�n|d:||f�|d;�|�dS)<NrZnoargrZarg0rJzargument of %sz x%dzx%dz, �voidrrz%s_cffi_d_%s(%s)z	static %sr�zreturn z  %s%s(%s);r�z#ifndef PYPY_VERSIONzstatic PyObject *z(_cffi_f_%s(PyObject *self, PyObject *%s)z  %s;z	result = zresult of %sz resultz  PyObject *arg%d;z1  if (!PyArg_UnpackTuple(args, "%s", %d, %d, %s))cSsg|]}d|�qS)z&arg%dr)r,rVrrrr-�sz:Recompiler._generate_cpy_function_decl.<locals>.<listcomp>z    return NULL;zarg%dzreturn NULLz  Py_BEGIN_ALLOW_THREADSz  _cffi_restore_errno();cSsg|]}d|�qS)zx%dr)r,rVrrrr-�sz  { %s%s(%s); }z  _cffi_save_errno();z  Py_END_ALLOW_THREADSz  (void)self; /* unused */z  (void)noarg; /* unused */z  return %s;�resultzresult typez  Py_INCREF(Py_None);z  return Py_None;z#elsecSs t|tj�pt|tj�o|j�S)N)rNrrO�
PrimitiveTyper�)r�rrr�need_indirection�sz@Recompiler._generate_cpy_function_decl.<locals>.need_indirectionFrTz %sx%dz%sx%dz *resultz
*result = z%s_cffi_f_%s(%s)z  return result;z#  define _cffi_f_%s _cffi_d_%sz#endif)r��_generate_cpy_constant_declr|rrJrQrIr�r0�abir�rNr�VoidTypertr�r�r�r��insertZ	void_type)r
rBrr��numargsZargname�	argumentsZcall_argumentsr�rVr��repr_argumentsr��name_and_argumentsZresult_code�argr�r�Zresult_decl�rngr��
differenceZindirectionZ	tp_resultrrr�_generate_cpy_function_decl�s�














z&Recompiler._generate_cpy_function_declcCs�|jr|jr|j||�dS|j|j�}t|j�}|jrBt}n |dkrPt}n|dkr^t	}nt
}|jdjt
|d|t||�d|d��dS)Nrrrlz
_cffi_f_%sz
_cffi_d_%s)r)r�r<�_generate_cpy_constant_ctxrDr[rrJZOP_DLOPEN_FUNCZOP_CPYTHON_BLTN_NZOP_CPYTHON_BLTN_OZOP_CPYTHON_BLTN_VrrrIr�CffiOp)r
rBrr&r�Z	meth_kindrrr�_generate_cpy_function_ctx#s 


z%Recompiler._generate_cpy_function_ctxcCsTt|tj�rP|j}|dkr0|jd�}d||f}|j|d||j�}tj||�}|S)Nz...rz_cffi_array_len(((%s)0)->%s)z%s[0])rNrr�rKr�r`r�)r
Z	tp_structZ
field_nameZtp_field�
actual_lengthZptr_struct_name�tp_itemrrrr`9s

zRecompiler._field_typecCs0|j|�|jr,x|j�D]}|j|�qWdS)N)rYr<�anonymous_struct_fields�_struct_collecttype)r
rB�fldtyperrrr�Es
zRecompiler._struct_collecttypecCs<|jdkrdS|j}d|f}|d�|d||f�|d�|d�|d�x�|j�D]�\}}}}	y||j�sv|dkr�|d|||f�wXx2t|tj�r�|jdks�|jd	kr�|j}|d
}q�W|d|j	dd
||	d�|f�WqXt
k
�r}
z|dt|
��WYdd}
~
XqXXqXW|d�|d||f�|�dS)Nz_cffi_checkfld_%sZ_CFFI_UNUSED_FNzstatic void %s(%s *p)r�z8  /* only to generate compile-time warnings or errors */z
  (void)p;rz>  (void)((p->%s) | 0);  /* check that '%s.%s' is an integer */z...z[0]z  { %s = &p->%s; (void)tmp; }z*tmpzfield %r)rjz
  /* %s */r�z(struct _cffi_align_%s { char x; %s y; };)r\r|r_r�rNrr�rKr�r�rrG)r
rBr�r�r�Z
checkfuncnameZfnameZftyper�fqualrkrrr�_struct_declLs6

&zRecompiler._struct_declNcCs�|j|}d}g}t|tj�r(|jd�|jdkr@|jd�d}||jjjkr�|dksd||jjjkr�|jdkrpn|j	s�t
|j��r�n
|jd�|jr�|jd�n|jd�d}dj
|�p�d	}g}|dk�rZ|j}	t|j|	��}
�x
|
D�]\}}}
}|j|||�}|j|d
|j|f�t}|
dk�r6t}d|
}nL|dk�sZt|tj��r`|jdk�r`d
}n"d|dk�rv|jd�n|j|f}|dk�s�|
dk�r�d
}n,|dk	�r�d|j|f}nd|jd�|f}|jt||||
t||j|���q�Wt|jd�}|jdj|�|dk�r$d}d}d}n4|dk	�r@d|jf}d}nd|f}d|f}d}nd
}d}d}|}|jdjt|j|||||||��|jj |�dS) NZ
_CFFI_F_UNIONZ_CFFI_F_OPAQUEZopaqueZ_CFFI_F_CHECK_FIELDSZ_CFFI_F_PACKEDZ_CFFI_F_EXTERNALZexternal�|�0z
field '%s.%s'rz
%d /* bits */z
(size_t)-1zsizeof(((%s)0)->%s)rz"((char *)&((%s)0)->%s) - (char *)0zoffsetof(%s, %s)rrmz
(size_t)-2r4Zunnamedzsizeof(*(%s)0)z-1 /* unknown alignment */z
sizeof(%s)z"offsetof(struct _cffi_align_%s, y)rrn���r�r�)!rDrNrZ	UnionTyperIr\r:r]r^�partial�anyr�Zpackedr0r<r�r_r`�_check_not_opaquerr!r"r�rKr�rr�rrr�extendr%rur�)r
rBr�r�r�r&Zreason_for_not_expandingr'r+Zexpand_anonymous_struct_unionr_Zfldnamer�rr�r r�offsetr*Zalignr)rrrr�ms�


















zRecompiler._struct_ctxcCs>xt|tj�r|j}qWt|tj�r:|jdkr:td|��dS)Nz0%s is of an opaque type (not declared in cdef()))rNrr�r�rOr\r�)r
rB�locationrrrr��s

zRecompiler._check_not_opaquecCs�t|jj��}|jdd�d�x�|D]�\}}||jkr$|jrJtd|f��|jjd�rx|jdd�j	�rx|jdd�}n4|jdkr�|j
dkr�d}|j|d�ntd	|f��|j|d|�q$WdS)
NcSs|dS)Nrr)Ztp_orderrrrrC�sz7Recompiler._add_missing_struct_unions.<locals>.<lambda>)rAzDinternal inconsistency: %r is partial but was not seen at this point�$rZ_IO_FILE�FILEzinternal inconsistency: %r)
r�rLrcrwrur�rr�
startswith�isdigitZ	forcenamer�r�)r
rirB�orderr�rrrrv�s


z%Recompiler._add_missing_struct_unionscCs|j|�dS)N)r�)r
rBrrrr� _generate_cpy_struct_collecttype�sz+Recompiler._generate_cpy_struct_collecttypecCs2|jd�}d|kr"||jdd�fS|d|fSdS)Nrrrb)r�r�)r
rBr�rrr�
_struct_names�s
zRecompiler._struct_namescCs|j|f|j|���dS)N)r�r�)r
rBrrrr�_generate_cpy_struct_decl�sz$Recompiler._generate_cpy_struct_declcCs|j|f|j|���dS)N)r�r�)r
rBrrrr�_generate_cpy_struct_ctx�sz#Recompiler._generate_cpy_struct_ctxcCs(t|tj�r|j||�n
|j|�dS)N)rNrrP�_generate_cpy_enum_collecttyper�)r
rBrrrr�#_generate_cpy_anonymous_collecttype�sz.Recompiler._generate_cpy_anonymous_collecttypecCs.t|tj�r|j|�n|j||d|�dS)N�typedef_)rNrrP�_generate_cpy_enum_declr�)r
rBrrrr�_generate_cpy_anonymous_decl�sz'Recompiler._generate_cpy_anonymous_declcCs0t|tj�r|j||�n|j||d|�dS)Nr�)rNrrP�	_enum_ctxr�)r
rBrrrr�_generate_cpy_anonymous_ctx�sz&Recompiler._generate_cpy_anonymous_ctx�constcCs�||f|jkrtd||f��|jj||f�|j}d||f}|r�|d|�|d�|d|f�|d||f�|dk	r�|dkr�d|f}|d	|f�|d
�|d�|d�n2|d
|�|d�|d|jd�|f�|d�|�dS)Nz duplicate declaration of %s '%s'z_cffi_%s_%sz$static int %s(unsigned long long *o)r�z  int n = (%s) <= 0;zI  *o = (unsigned long long)((%s) | 0);  /* check that %s is an integer */rz%dUz"  if (!_cffi_check_int(*o, n, %s))z    n |= 2;z  return n;r�zstatic void %s(char *o)z  *(%s)o = %s;r)r�rr�r|r�)r
�is_intrrB�categoryrr��funcnamerrr�_generate_cpy_consts0

zRecompiler._generate_cpy_constcCs"|j�}|s|jr|j|�dS)N)r�r<rY)r
rBrrrrr�"_generate_cpy_constant_collecttype&sz-Recompiler._generate_cpy_constant_collecttypecCs|j�}|j|||�dS)N)r�r)r
rBrrrrrr�+sz&Recompiler._generate_cpy_constant_declcCs`|jr|j�rttd�}n$|jr(t}nt}|j|}t||�}|jdjt	|d||��dS)Nrrlz_cffi_const_%sr�)
r<r�r��OP_CONSTANT_INTZOP_DLOPEN_CONSTZOP_CONSTANTrDrrrIr)r
rBrr
Z
const_kindr&rrrr�/s


z%Recompiler._generate_cpy_constant_ctxcCs|j|�dS)N)rY)r
rBrrrrr�?sz)Recompiler._generate_cpy_enum_collecttypecCs x|jD]}|jd|�qWdS)NT)�enumeratorsr)r
rBr�
enumeratorrrrr�Bsz"Recompiler._generate_cpy_enum_declcCs�|j|}ttd�}|jr"|j�x:t|j|j�D](\}}|jdj	t
|d|||d��q2W|dk	r�d|kr�|jr�d|}d|}n4|j|jg�}	|jj
|	�}tt|jj|	d��dk�}d	j|j�}
|jd
j	t|j||||
��dS)
Nrrlz_cffi_const_%s)rr�z
sizeof(%s)z
((%s)-1) <= 0rr.ror�r�)rDr��OP_ENUMr<Zcheck_not_partial�zipr	Z
enumvaluesrrrIrZbuild_baseinttyper:Zsizeof�int�castr0r1r)r
rBr�r&r
r
Z	enumvaluerr2Zbasetpr3rrrrFs"




zRecompiler._enum_ctxcCs|j||j��dS)N)rr�)r
rBrrrr�_generate_cpy_enum_ctx[sz!Recompiler._generate_cpy_enum_ctxcCsdS)Nr)r
rBrrrr�_generate_cpy_macro_collecttypeasz*Recompiler._generate_cpy_macro_collecttypecCs&|dkrd}n|}|jd||d�dS)Nz...T)r)r)r
rBrrrrr�_generate_cpy_macro_decldsz#Recompiler._generate_cpy_macro_declcCsT|dkr"|jrtd|f��d}n|}ttd�}|jdjt|d|||d��dS)Nz...zGcannot use the syntax '...' in '#define %s ...' when using the ABI moderrlz_cffi_const_%s)rr�)r<rr�rrrrIr)r
rBrrr
rrr�_generate_cpy_macro_ctxks


z"Recompiler._generate_cpy_macro_ctxcCsFt|tj�rB|j}|dkr$d|f}|j|jd|�}tj||�}|S)Nz...z_cffi_array_len(%s)z%s[0])rNrr�rKr�r�)r
rBZglobal_namer�r�rrrr�|s
zRecompiler._global_typecCs|j|j||��dS)N)rYr�)r
rBrrrr�"_generate_cpy_variable_collecttype�sz-Recompiler._generate_cpy_variable_collecttypecCs�|j}|j||�}t|tj�r4|jdkr4|j}d}nd}d|f}|d|j||jd��|d�|d||f�|d�|�dS)	Nr�&z*_cffi_var_%s(void)zstatic )rjr�z  return %s(%s);r�)	r|r�rNrr�rKr�r�rf)r
rBrr�Z	ampersandr�rrr�_generate_cpy_variable_decl�s	
z&Recompiler._generate_cpy_variable_declcCsL|j||�}|j|}|jr"t}nt}|jdjt|d|t||���dS)Nrlz_cffi_var_%s)	r�rDr<Z
OP_GLOBAL_VARZOP_GLOBAL_VAR_FrrrIrr�)r
rBrr&r rrr�_generate_cpy_variable_ctx�s

z%Recompiler._generate_cpy_variable_ctxcCs|j|�dS)N)rY)r
rBrrrr�'_generate_cpy_extern_python_collecttype�sz2Recompiler._generate_cpy_extern_python_collecttypecCs|j}t|jtj�rd}nd|}d|jjd|�f}|d|�|d|j||f�|�g}d|}x0t|j�D]"\}}	|	jd||�}
|j	|
�qtWd	j
|�}|p�d
}d||f}|jdkr�d
|}dd�}
tt
|j�dd�}|
|j�r�t|d�}t|jtj��r*d|jjd�||jjd�|f}|d||jj|�f�|d�|d|�|d�xft|j�D]X\}}	d|}
t|	tj��s�|
|	��r�d|
}
tj|	�}	|d|	jd�|d|
f��qjW|d|�t|jtj��s�|d|jjd�f�|d�|�|jd7_dS)Nr�zresult of %sz(int)sizeof(%s)rz3static struct _cffi_externpy_s _cffi_externpy__%s =z  { "%s.%s", %s };zargument of %sz a%dz, r�z%s(%s)�	__stdcallz_cffi_stdcall cSst|tj�o|jdkS)Nzlong double)rNrr�r)rBrrr�may_need_128_bits�sz9Recompiler._extern_python_decl.<locals>.may_need_128_bitsr6�z!sizeof(%s) > %d ? sizeof(%s) : %dz%s%sr�z
  char a[%s];z  char *p = a;za%drz  *(%s)(p + %d) = %s;rz,  _cffi_call_python(&_cffi_externpy__%s, p);z  return *(%s)p;r�r)r|rNr�rr�r�r;rQrJrIr0r�r?rrOr�r�)r
rBrZ
tag_and_spacer�Zsize_of_resultr�r�rVr�r�r�r�rZ	size_of_arrr�_extern_python_decl�sX





"zRecompiler._extern_python_declcCs|j||d�dS)Nzstatic )r)r
rBrrrr� _generate_cpy_extern_python_decl�sz+Recompiler._generate_cpy_extern_python_declcCs|j||d�dS)NzCFFI_DLLEXPORT )r)r
rBrrrr�#_generate_cpy_dllexport_python_decl�sz.Recompiler._generate_cpy_dllexport_python_declcCs|j||d�dS)Nr)r)r
rBrrrr�'_generate_cpy_extern_python_plus_c_decl�sz2Recompiler._generate_cpy_extern_python_plus_c_declcCsR|jrtd��|jrtd��|j|}tt|�}|jdjt	|d|||��dS)Nz,cannot use 'extern "Python"' in the ABI modez$a vararg function is extern "Python"rlz&_cffi_externpy__%s)
r<rr�rrDr�ZOP_EXTERN_PYTHONrrrIr)r
rBrr&r
rrr�_generate_cpy_extern_python_ctx�s


z*Recompiler._generate_cpy_extern_python_ctxcCsz|j}|d�xf|jd�D]X}|d|j��d}x6|D].}t|�dkrT||�d}|dt|�f7}q8W||�qWdS)Nz=// # NB. this is not a string because of a size limit in MSVCTz// r�Lz%d,)r|�
splitlines�rstripr�ord)r
�sr��lineZprinted_line�crrrr�s
z)Recompiler._print_string_literal_in_arraycCsttt�|j|<dS)N)r��OP_PRIMITIVEZ	PRIM_VOIDrH)r
rBr�rrr�_emit_bytecode_VoidTypesz"Recompiler._emit_bytecode_VoidTypecCst|j}tt|�|j|<dS)N)ZPRIMITIVE_TO_INDEXrr�r'rH)r
rBr�r7rrr�_emit_bytecode_PrimitiveTypes
z'Recompiler._emit_bytecode_PrimitiveTypecCs(d|j|j|jf}tt|�|j|<dS)Nzl_cffi_prim_int(sizeof(%s), (
           ((%s)-1) | 0 /* check that %s is an integer type */
         ) <= 0))rr�r'rH)r
rBr�r$rrr�!_emit_bytecode_UnknownIntegerTypesz,Recompiler._emit_bytecode_UnknownIntegerTypecCs$d|j|jf}tt|�|j|<dS)Nzd_cffi_prim_float(sizeof(%s) *
           (((%s)1) / 2) * 2 /* integer => 0, float => 1 */
         ))rr�r'rH)r
rBr�r$rrr�_emit_bytecode_UnknownFloatTypesz*Recompiler._emit_bytecode_UnknownFloatTypecCs�tt|j|j�|j|<|d7}xR|jD]H}|j|}||krht|tj�rX|j	||�ntt
|�|j|<|d7}q(Wt|j�}|j
dk	r�|j
dkr�|dO}ntd|j
f��tt|�|j|<dS)Nrrr4zabi=%r)r�ZOP_FUNCTIONrDr�rHrJrNrr�r)r!r
r�r�rZOP_FUNCTION_END)r
rBr�rUZ	realindexr'rrr�_emit_bytecode_RawFunctionType#s




z)Recompiler._emit_bytecode_RawFunctionTypecCstt|j|j�|j|<dS)N)r��
OP_POINTERrDr�rH)r
rBr�rrr�_emit_bytecode_PointerType6sz%Recompiler._emit_bytecode_PointerTypecCs"|j�}tt|j|�|j|<dS)N)r[r�r-rDrH)r
rBr��rawrrr�_emit_bytecode_FunctionPtrType<sz)Recompiler._emit_bytecode_FunctionPtrTypecCs||j|j}|jdkr(tt|�|j|<nP|jdkrNtdt|�jdd�f��n*tt	|�|j|<tdt|j��|j|d<dS)Nz...zitype %s badly placed: the '...' array length can only be used on global arrays or on fields of structuresz/*...*/r)
rDr�rKr�Z
OP_OPEN_ARRAYrHrrGr�ZOP_ARRAY)r
rBr�Z
item_indexrrr�_emit_bytecode_ArrayType@s

z#Recompiler._emit_bytecode_ArrayTypecCs|j|}tt|�|j|<dS)N)rLr�ZOP_STRUCT_UNIONrH)r
rBr�Zstruct_indexrrr�_emit_bytecode_StructTypeNs
z$Recompiler._emit_bytecode_StructTypecCs|j|}tt|�|j|<dS)N)rMr�rrH)r
rBr�Z
enum_indexrrr�_emit_bytecode_EnumTypeSs
z"Recompiler._emit_bytecode_EnumType)F)r)N)NrN)N)Urrrr�rr@rXrYrErsrxr|r�r�r~r�r}r�r�r�r�r�r�r�r�r�r�r�r�r�r`r�r�r�r�rvr�Z_generate_cpy_union_collecttyper�r�Z_generate_cpy_union_declr�Z_generate_cpy_union_ctxr�r�rrrr�r�r�r�rrrrrr�rrrrZ*_generate_cpy_dllexport_python_collecttypeZ._generate_cpy_extern_python_plus_c_collecttyperrrrrZ"_generate_cpy_dllexport_python_ctxZ&_generate_cpy_extern_python_plus_c_ctxr�r(r)r*r+r,r.Z_emit_bytecode_ConstPointerTypeZ_emit_bytecode_NamedPointerTyper0r1r2Z_emit_bytecode_UnionTyper3rrrrr9{s�
:
,
4-#!
Q	

	
5
r9�cseZdZ�fdd�Z�ZS)�NativeIOcs(t|t�r|jd�}tt|�j|�dS)N�ascii)rNZunicode�encode�superr5r{)r
r$)rSrrr{\s

zNativeIO.write)rrrr{�
__classcell__rr)rSrr5[sr5c
Cs|rtd|f�t|||dkd�}|j�|j�t�}|j||�|j�}y@t|d�� }|jt	|�d�|krvt
�WdQRX|r�td�dSt
k
�rd|tj�f}	t|	d��}|j
|�WdQRXytj|	|�Wn,tk
�rtj|�tj|	|�YnXd	SXdS)
Nz
generating %s)r<r�rz(already up-to-date)Fz%s.~%d�wT)�printr9rXrxr5r��getvaluer��readr�IOErrorr��getpidr{�rename�OSError�unlink)
r:r;r�Ztarget_file�verboseZ
recompilerr�output�f1Ztmp_filerrr�_make_c_or_py_sourceas4
rFFcCst|||||�S)N)rF)r:r;r�Z
target_c_filerCrrr�
make_c_source}s
rGcCst||d||�S)N)rF)r:r;Ztarget_py_filerCrrr�make_py_source�s
rHcCsj|jd�}y$tjtjj|f|dd����Wntk
rBYnX|d|7<tjj|f|��|fS)Nr�rr�r�)rdr��makedirsr�r0rA)Z	outputdir�modname�	extension�partsrrr�_modname_to_file�s
$rMcCs*t||�}|j|||f�t|||�|S)N)rRrI�setattr)�	patchlist�clsrZnew_meth�oldrrr�_patch_meth�s
rRcCs(x"t|�D]\}}}t|||�q
WdS)N)�reversedrN)rOrPrZold_methrrr�_unpatch_meths�srTcs\tjdkr(ddlm}t||ddd��tjdkrXddlm}�fd	d
�}t||d|��dS)N�win32r)�MSVCCompilerZ_remove_visual_c_refcSs|S)Nr)r
Z
manifest_filerrrrC�sz&_patch_for_embedding.<locals>.<lambda>�darwin)�	CCompilercs<d|jkr,t|j�|_|jjd�}d|j|<�|f|�|�S)Nz-bundlez-dynamiclib)Z	linker_sor�r�)r
rJ�kwdsrV)�old_link_shared_objectrr�my_link_shared_object�s


z3_patch_for_embedding.<locals>.my_link_shared_objectZlink_shared_object)�sys�platformZdistutils.msvc9compilerrVrRZdistutils.ccompilerrX)rOrVrXr[r)rZr�_patch_for_embedding�s


r^cslddlm}�jd�rR�dd
��tjdkr6�d7�ntjdkrJ�d7�n�d	7�t||d
�fdd��dS)Nr)�	build_extz.*r4rUz.dllrWz.dylibz.soZget_ext_filenamecs�S)Nr)r
Zext_name)�targetrrrC�sz#_patch_for_target.<locals>.<lambda>r�)Zdistutils.command.build_extr_�endswithr\r]rR)rOr`r_r)r`r�_patch_for_target�s




rbr�T�.ccKs�t|t�s|jd�}|jr$|j|�|dk	�r^|jdk	}|rF|j|�|dkrzt|||�\}}
|rl|g|
}
tj	j
|
�}n|}|	dkr�|r�d|}	nd}	tj||f|�}t
|||||d�}|�rTg}tj�}zn|r�t|�|	dkr�t||	�|�r|dk�rd}nd}td|tj	j|�f�tj|�tjd|||
�}Wdtj|�t|�X|S||fSn<|dk�rxt||d	�\}}t||||d�}|�r�|Sd|fSdS)
Nr6z%s.*r)rCr�zthe current directory isz setting the current directory toz%s %rz.py)rNrGr7Z_windows_unicodeZ_apply_windows_unicoder�Z_apply_embedding_fixrMr�r�r0rZ
get_extensionrG�getcwdr^rbr;�abspath�chdir�compilerTrH)r:r;r��tmpdirZcall_c_compilerZc_fileZsource_extensionZextradirZcompiler_verboser`�debugrYZ	embeddingrLZ
ext_c_fileZext�updatedrO�cwd�msg�outputfilenamerbrrr�	recompile�sb
















rncOs�ddlm}ddl}|jdt|��t|||f|�|�}|j||�}xFt|j�D]8}	|	j	d�sPt
|j|	�}
|
t
||	t��k	rPt||	|
�qPWdd�}||_
x6t|�D]*}	|	j	d�r�t|j|	�r�t||	t�q�W|jS)Nr)�udirrhrbc_st�dS)N)r)rJrYrrr�typeof_disabledsz _verify.<locals>.typeof_disabled)Ztesting.udirro�imp�
setdefaultrGrnZload_dynamic�dirr:r�rR�objectrNZ_typeof�hasattr�NotImplemented�lib)r:r;r�rJrYrorqrm�moduler�attrrprrr�_verifys 
rz)r4)F)F)r�TNrcNrNN) r�r\�iorrr�errorrZcffi_opcoder=r�r�rrr%r1r8r9�version_info�StringIOr5�BytesIOrFrGrHrMrRrTr^rbrnrzrrrr�<module>sHf



<

?>