AlkantarClanX12

Your IP : 18.227.52.248


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.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_versionc	CsPi|_|jd�t|jtd�}g|_x�|D]�}|jr*|j|dksFt�t|j�|j|<|jj|�xZ|j	D]P}t
|tjtj
tjtjtjf�s�t�|j|dkr�t|j�|j|<|jj|�qjW|jjd�q*WxZ|D]R}|jo�|j|dkr�t|j�|j|<|jj|�|jr�|jdk	r�|jjd�q�Wd|jj�k�s>t�i|_i|_x@|D]8}t
|tj��rnd|j|<nt
|tj��rPd|j|<�qPWx.tt|jdd�d��D]\}}||j|<�q�Wx.tt|jdd�d��D]\}}||j|<�q�Wx0|D](}t|d|jj�}|||j|��q�Wx |jD]}t
|t��s&t��q&Wt|j�|_dS)	NZcollecttype)�keyZEND�LENcSs|jS)N)r)�tprrr�<lambda>�sz/Recompiler.collect_type_table.<locals>.<lambda>cSs|jS)N)r)rCrrrrD�sZ_emit_bytecode_)�
_typesdict�	_generate�sorted�str�
cffi_typesZis_raw_function�AssertionErrorr�append�args�
isinstancer�VoidType�BasePrimitiveType�PointerType�StructOrUnionOrEnum�FunctionPtrTypeZ
is_array_type�length�values�_struct_unions�_enums�
StructOrUnion�EnumType�	enumerate�getattr�	__class__r�CffiOp�tuple)r
Z	all_declsrC�tp1�i�methodr rrr�collect_type_table�sZ






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)rMrZBaseTypeByIdentityr]�_do_collect_typerErR�as_raw_functionrW�fldtypesr:�_parser�_included_declarations�
enumfields�_field_typeZ
_get_items)r
rC�xZname1r^�_rrrrb�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:reZ
_declarations�itemsrG�splitrZ�AttributeErrorr�_current_quals�	ExceptionrZattach_exception_info)
r
�	step_name�lstrrC�qualsZkindZrealnamer`�errrrF�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],\}}|t
|�ks�t�||j|jks�t�q�Wt
|�t
|j�ks�t�|jd}x<|j
j	�D].\}}|t
|�k�st�||j|jks�t�q�Wt
|�t
|j
�k�s6t�dS)NZctxrucSs|jS)N)r)�entryrrrrD�sz0Recompiler.collect_step_tables.<locals>.<lambda>)rArvrw)�_lsts�	ALL_STEPS�set�_seen_struct_unionsrF�_add_missing_struct_unions�sortr]rUrkrrJrrV)r
rprqrCr_rrr�collect_step_tables�s*



zRecompiler.collect_step_tablesrcCs|jj|d�dS)N�
)�_f�write)r
Zwhatrrr�_prntszRecompiler._prntcCs:|jr|dkst�|j|�n|dk	s*t�|j||�dS)N)r<rJ�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,rCr_rrrr-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 */ruz  %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���)r�r�r:�
_embeddingr��indexr0r;rl�_print_string_literal_in_arrayr@�VERSION_EMBEDDED�dictrErkrYrI�_get_c_namerr|�_seen_constantsrFr{rzr�_included_ffis�_assigned_sourcermr�
_num_externpyr>)r
r�r��prntr�r_Zbase_module_name�typeindex2typer r)Znumsrprqry�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.)rMrH�listr]rrKr0r)r
riZrepr)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,rCr_rrrr-�srruz    _%ss = %s,z    _includes = (%s,),z, cSsg|]}d|�qS)z_ffi%dr)r,r_rrrr-�s�))r�r�rr:r��ranger�rmrr;r>r]rIr�r0r�rErkr{rz)r
r�r�Znum_includesr_r�r�r�Z	types_lstr�rprqrrrr��sB

zRecompiler.write_py_source_to_fcCs
|j|S)N)rE)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_%srrj�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�)rMrrO�is_complex_type�is_integer_typer�UnknownFloatType�
get_c_name�replacer@�VERSION_CHAR16CHAR32rP�"_convert_funcarg_to_c_ptr_or_arrayrQr�r�rRr)	r
rC�fromvar�tovar�errcodeZextraargZ	converter�cnameZerrvaluerrr�_convert_funcarg_to_cs@

z Recompiler._convert_funcarg_to_ccCst|tj�r|jd�dS)NzPy_ssize_t datasize)rMrrP�add)r
rC�	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
rCr�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 doublerrjr�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�)rMrrOr�rr�r�r�r@r�r�rPrR�	ArrayTyper�rWZfldnames�	TypeErrorr�rXr)r
rC�var�contextr�rrr�_convert_expr_from_cGs8

zRecompiler._convert_expr_from_ccCs|j|d|f�S)Nz
(*(%s *)0))�_global_type)r
rCrrrr�
_typedef_typejszRecompiler._typedef_typecCs|j|j||��dS)N)rbr�)r
rCrrrr�!_generate_cpy_typedef_collecttypemsz,Recompiler._generate_cpy_typedef_collecttypecCsdS)Nr)r
rCrrrr�_generate_cpy_typedef_declpsz%Recompiler._generate_cpy_typedef_declcCs$|j|}|jdjt||��dS)Nrx)rErzrKr8)r
rCrr&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�rZ�_struct_ctxrrMrZNamedPointerType�totype)r
rCrrrr�_generate_cpy_typedef_ctxwsz$Recompiler._generate_cpy_typedef_ctxcCs*|j|j��|jr&|jr&|j|�dS)N)rbrc�ellipsisr<)r
rCrrrr�"_generate_cpy_function_collecttype�sz-Recompiler._generate_cpy_function_collecttypecCs�|jst�t|tj�st�|jr2|j||�dS|j}t|j	�}|dkrPd}n|dkr^d}nd}g}g}d|}x:t
|j	�D],\}	}
|j|
jd|	|��|jd|	�q~Wd	j
|�}|p�d
}|jr�|jd}nd}d
|||f}
|d|jj|
�f�|d�d	j
|�}d}t|jtj��r$d}|d|||f�|d�|d�|d�|d||f�|d�d|}x4t
|j	�D]&\}	}
|
jd|	|�}|d|��qzWt�}x|j	D]}
|j|
|��q�Wx|D]}|d|f��q�Wt|jtj��sd}d|}d|jjd|�}||�nd}d}t|j	�dk�r�tt|j	��}x|D]}	|d|	��qJW|�|d|t|�t|�d	j
dd�|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�rL|d(�|�rl|d)|j|jd*d+��n|d,�|d-�|d�|d.�d/d0�}d1}g}g}d|}x^t
|j	�D]P\}	}
d}||
��r�d2}d3}|
jd4||	f|�}|j|�|jd5||	f��q�W|j}||��rJd|}|jd6|�}|jd|�tj}d}d7}d3}|�r�d	j
|�}|�pbd
}d8|||f}
|d|j|
�f�|d�|�r�||�d	j
|�}|d$|||f�|�r�|d9�|d�n|d:||f�|d;�|�dS)<NrZnoargrZarg0rLzargument 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,r_rrrr-�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,r_rrrr-�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)rMrrW�
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<rJrMrrRr��_generate_cpy_constant_declr�rrLrYrKr�r0�abir�rNr|r�r�r�r��insertZ	void_type)r
rCrr��numargsZargname�	argumentsZcall_argumentsr�r_r��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)Nrrrtz
_cffi_f_%sz
_cffi_d_%s)r)r�r<�_generate_cpy_constant_ctxrErcrrLZOP_DLOPEN_FUNCZOP_CPYTHON_BLTN_NZOP_CPYTHON_BLTN_OZOP_CPYTHON_BLTN_VrzrKrr\)r
rCrr&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])rMrr�rSr�rhr�)r
Z	tp_structZ
field_nameZtp_field�
actual_lengthZptr_struct_name�tp_itemrrrrh9s

zRecompiler._field_typecCs0|j|�|jr,x|j�D]}|j|�qWdS)N)rbr<�anonymous_struct_fields�_struct_collecttype)r
rC�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)rrz
  /* %s */r�z(struct _cffi_align_%s { char x; %s y; };)rdr�rgr�rMrr�rSr�r�rrH)r
rCr�r�r�Z
checkfuncnameZfnameZftyper�fqualrsrrr�_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)rruz
(size_t)-2r4Zunnamedzsizeof(*(%s)0)z-1 /* unknown alignment */z
sizeof(%s)z"offsetof(struct _cffi_align_%s, y)rrv���r�r�)!rErMrZ	UnionTyperKrdr:rerf�partial�anyr�Zpackedr0r<r�rgrh�_check_not_opaquerr!r"r�rSr�rr\rrz�extendr%r}r�)r
rCr�r�r�r&Zreason_for_not_expandingr'r+Zexpand_anonymous_struct_unionrgZfldnamer�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()))rMrr�r�rWrdr�)r
rC�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_orderrrrrD�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�rUrkrr}r�rr�
startswith�isdigitZ	forcenamer�r�)r
rqrC�orderr�rrrr~�s


z%Recompiler._add_missing_struct_unionscCs|j|�dS)N)r�)r
rCrrrr� _generate_cpy_struct_collecttype�sz+Recompiler._generate_cpy_struct_collecttypecCs2|jd�}d|kr"||jdd�fS|d|fSdS)Nrrrj)r�r�)r
rCr�rrr�
_struct_names�s
zRecompiler._struct_namescCs|j|f|j|���dS)N)r�r�)r
rCrrrr�_generate_cpy_struct_decl�sz$Recompiler._generate_cpy_struct_declcCs|j|f|j|���dS)N)r�r�)r
rCrrrr�_generate_cpy_struct_ctx�sz#Recompiler._generate_cpy_struct_ctxcCs(t|tj�r|j||�n
|j|�dS)N)rMrrX�_generate_cpy_enum_collecttyper�)r
rCrrrr�#_generate_cpy_anonymous_collecttype�sz.Recompiler._generate_cpy_anonymous_collecttypecCs.t|tj�r|j|�n|j||d|�dS)N�typedef_)rMrrX�_generate_cpy_enum_declr�)r
rCrrrr�_generate_cpy_anonymous_decl�sz'Recompiler._generate_cpy_anonymous_declcCs0t|tj�r|j||�n|j||d|�dS)Nr)rMrrX�	_enum_ctxr�)r
rCrrrr�_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�n>|dks�t�|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�rJr�)r
�is_intrrC�categoryrr��funcnamerrr�_generate_cpy_consts2

zRecompiler._generate_cpy_constcCs"|j�}|s|jr|j|�dS)N)r�r<rb)r
rCrrrrr�"_generate_cpy_constant_collecttype&sz-Recompiler._generate_cpy_constant_collecttypecCs|j�}|j|||�dS)N)r�r
)r
rCrrrrrr�+sz&Recompiler._generate_cpy_constant_declcCs`|jr|j�rttd�}n$|jr(t}nt}|j|}t||�}|jdjt	|d||��dS)Nrrtz_cffi_const_%sr�)
r<r�r\�OP_CONSTANT_INTZOP_DLOPEN_CONSTZOP_CONSTANTrErzrKr)r
rCrr
Z
const_kindr&rrrr�/s


z%Recompiler._generate_cpy_constant_ctxcCs|j|�dS)N)rb)r
rCrrrrr�?sz)Recompiler._generate_cpy_enum_collecttypecCs x|jD]}|jd|�qWdS)NT)�enumeratorsr
)r
rCr�
enumeratorrrrrBs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)
Nrrtz_cffi_const_%s)rr�z
sizeof(%s)z
((%s)-1) <= 0rr.rwr�r�)rEr\�OP_ENUMr<Zcheck_not_partial�zipr
Z
enumvaluesrzrKrZbuild_baseinttyper:Zsizeof�int�castr0r1r)r
rCr�r&r
rZ	enumvaluerr2Zbasetpr3rrrrFs"




zRecompiler._enum_ctxcCs|j||j��dS)N)rr�)r
rCrrrr�_generate_cpy_enum_ctx[sz!Recompiler._generate_cpy_enum_ctxcCsdS)Nr)r
rCrrrr�_generate_cpy_macro_collecttypeasz*Recompiler._generate_cpy_macro_collecttypecCs&|dkrd}n|}|jd||d�dS)Nz...T)r)r
)r
rCrr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 moderrtz_cffi_const_%s)rr�)r<rr\rrzrKr)r
rCrr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])rMrr�rSr�r�)r
rCZglobal_namer�r�rrrr�|s
zRecompiler._global_typecCs|j|j||��dS)N)rbr�)r
rCrrrr�"_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 )rrr�z  return %s(%s);r�)	r�r�rMrr�rSr�r�rn)r
rCrr�Z	ampersandr�rrr�_generate_cpy_variable_decl�s	
z&Recompiler._generate_cpy_variable_declcCsL|j||�}|j|}|jr"t}nt}|jdjt|d|t||���dS)Nrtz_cffi_var_%s)	r�rEr<Z
OP_GLOBAL_VARZOP_GLOBAL_VAR_FrzrKrr\)r
rCrr&r rrr�_generate_cpy_variable_ctx�s

z%Recompiler._generate_cpy_variable_ctxcCst|tj�st�|j|�dS)N)rMrrRrJrb)r
rCrrrr�'_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)rMrr�r)rCrrr�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�rMr�rrNr�r;rYrLrKr0r�r?rrWrPr�)r
rCrZ
tag_and_spacer�Zsize_of_resultr�r�r_r�r�r�r�rZ	size_of_arrr�_extern_python_decl�sX





"zRecompiler._extern_python_declcCs|j||d�dS)Nzstatic )r)r
rCrrrr� _generate_cpy_extern_python_decl�sz+Recompiler._generate_cpy_extern_python_declcCs|j||d�dS)NzCFFI_DLLEXPORT )r)r
rCrrrr�#_generate_cpy_dllexport_python_decl�sz.Recompiler._generate_cpy_dllexport_python_declcCs|j||d�dS)Nr)r)r
rCrrrr�'_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"rtz&_cffi_externpy__%s)
r<rr�rrEr\ZOP_EXTERN_PYTHONrzrKr)r
rCrr&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_VOIDrI)r
rCr�rrr�_emit_bytecode_VoidTypesz"Recompiler._emit_bytecode_VoidTypecCst|j}tt|�|j|<dS)N)ZPRIMITIVE_TO_INDEXrr\r+rI)r
rCr�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+rI)r
rCr�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+rI)r
rCr�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_FUNCTIONrEr�rIrLrMrr�r-r!rr�r�rZOP_FUNCTION_END)r
rCr�r^Z	realindexr'rrr�_emit_bytecode_RawFunctionType#s




z)Recompiler._emit_bytecode_RawFunctionTypecCstt|j|j�|j|<dS)N)r\�
OP_POINTERrEr�rI)r
rCr�rrr�_emit_bytecode_PointerType6sz%Recompiler._emit_bytecode_PointerTypecCs"|j�}tt|j|�|j|<dS)N)rcr\r1rErI)r
rCr��rawrrr�_emit_bytecode_FunctionPtrType<sz)Recompiler._emit_bytecode_FunctionPtrTypecCs�|j|j}|jdkr(tt|�|j|<nf|jdkrNtdt|�jdd�f��n@|j|ddksdt	�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/*...*/rrB)rEr�rSr\Z
OP_OPEN_ARRAYrIrrHr�rJZOP_ARRAY)r
rCr�Z
item_indexrrr�_emit_bytecode_ArrayType@s

z#Recompiler._emit_bytecode_ArrayTypecCs|j|}tt|�|j|<dS)N)rUr\ZOP_STRUCT_UNIONrI)r
rCr�Zstruct_indexrrr�_emit_bytecode_StructTypeNs
z$Recompiler._emit_bytecode_StructTypecCs|j|}tt|�|j|<dS)N)rVr\rrI)r
rCr�Z
enum_indexrrr�_emit_bytecode_EnumTypeSs
z"Recompiler._emit_bytecode_EnumType)F)r)N)NrN)N)Urrrr�rr@rarbrFr{r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rhr�r�r�r�r~r�Z_generate_cpy_union_collecttyper�r�Z_generate_cpy_union_declr�Z_generate_cpy_union_ctxrrrr
rr�r�r�rrrrrrr�rrrrZ*_generate_cpy_dllexport_python_collecttypeZ._generate_cpy_extern_python_plus_c_collecttyperr r!r"r#Z"_generate_cpy_dllexport_python_ctxZ&_generate_cpy_extern_python_plus_c_ctxr�r,r-r.r/r0r2Z_emit_bytecode_ConstPointerTypeZ_emit_bytecode_NamedPointerTyper4r5r6Z_emit_bytecode_UnionTyper7rrrrr9{s�
:
,
4-#!
Q	

	
5
r9�cseZdZ�fdd�Z�ZS)�NativeIOcs(t|t�r|jd�}tt|�j|�dS)N�ascii)rMZunicode�encode�superr9r�)r
r()r[rrr�\s

zNativeIO.write)rrrr��
__classcell__rr)r[rr9[sr9c
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)�printr9rar�r9r��getvaluer��readr�IOErrorr��getpidr��rename�OSError�unlink)
r:r;r�Ztarget_file�verboseZ
recompilerr��output�f1Ztmp_filerrr�_make_c_or_py_sourceas4
rJFcCs|dk	st�t|||||�S)N)rJrJ)r:r;r�Z
target_c_filerGrrr�
make_c_source}s
rKcCst||d||�S)N)rJ)r:r;Ztarget_py_filerGrrr�make_py_source�s
rLcCsj|jd�}y$tjtjj|f|dd����Wntk
rBYnX|d|7<tjj|f|��|fS)Nr�rr�r�)rlr��makedirsr�r0rE)Z	outputdir�modname�	extension�partsrrr�_modname_to_file�s
$rQcCs*t||�}|j|||f�t|||�|S)N)rZrK�setattr)�	patchlist�clsrZnew_meth�oldrrr�_patch_meth�s
rVcCs(x"t|�D]\}}}t|||�q
WdS)N)�reversedrR)rSrTrZold_methrrr�_unpatch_meths�srXcs\tjdkr(ddlm}t||ddd��tjdkrXddlm}�fd	d
�}t||d|��dS)N�win32r)�MSVCCompilerZ_remove_visual_c_refcSs|S)Nr)r
Z
manifest_filerrrrD�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
rL�kwdsr_)�old_link_shared_objectrr�my_link_shared_object�s


z3_patch_for_embedding.<locals>.my_link_shared_objectZlink_shared_object)�sys�platformZdistutils.msvc9compilerrZrVZdistutils.ccompilerr\)rSrZr\r_r)r^r�_patch_for_embedding�s


rbcslddlm}�jd�rR�dd
��tjdkr6�d7�ntjdkrJ�d7�n�d	7�t||d
�fdd��dS)Nr)�	build_extz.*r4rYz.dllr[z.dylibz.soZget_ext_filenamecs�S)Nr)r
Zext_name)�targetrrrD�sz#_patch_for_target.<locals>.<lambda>r�)Zdistutils.command.build_extrc�endswithr`rarV)rSrdrcr)rdr�_patch_for_target�s




rfr�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)
Nr:z%s.*r)rGr�zthe current directory isz setting the current directory toz%s %rz.py)rMrHr;Z_windows_unicodeZ_apply_windows_unicoder�Z_apply_embedding_fixrQr�r�r0rZ
get_extensionrK�getcwdrbrfr?�abspath�chdir�compilerXrL)r:r;r��tmpdirZcall_c_compilerZc_fileZsource_extensionZextradirZcompiler_verboserd�debugr]Z	embeddingrPZ
ext_c_fileZext�updatedrS�cwd�msg�outputfilenamerjrrr�	recompile�sb
















rrcOs�ddlm}ddl}|tjks,td|f��|jdt|��t|||f|�|�}|j	||�}xFt
|j�D]8}	|	jd�sht
|j|	�}
|
t
||	t��k	rht||	|
�qhWdd�}||_x6t
|�D]*}	|	jd�r�t|j|	�r�t||	t�q�W|jS)Nr)�udirzmodule name conflict: %rrlrjc_st�dS)N)r)rLr]rrr�typeof_disabledsz _verify.<locals>.typeof_disabled)Ztesting.udirrs�impr`�modulesrJ�
setdefaultrHrrZload_dynamic�dirr:r�rZ�objectrRZ_typeof�hasattr�NotImplemented�lib)r:r;r�rLr]rsrurq�moduler�attrrtrrr�_verifys$

r)r8)F)F)r�TNrgNrNN) r�r`�iorrr�errorrZcffi_opcoder=r�r�rrr%r1r8r9�version_info�StringIOr9�BytesIOrJrKrLrQrVrXrbrfrrrrrrr�<module>sHf



<