o
    Êó·ii«  ã                   @   sD   d dl Z ddlmZ ddlmZ ddlmZ G dd„ deƒ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   úVC:\Users\psant\Downloads\PAF backend\school\venv\Lib\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ÚpathZso_suffixesÚfÚfilenameÚdescrr   r   r   r      s   ÿzVCPythonEngine.find_modulec                 C   s   i | _ |  d¡ d S )NZcollecttype)Ú
_typesdictÚ	_generate©r   r   r   r   Úcollect_types%   s   zVCPythonEngine.collect_typesÚ c                 C   s   | j  |d ¡ d S )NÚ
)Z_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   s  |   ¡  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ƒ |d|f ƒ |dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |ƒ  |d ƒ |ƒ  |dƒ |d!| ƒ |dƒ |dƒ |d"| ƒ |dƒ |d#ƒ |d$|f ƒ |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#if PY_MAJOR_VERSION >= 3z.static struct PyModuleDef _cffi_module_def = {z  PyModuleDef_HEAD_INIT,z  "%s",z  NULL,z  -1,z  _cffi_methods,z  NULL, NULL, NULL, NULLZ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;Ú}z#elsezinit%s(void)z+  lib = Py_InitModule("%s", _cffi_methods);z    return;z!  if (%s < 0 || _cffi_init() < 0)z	  return;)	r#   Ú_chained_list_constantsr(   Úcffimod_headerr
   Úpreambler!   Ú_generate_setup_customÚget_module_name)r   ÚprntÚmodnameÚ	constantsr   r   r   Úwrite_source_to_f7   sz   




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ƒ‰ ˆ ƒ }ˆ |t|¡r²d
d l}| dˆj ¡  ¡ ˆjˆd|d� ˆjˆ_ˆjˆ_|S )NÚgetdlopenflagsÚsetdlopenflagszimporting %r: %sZ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   ©rE   Úi)Ú
revmappingr   r   rH   ²   ó    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__Z_cffi_python_moduler   Z	_cffi_ffirM   rQ   r   )rP   Úmoduler   r   r   rP   º   s
    rP   r   z0reimporting %r might overwrite older definitionsZloaded©Úlibrary)r   Úacquire_lockÚhasattrÚsysrC   rD   Zload_dynamicr
   r>   Zmodulefilenamer   r   Úrelease_lockÚ_loadÚdictr    ÚitemsÚranger0   rN   Úmapr   Ú_get_cached_btypeÚobjectZ_cffi_setupÚwarningsÚwarnZ_cffi_original_ffir   Z_cffi_types_of_builtin_funcs)r   ÚflagsZprevious_flagsÚeÚerrorÚlstrW   rc   r   )rP   rU   rK   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   )rE   rF   r1   Zqualr   r   r   rH   Ð   s    z4VCPythonEngine._get_declarations.<locals>.<listcomp>)r   Ú_parserZ_declarationsr^   Úsort)r   rh   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)rl   ÚsplitÚgetattrÚAttributeErrorr   Ú	Exceptionr   Úattach_exception_info)r   Ú	step_namer/   r1   ÚkindÚrealnamer5   rf   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 )Nrm   r   z
_%s_cpy_%s)rl   rn   ro   rq   r   rr   )
r   rU   rs   r   r/   r1   rt   ru   r5   rf   r   r   r   r\   ä   s   €þûzVCPythonEngine._loadc                 C   r   r	   r   ©r   r1   r/   r   r   r   Ú_generate_nothingî   r   z VCPythonEngine._generate_nothingc                 K   r   r	   r   ©r   r1   r/   rU   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$   Ú_BoolZ_cffi_to_c_intz, %sz*not implemented in verify(): complex typesz(%s)_cffi_to_c_%srm   Ú_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()))r-   r   r.   Úis_integer_typer/   Zis_complex_typer   Ú
get_c_nameÚreplaceÚPointerTypeÚ"_convert_funcarg_to_c_ptr_or_arrayÚStructOrUnionÚEnumTyper(   r+   ÚFunctionPtrTypeÚNotImplementedError)r   r1   ÚfromvarÚtovarÚerrcodeZextraargÚ	converterZ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);)r-   r   r€   Úadd)r   r1   Ú	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;r8   )r(   r+   )r   r1   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|ƒ‚)Nrz   z_cffi_from_c_int(%s, %s)r,   z_cffi_from_c_%s(%s)rm   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)))r-   r   r.   r}   r/   r   r+   r€   r„   Ú	ArrayTypeÚitemr‚   ÚfldnamesÚ	TypeErrorÚ_get_c_namerƒ   r…   )r   r1   ÚvarÚcontextr   r   r   Ú_convert_expr_from_c1  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	   )r-   r   r„   Úellipsisr3   ÚargsÚresult)r   r1   r/   r*   r   r   r   Ú"_generate_cpy_function_collecttypeX  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   Znoargr   Zarg0r˜   ústatic PyObject *z(_cffi_f_%s(PyObject *self, PyObject *%s)r6   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   rI   r   r   r   rH   �  rL   z>VCPythonEngine._generate_cpy_function_decl.<locals>.<listcomp>r7   zarg%dúx%dzreturn NULLz  Py_BEGIN_ALLOW_THREADSz  _cffi_restore_errno();z  { %s%s(%s); }c                 S   rž   )rŸ   r   rI   r   r   r   rH   �  rL   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;r9   )r-   r   r„   r—   Ú_generate_cpy_constr(   r0   r˜   Ú	enumerater~   ÚsetrŽ   Úsortedr™   ZVoidTyper_   ÚjoinrŠ   r–   )r   r1   r/   r?   ÚnumargsÚargnamer•   rJ   r*   rŒ   r�   r4   Zresult_codeÚrngZfreeliner   r   r   Ú_generate_cpy_function_declc  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   ZMETH_NOARGSr   ZMETH_OZMETH_VARARGSz  {"%s", _cffi_f_%s, %s, NULL},)r—   r0   r˜   r(   )r   r1   r/   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—   ro   Úsetattrr   )r   r1   r/   rU   rW   Úfuncr   r   r   Ú_loaded_cpy_functionÁ  s
   
z#VCPythonEngine._loaded_cpy_functionc                 C   ó    ||j ksJ ‚|  |d|¡ d S ©NÚstruct©r/   Ú_generate_struct_or_union_declrv   r   r   r   Ú_generate_cpy_struct_declÌ  ó   z(VCPythonEngine._generate_cpy_struct_declc                 C   ó   |   |d|¡ d S r±   ©Ú _generate_struct_or_union_methodrv   r   r   r   Ú_generate_cpy_struct_methodÏ  ó   z*VCPythonEngine._generate_cpy_struct_methodc                 C   ó   |   |d||¡ d S r±   ©Ú_loading_struct_or_union©r   r1   r/   rU   r   r   r   Ú_loading_cpy_structÑ  r)   z"VCPythonEngine._loading_cpy_structc                 K   ó   |   |¡ d S r	   ©Ú_loaded_struct_or_unionrx   r   r   r   Ú_loaded_cpy_structÓ  ó   z!VCPythonEngine._loaded_cpy_structc                 C   r°   ©NÚunionr³   rv   r   r   r   Ú_generate_cpy_union_decl×  r¶   z'VCPythonEngine._generate_cpy_union_declc                 C   r·   rÆ   r¸   rv   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Â   rx   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)r6   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)Zqualsz
  /* %s */r9   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(   Ú
enumfieldsr-   r   r.   r}   r~   r   Ústrr�   Úlengthr“   )r   r1   Úprefixr/   ZcheckfuncnameÚlayoutfuncnameÚcnamer?   ÚfnameÚftypeÚfbitsizeÚfqualrf   r   r   r   r´   á  sd   
ÿÿþ€ÿ
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   r1   rÒ   r/   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‘   ro   r-   r   r‚   ÚpartialZforce_flattenr0   ZfixedlayoutrÎ   r   )r   r1   rÒ   r/   rU   rÓ   ÚfunctionÚlayoutZ	totalsizeZtotalalignmentZfieldofsZ	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   )Z	realvalueZexpectedvalueÚmsgr   r   r   Úcheck7  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   ra   r   ÚpopÚsizeofZalignofrÏ   Zoffsetofr0   )r   r1   rß   r   ZBStructrÝ   rÔ   rJ   rÕ   rÖ   r×   rØ   ZBFieldr   r   r   rÃ   0  s4   


ÿ
ÿ
éz&VCPythonEngine._loaded_struct_or_unionc                 C   s0   t |tjƒr|  ||d¡ d S |  |d|¡ d S ©Nr$   )r-   r   rƒ   Ú_generate_cpy_enum_declr´   rv   r   r   r   Ú_generate_cpy_anonymous_declT  s   z+VCPythonEngine._generate_cpy_anonymous_declc                 C   s"   t |tjƒs|  |d|¡ d S d S râ   )r-   r   rƒ   r¹   rv   r   r   r   Ú_generate_cpy_anonymous_methodZ  s   ÿz-VCPythonEngine._generate_cpy_anonymous_methodc                 C   s2   t |tjƒr|  |||¡ d S |  |d||¡ d S râ   )r-   r   rƒ   Ú_loading_cpy_enumr¾   r¿   r   r   r   Ú_loading_cpy_anonymous^  s   z%VCPythonEngine._loading_cpy_anonymousc                 K   s4   t |tjƒr| j|||fi |¤Ž d S |  |¡ d S r	   )r-   r   rƒ   Ú_loaded_cpy_enumrÃ   rx   r   r   r   Ú_loaded_cpy_anonymousd  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)r6   z  PyObject *o;z
  int res;rœ   z irê   r”   ú&z  i = (%s);z	  o = %s;rJ   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;r8   z-  res = PyObject_SetAttrString(lib, "%s", o);z  Py_DECREF(o);z  if (res < 0)ú  return %s;ú(lib)r9   )r(   r~   Ú_check_int_constant_valuer–   r:   )r   Úis_intr/   r1   ÚcategoryÚvartpÚdelayedÚsize_tooÚcheck_valuer?   ÚfuncnameZrealexprr   r   r   r¢   m  sR   

ÿ
ÿ
z"VCPythonEngine._generate_cpy_constc                 C   s*   t |tjƒo	| ¡ }|s|  |¡ d S d S r	   )r-   r   r.   r}   r3   ©r   r1   r/   rñ   r   r   r   Ú"_generate_cpy_constant_collecttype�  s   ÿz1VCPythonEngine._generate_cpy_constant_collecttypec                 C   s&   t |tjƒo	| ¡ }|  |||¡ d S r	   )r-   r   r.   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í   r8   )r(   )r   r/   rG   Z
err_prefixr?   r   r   r   rð   ­  s,   
ÿÿÿÿz(VCPythonEngine._check_int_constant_valuec                 C   s   |  dd¡}d||f S )Nú$Z___D_z_cffi_e_%s_%s)r   )r   rÒ   r/   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ë   r6   z	enum %s: rî   rï   r9   )	rÛ   Úenumeratorsr¢   rü   r(   ÚzipÚ
enumvaluesrð   r:   )r   r1   r/   rÒ   Ú
enumeratorr÷   r?   Ú	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   )ro   )rE   r  ©rU   r   r   rH   Þ  s    ÿz4VCPythonEngine._loading_cpy_enum.<locals>.<listcomp>T)rÛ   rþ   Útupler   Zpartial_resolved)r   r1   r/   rU   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   r1   r/   rU   rW   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   r1   r/   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	   )r-   r   r�   r€   r�   r3   ©r   r1   r/   Z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ò   )r-   r   r�   r€   r�   r¢   Úlength_is_unknownr  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   rV   ©Úptrr   r   Úgetter%  s   z3VCPythonEngine._loaded_cpy_variable.<locals>.getterc                    s   |ˆ d< d S r   r   )rW   rG   r
  r   r   Úsetter'  s   z3VCPythonEngine._loaded_cpy_variable.<locals>.setter)ro   r-   r   r�   r  r  r   ra   r�   Údivmodrá   r   Zresolve_lengthrÑ   Úcastr­   Údelattrr*   ÚpropertyrM   Úappend)r   r1   r/   rU   rW   rG   ÚsizeZ	BItemTyperÑ   ÚrestZBArrayr  r  r   r
  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)r6   rî   Tr9   )r(   r:   )r   r?   r   r   r   r=   .  s
   z%VCPythonEngine._generate_setup_custom)r$   r	   )Nrê   NTFN)rý   )LrR   rS   rT   Z
_class_keyZ_gen_python_moduler   r   r   r#   r(   r+   r3   rB   ri   rl   r!   r\   rw   ry   rŠ   rŽ   r�   r–   Z!_generate_cpy_typedef_collecttypeZ_generate_cpy_typedef_declZ_generate_cpy_typedef_methodZ_loading_cpy_typedefZ_loaded_cpy_typedefrš   rª   r¬   Z_loading_cpy_functionr¯   Z _generate_cpy_struct_collecttyperµ   rº   rÀ   rÄ   Z_generate_cpy_union_collecttyperÈ   rÉ   rÊ   rË   r´   r¹   r¾   rÃ   Z#_generate_cpy_anonymous_collecttyperä   rå   rç   ré   r¢   rù   rú   Z_generate_cpy_constant_methodZ_loading_cpy_constantZ_loaded_cpy_constantrð   rü   rã   Z_generate_cpy_enum_collecttypeZ_generate_cpy_enum_methodræ   rè   r  Z_generate_cpy_macro_collecttypeZ_generate_cpy_macro_methodZ_loading_cpy_macroZ_loaded_cpy_macror  r	  Z_generate_cpy_variable_methodZ_loading_cpy_variabler  r=   r   r   r   r   r   
   sš    

^:
'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

#if PY_MAJOR_VERSION < 3
# undef PyCapsule_CheckExact
# undef PyCapsule_GetPointer
# define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule))
# define PyCapsule_GetPointer(capsule, name) \
    (PyCObject_AsVoidPtr(capsule))
#endif

#if PY_MAJOR_VERSION >= 3
# define PyInt_FromLong PyLong_FromLong
#endif

#define _cffi_from_c_double PyFloat_FromDouble
#define _cffi_from_c_float PyFloat_FromDouble
#define _cffi_from_c_long PyInt_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) ?      \
            PyInt_FromLong((long)(x)) :                                  \
            PyLong_FromUnsignedLongLong((unsigned long long)(x)) :       \
        ((long long)(x) >= (long long)LONG_MIN) ?                        \
            PyInt_FromLong((long)(x)) :                                  \
            PyLong_FromLongLong((long long)(x)))

#define _cffi_from_c_int(x, type)                                        \
    (((type)-1) > 0 ? /* unsigned */                                     \
        (sizeof(type) < sizeof(long) ?                                   \
            PyInt_FromLong((long)x) :                                    \
         sizeof(type) == sizeof(long) ?                                  \
            PyLong_FromUnsignedLong((unsigned long)x) :                  \
            PyLong_FromUnsignedLongLong((unsigned long long)x)) :        \
        (sizeof(type) <= sizeof(long) ?                                  \
            PyInt_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))

/**********/
)
rZ   r$   r   rg   r   r   r   rb   r   r;   r   r   r   r   Ú<module>   s         1