o
    àý°j¨  ã                   @   sB   d dl Z ddlmZ ddlmZ ddlmZ G dd„ dƒZdZdS )	é    Né   )Úmodel©ÚVerificationError)Ú_imp_emulationc                   @   s  e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdndd„Z	dd„ Z
dd„ Zdd„ Zdod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(„ ZeZeZeZeZeZd)d*„ Zd+d,„ Zd-d.„ ZeZd/d0„ Z eZ!d1d2„ Z"d3d4„ Z#d5d6„ Z$d7d8„ Z%eZ&d9d:„ Z'd;d<„ Z(d=d>„ Z)d?d@„ Z*dAdB„ Z+dCdD„ Z,dEdF„ Z-dGdH„ Z.eZ/dIdJ„ Z0dKdL„ Z1dMdN„ Z2dOdP„ Z3	Q	R	dpdSdT„Z4dUdV„ Z5dWdX„ Z6eZ7eZ8eZ9dndYdZ„Z:d[d\„ Z;dqd^d_„Z<eZ=eZ>d`da„ Z?dbdc„ Z@ddde„ ZAeZBeZCeZDeZEdfdg„ ZFdhdi„ ZGeZHeZIdjdk„ ZJdldm„ ZKdS )rÚVCPythonEngineÚxTc                 C   s   || _ |j| _i | _i | _d S ©N)ÚverifierÚffiÚ_struct_pending_verificationÚ_types_of_builtin_functions)Úselfr
   © r   úƒ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/cffi/vengine_cpy.pyÚ__init__   s   
zVCPythonEngine.__init__c                 C   ó   d S r	   r   )r   Úkwdsr   r   r   Úpatch_extension_kwds   ó   z#VCPythonEngine.patch_extension_kwdsc                 C   sP   zt  ||¡\}}}W n
 ty   Y d S w |d ur| ¡  |d |vr&d S |S ©Nr   )ÚimpÚfind_moduleÚImportErrorÚclose)r   Úmodule_nameÚpathÚso_suffixesÚfÚfilenameÚdescrr   r   r   r      s   ÿzVCPythonEngine.find_modulec                 C   s   i | _ |  d¡ d S )NÚcollecttype)Ú
_typesdictÚ	_generate©r   r   r   r   Úcollect_types%   s   zVCPythonEngine.collect_typesÚ c                 C   s   | j  |d ¡ d S )NÚ
)Ú_fÚwrite)r   Úwhatr   r   r   Ú_prnt)   ó   zVCPythonEngine._prntc                 C   s
   | j | S r	   )r"   )r   Útyper   r   r   Ú_gettypenum,   s   
zVCPythonEngine._gettypenumc                 C   s@   t |tjƒr|jdkr|| jvrt| jƒ}|| j|< d S d S d S )Núlong double)Ú
isinstancer   ÚPrimitiveTypeÚnamer"   Úlen)r   ÚtpÚnumr   r   r   Ú_do_collect_type0   s   


þÿzVCPythonEngine._do_collect_typec                 C   sr  |   ¡  ddg| _| j}|tƒ |ƒ  || jjƒ |ƒ  |  d¡ |  ¡  |ƒ  |dƒ |  d¡ |dƒ |dƒ |dƒ |ƒ  | j ¡ }| jd }|d	ƒ |d
ƒ |d| ƒ |dƒ |dƒ |dƒ |dƒ |dƒ |ƒ  |dƒ |d| ƒ |dƒ |dƒ |dƒ |dƒ |dƒ |d|f ƒ |dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |ƒ  d S )Nz((void)lib,0)Údeclz&static PyMethodDef _cffi_methods[] = {Úmethodz3  {"_cffi_setup", _cffi_setup, METH_VARARGS, NULL},z)  {NULL, NULL, 0, NULL}    /* Sentinel */z};Fz.static struct PyModuleDef _cffi_module_def = {z  PyModuleDef_HEAD_INIT,z  "%s",z  NULL,z  -1,z  _cffi_methods,z  NULL, NULL, NULL, NULLÚPyMODINIT_FUNCzPyInit_%s(void)Ú{z  PyObject *lib;z+  lib = PyModule_Create(&_cffi_module_def);z  if (lib == NULL)ú    return NULL;z#  if (%s < 0 || _cffi_init() < 0) {z    Py_DECREF(lib);ú  }z#if Py_GIL_DISABLEDz5  PyUnstable_Module_SetGIL(lib, Py_MOD_GIL_NOT_USED);z#endifz  return lib;Ú})	r%   Ú_chained_list_constantsr+   Úcffimod_headerr
   Úpreambler#   Ú_generate_setup_customÚget_module_name)r   ÚprntÚmodnameÚ	constantsr   r   r   Úwrite_source_to_f7   sX   





z VCPythonEngine.write_source_to_fNc              
      s†  t  ¡  z\ttdƒrt ¡ }z@zttdƒr|d urt |¡ t  ˆj ¡ ˆjj	¡‰W n t
yB } zdˆjj	|f }t|ƒ‚d }~ww W ttdƒrNt |¡ nttdƒrZt |¡ w w W t  ¡  nt  ¡  w ˆ ˆd¡ tdd„ ˆj ¡ D ƒƒ‰‡fdd„ttˆƒƒD ƒ}ttˆjj|ƒƒ}G ‡ ‡‡fdd	„d	ƒ‰ ˆ ƒ }ˆ |t|¡r±d
d l}| dˆj ¡  ¡ ˆjˆd|d� ˆjˆ_ˆjˆ_|S )NÚgetdlopenflagsÚsetdlopenflagszimporting %r: %sÚloadingc                 S   s   g | ]\}}||f‘qS r   r   )Ú.0ÚkeyÚvaluer   r   r   Ú
<listcomp>Ÿ   s    ÿz/VCPythonEngine.load_library.<locals>.<listcomp>c                    s   g | ]}ˆ | ‘qS r   r   ©rJ   Úi)Ú
revmappingr   r   rM   ¡   ó    c                       s&   e Zd Z”Z”jZg Z‡ fdd„ZdS )z/VCPythonEngine.load_library.<locals>.FFILibraryc                    s   ˆ j t| jƒ S r	   )Ú	_cffi_dirÚlistÚ__dict__r$   )Ú
FFILibraryr   r   Ú__dir__­   s   z7VCPythonEngine.load_library.<locals>.FFILibrary.__dir__N)Ú__name__Ú
__module__Ú__qualname__Ú_cffi_python_moduler   Ú	_cffi_ffirR   rV   r   )rU   Úmoduler   r   r   rU   ©   s
    rU   r   z0reimporting %r might overwrite older definitionsÚloaded©Úlibrary)r   Úacquire_lockÚhasattrÚsysrG   rH   Úload_dynamicr
   rB   Úmodulefilenamer   r   Úrelease_lockÚ_loadÚdictr"   ÚitemsÚranger3   rS   Úmapr   Ú_get_cached_btypeÚ_cffi_setupÚwarningsÚwarnÚ_cffi_original_ffir   Ú_cffi_types_of_builtin_funcs)r   ÚflagsÚprevious_flagsÚeÚerrorÚlstr_   rm   r   )rU   r\   rP   r   r   Úload_library„   sR   

ÿ€þþ

€
ÿÿ€ÿÿzVCPythonEngine.load_libraryc                 C   s$   dd„ | j jj ¡ D ƒ}| ¡  |S )Nc                 S   s   g | ]
\}\}}||f‘qS r   r   )rJ   rK   r4   Úqualr   r   r   rM   ¿   s    z4VCPythonEngine._get_declarations.<locals>.<listcomp>)r   Ú_parserÚ_declarationsrh   Úsort)r   ru   r   r   r   Ú_get_declarations¾   s
   ÿz VCPythonEngine._get_declarationsc                 C   sŒ   |   ¡ D ]?\}}| dd¡\}}zt| d||f ƒ}W n ty(   td| ƒ‚w z|||ƒ W q tyC } zt ||¡ ‚ d }~ww d S )Nú r   z_generate_cpy_%s_%sznot implemented in verify(): %r)r{   ÚsplitÚgetattrÚAttributeErrorr   Ú	Exceptionr   Úattach_exception_info)r   Ú	step_namer2   r4   ÚkindÚrealnamer8   rs   r   r   r   r#   Ä   s&   ÿÿÿ€þözVCPythonEngine._generatec           
      K   sv   |   ¡ D ]4\}}| dd¡\}}t| d||f ƒ}z||||fi |¤Ž W q ty8 }	 zt |	|¡ ‚ d }	~	ww d S )Nr|   r   z
_%s_cpy_%s)r{   r}   r~   r€   r   r�   )
r   r\   r‚   r   r2   r4   rƒ   r„   r8   rs   r   r   r   rf   Ó   s   €þûzVCPythonEngine._loadc                 C   r   r	   r   ©r   r4   r2   r   r   r   Ú_generate_nothingÝ   r   z VCPythonEngine._generate_nothingc                 K   r   r	   r   ©r   r4   r2   r\   r   r   r   r   Ú_loaded_noopà   r   zVCPythonEngine._loaded_noopc                 C   s:  d}t |tjƒr2| ¡ r|jdkrd}d|j }n| ¡ r!tdƒ‚d| d¡|j dd¡f }d	}nJt |tj	ƒrB|  
||||¡ d S t |tjtjfƒra|  d
||  |¡|f ¡ |  d| ¡ d S t |tjƒrxd| d¡ }d|  |¡ }d}nt|ƒ‚|  d||||f ¡ |  d|| d¡|f ¡ |  d| ¡ d S )Nr&   Ú_BoolÚ_cffi_to_c_intz, %sz*not implemented in verify(): complex typesz(%s)_cffi_to_c_%sr|   Ú_z-1z6  if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)z    %s;z(%s)_cffi_to_c_pointerz, _cffi_type(%d)ÚNULLz  %s = %s(%s%s);z'  if (%s == (%s)%s && PyErr_Occurred()))r0   r   r1   Úis_integer_typer2   Úis_complex_typer   Ú
get_c_nameÚreplaceÚPointerTypeÚ"_convert_funcarg_to_c_ptr_or_arrayÚStructOrUnionÚEnumTyper+   r.   ÚFunctionPtrTypeÚNotImplementedError)r   r4   ÚfromvarÚtovarÚerrcodeÚextraargÚ	converterÚerrvaluer   r   r   Ú_convert_funcarg_to_cå   sD   ÿ
ÿÿÿÿz$VCPythonEngine._convert_funcarg_to_cc                 C   s2   t |tjƒr| d¡ | d¡ | d¡ d S d S )NzPy_ssize_t datasizez-struct _cffi_freeme_s *large_args_free = NULLzIif (large_args_free != NULL) _cffi_free_array_arguments(large_args_free);)r0   r   r‘   Úadd)r   r4   Ú	localvarsÚ	freelinesr   r   r   Ú_extra_local_variables  s
   

ýz%VCPythonEngine._extra_local_variablesc                 C   s~   |   d¡ |   d|  |¡||f ¡ |   d¡ |   d|f ¡ |   d|  |¡||f ¡ |   d¡ |   d| ¡ |   d¡ d S )	Nz1  datasize = _cffi_prepare_pointer_call_argument(z(      _cffi_type(%d), %s, (char **)&%s);z  if (datasize != 0) {zE    %s = ((size_t)datasize) <= 640 ? alloca((size_t)datasize) : NULL;zF    if (_cffi_convert_array_argument(_cffi_type(%d), %s, (char **)&%s,z,            datasize, &large_args_free) < 0)z	      %s;r<   )r+   r.   )r   r4   r—   r˜   r™   r   r   r   r’     s   
ÿ
ÿÿ
z1VCPythonEngine._convert_funcarg_to_c_ptr_or_arrayc                 C   s
  t |tjƒr/| ¡ r|jdkrd||jf S |jdkr&d|j dd¡|f S d||  |¡f S t |tjtjfƒrAd||  |¡f S t |tj	ƒrTd||  t |j
¡¡f S t |tjƒrr|jd u ritd	| ¡ |f ƒ‚d
||  |¡f S t |tjƒr�d||  |¡f S t|ƒ‚)Nr‰   z_cffi_from_c_int(%s, %s)r/   z_cffi_from_c_%s(%s)r|   r‹   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)))r0   r   r1   r�   r2   r�   r.   r‘   r•   Ú	ArrayTypeÚitemr“   ÚfldnamesÚ	TypeErrorÚ_get_c_namer”   r–   )r   r4   ÚvarÚcontextr   r   r   Ú_convert_expr_from_c   s:   

ÿ
ÿÿ
ÿ
ÿ
ÿz#VCPythonEngine._convert_expr_from_cc                 C   sJ   t |tjƒsJ ‚|jr|  |¡ d S |jD ]}|  |¡ q|  |j¡ d S r	   )r0   r   r•   Úellipsisr6   ÚargsÚresult)r   r4   r2   r-   r   r   r   Ú"_generate_cpy_function_collecttypeG  s   
z1VCPythonEngine._generate_cpy_function_collecttypec              
   C   s°  t |tjƒsJ ‚|jr|  d||¡ d S | j}t|jƒ}|dkr#d}n	|dkr*d}nd}|dƒ |d||f ƒ |d	ƒ d
| }t|jƒD ]\}}|d| 	d| |¡ ƒ qEt
ƒ }	t
ƒ }
|jD ]	}|  ||	|
¡ q_t|	ƒD ]	}|d|f ƒ qmt |jtjƒs”d}d| }|d|j 	d|¡ ƒ |dƒ nd}t|jƒdkrÉtt|jƒƒ}|D ]}|d| ƒ q¦|ƒ  |dd| |d dd„ |D ƒ¡f ƒ |dƒ |ƒ  t|jƒD ]\}}|  |d| d| d¡ |ƒ  qÑ|dƒ |dƒ |d||d dd„ tt|jƒƒD ƒ¡f ƒ |d ƒ |d!ƒ |ƒ  |d"ƒ |dk�r|d#ƒ |�r;|d$|  |jd%d&¡ ƒ |
D ]	}|d'| ƒ �q,|d(ƒ n|
D ]	}|d'| ƒ �q=|d)ƒ |d*ƒ |d+ƒ |ƒ  d S ),NFr   Únoargr   Úarg0r«   ústatic PyObject *z(_cffi_f_%s(PyObject *self, PyObject *%s)r:   zargument of %sú  %s;z x%dz	result = zresult of %sz resultz  PyObject *pyresult;r&   z  PyObject *arg%d;z+  if (!PyArg_ParseTuple(args, "%s:%s", %s))ÚOz, c                 S   ó   g | ]}d | ‘qS )z&arg%dr   rN   r   r   r   rM     rQ   z>VCPythonEngine._generate_cpy_function_decl.<locals>.<listcomp>r;   zarg%dúx%dzreturn NULLz  Py_BEGIN_ALLOW_THREADSz  _cffi_restore_errno();z  { %s%s(%s); }c                 S   r³   )r´   r   rN   r   r   r   rM   Œ  rQ   z  _cffi_save_errno();z  Py_END_ALLOW_THREADSú  (void)self; /* unused */ú  (void)noarg; /* unused */z  pyresult = %s;r¬   zresult typez  z  return pyresult;z  Py_INCREF(Py_None);z  return Py_None;r=   )r0   r   r•   rª   Ú_generate_cpy_constr+   r3   r«   Ú	enumerater�   Úsetr¡   Úsortedr¬   ÚVoidTyperi   Újoinr�   r©   )r   r4   r2   rC   ÚnumargsÚargnamer¨   rO   r-   rŸ   r    r7   Úresult_codeÚrngÚfreeliner   r   r   Ú_generate_cpy_function_declR  sŠ   


ÿÿþ
ÿ

z*VCPythonEngine._generate_cpy_function_declc                 C   sL   |j rd S t|jƒ}|dkrd}n	|dkrd}nd}|  d|||f ¡ d S )Nr   ÚMETH_NOARGSr   ÚMETH_OÚMETH_VARARGSz  {"%s", _cffi_f_%s, %s, NULL},)rª   r3   r«   r+   )r   r4   r2   r½   Úmethr   r   r   Ú_generate_cpy_function_method¢  s   
z,VCPythonEngine._generate_cpy_function_methodc                 C   s.   |j rd S t||ƒ}t|||ƒ || j|< d S r	   )rª   r~   Úsetattrr   )r   r4   r2   r\   r_   Úfuncr   r   r   Ú_loaded_cpy_function°  s
   
z#VCPythonEngine._loaded_cpy_functionc                 C   ó    ||j ksJ ‚|  |d|¡ d S ©NÚstruct©r2   Ú_generate_struct_or_union_declr…   r   r   r   Ú_generate_cpy_struct_decl»  ó   z(VCPythonEngine._generate_cpy_struct_declc                 C   ó   |   |d|¡ d S rÌ   ©Ú _generate_struct_or_union_methodr…   r   r   r   Ú_generate_cpy_struct_method¾  ó   z*VCPythonEngine._generate_cpy_struct_methodc                 C   ó   |   |d||¡ d S rÌ   ©Ú_loading_struct_or_union©r   r4   r2   r\   r   r   r   Ú_loading_cpy_structÀ  r,   z"VCPythonEngine._loading_cpy_structc                 K   ó   |   |¡ d S r	   ©Ú_loaded_struct_or_unionr‡   r   r   r   Ú_loaded_cpy_structÂ  ó   z!VCPythonEngine._loaded_cpy_structc                 C   rË   ©NÚunionrÎ   r…   r   r   r   Ú_generate_cpy_union_declÆ  rÑ   z'VCPythonEngine._generate_cpy_union_declc                 C   rÒ   rá   rÓ   r…   r   r   r   Ú_generate_cpy_union_methodÉ  rÖ   z)VCPythonEngine._generate_cpy_union_methodc                 C   r×   rá   rØ   rÚ   r   r   r   Ú_loading_cpy_unionË  r,   z!VCPythonEngine._loading_cpy_unionc                 K   rÜ   r	   rÝ   r‡   r   r   r   Ú_loaded_cpy_unionÍ  rà   z VCPythonEngine._loaded_cpy_unionc                 C   s  |j d u rd S d||f }d||f }d||f  ¡ }| j}|d||f ƒ |dƒ |dƒ |dƒ | ¡ D ]G\}}	}
}t|	tjƒrF|	 ¡ sJ|
dkrQ|d	| ƒ q6z|d
|	jdd| |d�|f ƒ W q6 t	y} } z|dt
|ƒ ƒ W Y d }~q6d }~ww |dƒ |dƒ |d|f ƒ |dƒ |d| ƒ |dƒ |d| ƒ |dƒ | ¡ D ]/\}}	}
}|
dkr´q©|d||f ƒ t|	tjƒrÐ|	jd u rÐ|d|	 ¡  ƒ q©|d||f ƒ q©|dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |d|f ƒ |dƒ |ƒ  d S ) Nz_cffi_check_%s_%sú_cffi_layout_%s_%sú%s %szstatic void %s(%s *p)r:   z8  /* only to generate compile-time warnings or errors */z
  (void)p;r   z  (void)((p->%s) << 1);z  { %s = &p->%s; (void)tmp; }z*tmpzfield %r)Úqualsz
  /* %s */r=   r°   z#%s(PyObject *self, PyObject *noarg)z,  struct _cffi_aligncheck { char x; %s y; };z  static Py_ssize_t nums[] = {z    sizeof(%s),z)    offsetof(struct _cffi_aligncheck, y),z    offsetof(%s, %s),z    0,  /* %s */z    sizeof(((%s *)0)->%s),z    -1z  };rµ   r¶   z'  return _cffi_get_struct_layout(nums);z3  /* the next line is not executed, but compiled */z  %s(0);)r¤   Ústripr+   Ú
enumfieldsr0   r   r1   r�   r�   r   Ústrr¢   Úlengthr¦   )r   r4   Úprefixr2   ÚcheckfuncnameÚlayoutfuncnameÚcnamerC   ÚfnameÚftypeÚfbitsizeÚfqualrs   r   r   r   rÏ   Ð  sb   
ÿþ€ÿ
z-VCPythonEngine._generate_struct_or_union_declc                 C   s0   |j d u rd S d||f }|  d||f ¡ d S )Nrç   z   {"%s", %s, METH_NOARGS, NULL},)r¤   r+   )r   r4   rî   r2   rð   r   r   r   rÔ     s   
ÿz/VCPythonEngine._generate_struct_or_union_methodc                 C   sÎ   |j d u rd S d||f }t||ƒ}|ƒ }t|tjƒrV|jrV|d }|d }	|dd d… }
|dd d… }| ¡  t|
ƒt|ƒ  krJt|j ƒksMJ ‚ J ‚|
|||	f|_d S d||f  	¡ }||f| j
|< d S )Nrç   r   r   é   é   rè   )r¤   r~   r0   r   r“   ÚpartialÚforce_flattenr3   Úfixedlayoutrê   r   )r   r4   rî   r2   r\   rð   ÚfunctionÚlayoutÚ	totalsizeÚtotalalignmentÚfieldofsÚ	fieldsizerñ   r   r   r   rÙ   
  s   

*z'VCPythonEngine._loading_struct_or_unionc                 C   s  |j d u rd S | j |¡ || jv r†dd„ }| j}| |¡}| j |¡\}}||d | |¡dƒ ||d | |¡dƒ d}| ¡ D ];\}}	}
}|
dkrMqB||| | ||¡d|f ƒ ||d  dkry| |	¡}|||d  | |¡d	|f ƒ |d7 }qB|t	|ƒksˆJ ‚d S d S )
Nc                 S   s   | |krt d||| f ƒ‚d S )Nz'%s (we have %d, but C compiler says %d)r   )Ú	realvalueÚexpectedvalueÚmsgr   r   r   Úcheck&  s   ÿÿÿz5VCPythonEngine._loaded_struct_or_union.<locals>.checkr   zwrong total sizer   zwrong total alignmentrö   zwrong offset for field %rzwrong size for field %r)
r¤   r   rk   r   ÚpopÚsizeofÚalignofrë   Úoffsetofr3   )r   r4   r  r   ÚBStructrü   rñ   rO   rò   ró   rô   rõ   ÚBFieldr   r   r   rÞ     s4   


ÿ
ÿ
éz&VCPythonEngine._loaded_struct_or_unionc                 C   s0   t |tjƒr|  ||d¡ d S |  |d|¡ d S ©Nr&   )r0   r   r”   Ú_generate_cpy_enum_declrÏ   r…   r   r   r   Ú_generate_cpy_anonymous_declC  s   z+VCPythonEngine._generate_cpy_anonymous_declc                 C   s"   t |tjƒs|  |d|¡ d S d S r  )r0   r   r”   rÔ   r…   r   r   r   Ú_generate_cpy_anonymous_methodI  s   ÿz-VCPythonEngine._generate_cpy_anonymous_methodc                 C   s2   t |tjƒr|  |||¡ d S |  |d||¡ d S r  )r0   r   r”   Ú_loading_cpy_enumrÙ   rÚ   r   r   r   Ú_loading_cpy_anonymousM  s   z%VCPythonEngine._loading_cpy_anonymousc                 K   s4   t |tjƒr| j|||fi |¤Ž d S |  |¡ d S r	   )r0   r   r”   Ú_loaded_cpy_enumrÞ   r‡   r   r   r   Ú_loaded_cpy_anonymousS  s   z$VCPythonEngine._loaded_cpy_anonymousÚconstFc	                 C   sr  | j }	d||f }
|	d|
 ƒ |	dƒ |	dƒ |	dƒ |s*|	d|p"| d|¡ ƒ n|dks0J ‚|d ur:|  ||¡ |s_|d	krEd
| }n|}|	d|f ƒ |	d|  |dd¡f ƒ |s^J ‚n|	d| ƒ |	dƒ |	dƒ |rŽ|	dƒ |	dƒ |	d|f ƒ |	dƒ |	dƒ |	dƒ |	dƒ |	d| ƒ |	dƒ |	dƒ |	dƒ |	d| j|  ƒ |
d | j|< |	dƒ |	ƒ  d S )Nz_cffi_%s_%sústatic int %s(PyObject *lib)r:   z  PyObject *o;z
  int res;r±   z ir  r§   ú&z  i = (%s);z	  o = %s;rO   zvariable typez!  o = _cffi_from_c_int_const(%s);z  if (o == NULL)ú    return -1;z  {z    PyObject *o1 = o;z8    o = Py_BuildValue("On", o1, (Py_ssize_t)sizeof(%s));z    Py_DECREF(o1);z    if (o == NULL)z      return -1;r<   z-  res = PyObject_SetAttrString(lib, "%s", o);z  Py_DECREF(o);z  if (res < 0)ú  return %s;ú(lib)r=   )r+   r�   Ú_check_int_constant_valuer©   r>   )r   Úis_intr2   r4   ÚcategoryÚvartpÚdelayedÚsize_tooÚcheck_valuerC   ÚfuncnameÚrealexprr   r   r   r·   \  sR   

ÿ
ÿ
z"VCPythonEngine._generate_cpy_constc                 C   s*   t |tjƒo	| ¡ }|s|  |¡ d S d S r	   )r0   r   r1   r�   r6   ©r   r4   r2   r  r   r   r   Ú"_generate_cpy_constant_collecttypeŒ  s   ÿz1VCPythonEngine._generate_cpy_constant_collecttypec                 C   s&   t |tjƒo	| ¡ }|  |||¡ d S r	   )r0   r   r1   r�   r·   r"  r   r   r   Ú_generate_cpy_constant_decl‘  s   z*VCPythonEngine._generate_cpy_constant_declc                 C   sž   | j }|dkr|d|||f ƒ n	|d|||f ƒ |dƒ |d| ƒ |d| ƒ |dƒ |d| ƒ |d	ƒ |d
ƒ |d|||f ƒ |dƒ |dƒ d S )Nr   z&  if ((%s) > 0 || (long)(%s) != %dL) {z1  if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {z    char buf[64];z    if ((%s) <= 0)z.        snprintf(buf, 63, "%%ld", (long)(%s));z    elsez7        snprintf(buf, 63, "%%lu", (unsigned long)(%s));z)    PyErr_Format(_cffi_VerificationError,z6                 "%s%s has the real value %s, not %s",z(                 "%s", "%s", buf, "%d");r  r<   )r+   )r   r2   rL   Ú
err_prefixrC   r   r   r   r  œ  s,   
ÿÿÿÿz(VCPythonEngine._check_int_constant_valuec                 C   s   |  dd¡}d||f S )Nú$Ú___D_z_cffi_e_%s_%s)r�   )r   rî   r2   r   r   r   Ú_enum_funcname±  s   zVCPythonEngine._enum_funcnameÚenumc                 C   s¨   |j r|jD ]
}| jd|dd� qd S |  ||¡}| j}|d| ƒ |dƒ t|j|jƒD ]\}}|  ||d| ¡ q-|d| jd  ƒ |d | jd< |d	ƒ |ƒ  d S )
NTF)r  r  r:   z	enum %s: r  r  r=   )	rø   Úenumeratorsr·   r(  r+   ÚzipÚ
enumvaluesr  r>   )r   r4   r2   rî   Ú
enumeratorr   rC   Ú	enumvaluer   r   r   r  ¶  s    
ÿ
z&VCPythonEngine._generate_cpy_enum_declc                    s2   |j r‡ fdd„|jD ƒ}t|ƒ|_d|_d S d S )Nc                    s   g | ]}t ˆ |ƒ‘qS r   )r~   )rJ   r-  ©r\   r   r   rM   Í  s    ÿz4VCPythonEngine._loading_cpy_enum.<locals>.<listcomp>T)rø   r*  Útupler,  Úpartial_resolved)r   r4   r2   r\   r,  r   r/  r   r  Ë  s   
ÿ

üz VCPythonEngine._loading_cpy_enumc                 C   s(   t |j|jƒD ]
\}}t|||ƒ qd S r	   )r+  r*  r,  rÈ   )r   r4   r2   r\   r_   r-  r.  r   r   r   r  Ò  s   ÿzVCPythonEngine._loaded_cpy_enumc                 C   s&   |dkrd }n|}| j d||d� d S )Nz...T)r  )r·   )r   r4   r2   r  r   r   r   Ú_generate_cpy_macro_declÙ  s   z'VCPythonEngine._generate_cpy_macro_declc                 C   s2   t |tjƒrt |j¡}nt |¡}|  |¡ d S r	   )r0   r   r¢   r‘   r£   r6   ©r   r4   r2   Útp_ptrr   r   r   Ú"_generate_cpy_variable_collecttypeè  s   
z1VCPythonEngine._generate_cpy_variable_collecttypec                 C   sT   t |tjƒrt |j¡}| jd|||| ¡ d� d S t |¡}| jd||dd� d S )NF)r  r  r§   )r  )r0   r   r¢   r‘   r£   r·   Úlength_is_unknownr3  r   r   r   Ú_generate_cpy_variable_declï  s   
ÿ
z*VCPythonEngine._generate_cpy_variable_declc                    s   t ||ƒ}t|tjƒrX| ¡ r>t|tƒsJ ‚|\}}| j |j¡}t	|| j 
|¡ƒ\}}	|	dkr9td||jf ƒ‚| |¡}|jd urV| j |¡}
| j |
|¡}t|||ƒ d S |‰ t||ƒ ‡ fdd„}‡ fdd„}tt|ƒ|t||ƒƒ t|ƒj |¡ d S )Nr   z/bad size: %r does not seem to be an array of %sc                    s   ˆ d S r   r   r^   ©Úptrr   r   Úgetter  s   z3VCPythonEngine._loaded_cpy_variable.<locals>.getterc                    s   |ˆ d< d S r   r   )r_   rL   r8  r   r   Úsetter  s   z3VCPythonEngine._loaded_cpy_variable.<locals>.setter)r~   r0   r   r¢   r6  r0  r   rk   r£   Údivmodr  r   Úresolve_lengthrí   ÚcastrÈ   Údelattrr-   ÚpropertyrR   Úappend)r   r4   r2   r\   r_   rL   ÚsizeÚ	BItemTyperí   ÚrestÚBArrayr:  r;  r   r8  r   Ú_loaded_cpy_variableû  s2   
ÿÿ


z#VCPythonEngine._loaded_cpy_variablec                 C   s4   | j }|dƒ |dƒ |d| jd  ƒ |dƒ d S )Nz,static int _cffi_setup_custom(PyObject *lib)r:   r  Tr=   )r+   r>   )r   rC   r   r   r   rA     s
   z%VCPythonEngine._generate_setup_custom)r&   r	   )Nr  NTFN)r)  )LrW   rX   rY   Ú
_class_keyÚ_gen_python_moduler   r   r   r%   r+   r.   r6   rF   rv   r{   r#   rf   r†   rˆ   r�   r¡   r’   r©   Ú!_generate_cpy_typedef_collecttypeÚ_generate_cpy_typedef_declÚ_generate_cpy_typedef_methodÚ_loading_cpy_typedefÚ_loaded_cpy_typedefr­   rÂ   rÇ   Ú_loading_cpy_functionrÊ   Ú _generate_cpy_struct_collecttyperÐ   rÕ   rÛ   rß   Ú_generate_cpy_union_collecttyperã   rä   rå   ræ   rÏ   rÔ   rÙ   rÞ   Ú#_generate_cpy_anonymous_collecttyper  r  r  r  r·   r#  r$  Ú_generate_cpy_constant_methodÚ_loading_cpy_constantÚ_loaded_cpy_constantr  r(  r  Ú_generate_cpy_enum_collecttypeÚ_generate_cpy_enum_methodr  r  r2  Ú_generate_cpy_macro_collecttypeÚ_generate_cpy_macro_methodÚ_loading_cpy_macroÚ_loaded_cpy_macror5  r7  Ú_generate_cpy_variable_methodÚ_loading_cpy_variablerF  rA   r   r   r   r   r   
   sš    

M:
'P
3"	
þ0

	"r   a´(  
#include <Python.h>
#include <stddef.h>

/* this block of #ifs should be kept exactly identical between
   c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
   and cffi/_cffi_include.h */
#if defined(_MSC_VER)
# include <malloc.h>   /* for alloca() */
# if _MSC_VER < 1600   /* MSVC < 2010 */
   typedef __int8 int8_t;
   typedef __int16 int16_t;
   typedef __int32 int32_t;
   typedef __int64 int64_t;
   typedef unsigned __int8 uint8_t;
   typedef unsigned __int16 uint16_t;
   typedef unsigned __int32 uint32_t;
   typedef unsigned __int64 uint64_t;
   typedef __int8 int_least8_t;
   typedef __int16 int_least16_t;
   typedef __int32 int_least32_t;
   typedef __int64 int_least64_t;
   typedef unsigned __int8 uint_least8_t;
   typedef unsigned __int16 uint_least16_t;
   typedef unsigned __int32 uint_least32_t;
   typedef unsigned __int64 uint_least64_t;
   typedef __int8 int_fast8_t;
   typedef __int16 int_fast16_t;
   typedef __int32 int_fast32_t;
   typedef __int64 int_fast64_t;
   typedef unsigned __int8 uint_fast8_t;
   typedef unsigned __int16 uint_fast16_t;
   typedef unsigned __int32 uint_fast32_t;
   typedef unsigned __int64 uint_fast64_t;
   typedef __int64 intmax_t;
   typedef unsigned __int64 uintmax_t;
# else
#  include <stdint.h>
# endif
# if _MSC_VER < 1800   /* MSVC < 2013 */
#  ifndef __cplusplus
    typedef unsigned char _Bool;
#  endif
# endif
# define _cffi_float_complex_t   _Fcomplex    /* include <complex.h> for it */
# define _cffi_double_complex_t  _Dcomplex    /* include <complex.h> for it */
#else
# include <stdint.h>
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
#  include <alloca.h>
# endif
# define _cffi_float_complex_t   float _Complex
# define _cffi_double_complex_t  double _Complex
#endif

#define _cffi_from_c_double PyFloat_FromDouble
#define _cffi_from_c_float PyFloat_FromDouble
#define _cffi_from_c_long PyLong_FromLong
#define _cffi_from_c_ulong PyLong_FromUnsignedLong
#define _cffi_from_c_longlong PyLong_FromLongLong
#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
#define _cffi_from_c__Bool PyBool_FromLong

#define _cffi_to_c_double PyFloat_AsDouble
#define _cffi_to_c_float PyFloat_AsDouble

#define _cffi_from_c_int_const(x)                                        \
    (((x) > 0) ?                                                         \
        ((unsigned long long)(x) <= (unsigned long long)LONG_MAX) ?      \
            PyLong_FromLong((long)(x)) :                                 \
            PyLong_FromUnsignedLongLong((unsigned long long)(x)) :       \
        ((long long)(x) >= (long long)LONG_MIN) ?                        \
            PyLong_FromLong((long)(x)) :                                 \
            PyLong_FromLongLong((long long)(x)))

#define _cffi_from_c_int(x, type)                                        \
    (((type)-1) > 0 ? /* unsigned */                                     \
        (sizeof(type) < sizeof(long) ?                                   \
            PyLong_FromLong((long)x) :                                   \
         sizeof(type) == sizeof(long) ?                                  \
            PyLong_FromUnsignedLong((unsigned long)x) :                  \
            PyLong_FromUnsignedLongLong((unsigned long long)x)) :        \
        (sizeof(type) <= sizeof(long) ?                                  \
            PyLong_FromLong((long)x) :                                   \
            PyLong_FromLongLong((long long)x)))

#define _cffi_to_c_int(o, type)                                          \
    ((type)(                                                             \
     sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o)        \
                                         : (type)_cffi_to_c_i8(o)) :     \
     sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o)       \
                                         : (type)_cffi_to_c_i16(o)) :    \
     sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o)       \
                                         : (type)_cffi_to_c_i32(o)) :    \
     sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o)       \
                                         : (type)_cffi_to_c_i64(o)) :    \
     (Py_FatalError("unsupported size for type " #type), (type)0)))

#define _cffi_to_c_i8                                                    \
                 ((int(*)(PyObject *))_cffi_exports[1])
#define _cffi_to_c_u8                                                    \
                 ((int(*)(PyObject *))_cffi_exports[2])
#define _cffi_to_c_i16                                                   \
                 ((int(*)(PyObject *))_cffi_exports[3])
#define _cffi_to_c_u16                                                   \
                 ((int(*)(PyObject *))_cffi_exports[4])
#define _cffi_to_c_i32                                                   \
                 ((int(*)(PyObject *))_cffi_exports[5])
#define _cffi_to_c_u32                                                   \
                 ((unsigned int(*)(PyObject *))_cffi_exports[6])
#define _cffi_to_c_i64                                                   \
                 ((long long(*)(PyObject *))_cffi_exports[7])
#define _cffi_to_c_u64                                                   \
                 ((unsigned long long(*)(PyObject *))_cffi_exports[8])
#define _cffi_to_c_char                                                  \
                 ((int(*)(PyObject *))_cffi_exports[9])
#define _cffi_from_c_pointer                                             \
    ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10])
#define _cffi_to_c_pointer                                               \
    ((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11])
#define _cffi_get_struct_layout                                          \
    ((PyObject *(*)(Py_ssize_t[]))_cffi_exports[12])
#define _cffi_restore_errno                                              \
    ((void(*)(void))_cffi_exports[13])
#define _cffi_save_errno                                                 \
    ((void(*)(void))_cffi_exports[14])
#define _cffi_from_c_char                                                \
    ((PyObject *(*)(char))_cffi_exports[15])
#define _cffi_from_c_deref                                               \
    ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16])
#define _cffi_to_c                                                       \
    ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17])
#define _cffi_from_c_struct                                              \
    ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18])
#define _cffi_to_c_wchar_t                                               \
    ((wchar_t(*)(PyObject *))_cffi_exports[19])
#define _cffi_from_c_wchar_t                                             \
    ((PyObject *(*)(wchar_t))_cffi_exports[20])
#define _cffi_to_c_long_double                                           \
    ((long double(*)(PyObject *))_cffi_exports[21])
#define _cffi_to_c__Bool                                                 \
    ((_Bool(*)(PyObject *))_cffi_exports[22])
#define _cffi_prepare_pointer_call_argument                              \
    ((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23])
#define _cffi_convert_array_from_object                                  \
    ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24])
#define _CFFI_NUM_EXPORTS 25

typedef struct _ctypedescr CTypeDescrObject;

static void *_cffi_exports[_CFFI_NUM_EXPORTS];
static PyObject *_cffi_types, *_cffi_VerificationError;

static int _cffi_setup_custom(PyObject *lib);   /* forward */

static PyObject *_cffi_setup(PyObject *self, PyObject *args)
{
    PyObject *library;
    int was_alive = (_cffi_types != NULL);
    (void)self; /* unused */
    if (!PyArg_ParseTuple(args, "OOO", &_cffi_types, &_cffi_VerificationError,
                                       &library))
        return NULL;
    Py_INCREF(_cffi_types);
    Py_INCREF(_cffi_VerificationError);
    if (_cffi_setup_custom(library) < 0)
        return NULL;
    return PyBool_FromLong(was_alive);
}

union _cffi_union_alignment_u {
    unsigned char m_char;
    unsigned short m_short;
    unsigned int m_int;
    unsigned long m_long;
    unsigned long long m_longlong;
    float m_float;
    double m_double;
    long double m_longdouble;
};

struct _cffi_freeme_s {
    struct _cffi_freeme_s *next;
    union _cffi_union_alignment_u alignment;
};

#ifdef __GNUC__
  __attribute__((unused))
#endif
static int _cffi_convert_array_argument(CTypeDescrObject *ctptr, PyObject *arg,
                                        char **output_data, Py_ssize_t datasize,
                                        struct _cffi_freeme_s **freeme)
{
    char *p;
    if (datasize < 0)
        return -1;

    p = *output_data;
    if (p == NULL) {
        struct _cffi_freeme_s *fp = (struct _cffi_freeme_s *)PyObject_Malloc(
            offsetof(struct _cffi_freeme_s, alignment) + (size_t)datasize);
        if (fp == NULL)
            return -1;
        fp->next = *freeme;
        *freeme = fp;
        p = *output_data = (char *)&fp->alignment;
    }
    memset((void *)p, 0, (size_t)datasize);
    return _cffi_convert_array_from_object(p, ctptr, arg);
}

#ifdef __GNUC__
  __attribute__((unused))
#endif
static void _cffi_free_array_arguments(struct _cffi_freeme_s *freeme)
{
    do {
        void *p = (void *)freeme;
        freeme = freeme->next;
        PyObject_Free(p);
    } while (freeme != NULL);
}

static int _cffi_init(void)
{
    PyObject *module, *c_api_object = NULL;

    module = PyImport_ImportModule("_cffi_backend");
    if (module == NULL)
        goto failure;

    c_api_object = PyObject_GetAttrString(module, "_C_API");
    if (c_api_object == NULL)
        goto failure;
    if (!PyCapsule_CheckExact(c_api_object)) {
        PyErr_SetNone(PyExc_ImportError);
        goto failure;
    }
    memcpy(_cffi_exports, PyCapsule_GetPointer(c_api_object, "cffi"),
           _CFFI_NUM_EXPORTS * sizeof(void *));

    Py_DECREF(module);
    Py_DECREF(c_api_object);
    return 0;

  failure:
    Py_XDECREF(module);
    Py_XDECREF(c_api_object);
    return -1;
}

#define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num))

/**********/
)	rb   r&   r   rt   r   r   r   r   r?   r   r   r   r   Ú<module>   s          