_e[c@sddlZddlZddlmZddlmZddlmZyeWn*e k r|ddl m Z dZnXye Wne k re Z nXdefd YZd Zd Zd ZdS( iNi(t allocate_lock(t CDefError(tmodel(tCallablecCs t|tS(N(t isinstanceR(tx((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt stFFIcBs eZdZd5dZeedZedZedZddZ dZ dZ ed Z d Z d Zd Zd Zd5dZd5d5edZdZddZdZdZdZd5d5d5dZddZddZdZdd5dZdZdZe eed5dZ!ddZ"d Z#d!Z$d"Z%d#Z&d$Z'd%Z(d&Z)d'Z*d(Z+d)d*Z,d+ed,Z-d-Z.d.Z/d/dd5d5d0Z0d1Z1d2Z2d3Z3d4Z4RS(6s The main top-level class that you instantiate once, or once per module. Example usage: ffi = FFI() ffi.cdef(""" int printf(const char *, ...); """) C = ffi.dlopen(None) # standard library -or- C = ffi.verify() # use a C compiler: verify the decl above is right C.printf("hello, %s!\n", ffi.new("char[]", "world")) cCs|dkrddl}ddlm}|j|krt|drktd|t|j|jfqtd|t|jfqnddlm}||_t |_ |j |_ i|_ tjd j|_tjd j|_g|_g|_g|_g|_d|_i|_d|_d|_tj||_t|d rm|j|nxBt|jD]1}|j d r}t!||t"||q}q}W|j /|j#tj$|_%|j#tj&|_'WdQXt(|tjrCtt)d ss|j*|j%dt)_+|j,\t)_-t)_.qsn0|j*|j%d|_+|j,\|_-|_.|j/|_/dS(s{Create an FFI instance. The 'backend' argument is used to select a non-default backend, mostly for tests. iNi(t __version__t__file__sVersion mismatch: this is the 'cffi' package version %s, located in %r. When we import the top-level '_cffi_backend' extension module, we get version %s, located in %r. The two versions should be equal; check your installation.sVersion mismatch: this is the 'cffi' package version %s, located in %r. This interpreter comes with a built-in '_cffi_backend' module, which is version %s. The two versions should be equal; check your installation.(tcparsert parsed_typest new_typestset_ffitRTLD_tNULLi(0tNonet _cffi_backendtRthasattrt ExceptionR R t_backendRt_locktParsert_parsert_cached_btypesttypest ModuleTypet__dict__t _parsed_typest _new_typest_function_cachest _librariest _cdefsourcest_included_ffist_windows_unicodet_init_once_cachet _cdef_versiont _embeddingRt get_typecachet _typecacheR tlistt startswithtsetattrtgetattrt_get_cached_btypet voidp_typetBVoidPtchar_array_typetBCharARRtcastRt _get_typestCDatatCTypetbuffer(tselftbackendRR tname((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt__init__'sR               cCs|j|d|d|dS(sParse the given C source. This registers all declared functions, types, and global variables. The functions and global variables can then be accessed via either 'ffi.dlopen()' or 'ffi.verify()'. The types can be used in 'ffi.new()' and other functions. If 'packed' is specified as True, all structs declared inside this cdef are packed, i.e. laid out without any field alignment at all. toverridetpackedN(t_cdef(R7tcsourceR;R<((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytcdefcscCs8|j|d|dt|jdkr4d|_ndS(NR<t dllexportR(R=tTrueR&R(R7R>R<((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt embedding_apimsc Kst|ts?t|ts-tdn|jd}n|jt|_|jj |d|||j j ||rx|j D]}|j qWn|jj}|rg|j_x!|D]}|j||qWnWdQXdS(Ns cdef() argument must be a stringtasciiR;(Rtstrt basestringt TypeErrortencodeRtobjectR%RtparseR!tappendRtcleart _recompletetfinish_backend_type(R7R>R;toptionstcachet finishlistttp((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyR=rs      icCsmt|ts!|dks!t|j=t|||\}}|jj||jj|WdQX|S(sHLoad and return a dynamic library identified by 'name'. The standard C library can be loaded by passing None. Note that functions and types declared by 'ffi.cdef()' are not linked to a particular library, just like C headers; in the library we only look for the actual (untyped) symbols. N( RRERtAssertionErrorRt_make_ffi_libraryRRJR (R7R9tflagstlibtfunction_cache((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytdlopens ! cCst|j|dS(sClose a library obtained with ffi.dlopen(). After this call, access to functions or variables from the library will fail (possibly with a segmentation fault). N(ttypet__cffi_close__(R7RU((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytdlclosescCs|}||jkr |j|St|tsA|jd}n|jj|}|j}|rq|j}n|j|}||f}||j|<|S(NRC( RRRDRGRt parse_typetis_raw_functiontas_function_pointerR-(R7tcdecltkeyRXtreally_a_function_typetbtypetresult((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt_typeof_lockeds    cCswy|j|}Wn0tk rC|j|j|}WdQXnX|\}}|rs| rstd|fn|S(Ns>the type %r is a function type, not a pointer-to-function type(RtKeyErrorRRcR(R7R^tconsider_function_as_funcptrRbRaR`((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt_typeofs    cCst|tr|j|St||jr>|jj|St|tjrot|}|dk ro|Snt|tj rt |dr|j |j |jSWdQXntt|dS(sParse the C type given as a string and return the corresponding object. It can also be used on 'cdata' instance to get its C type. t_cffi_base_typeN(RRERfR4RttypeofRtBuiltinFunctionTypet_builtin_function_typeRt FunctionTypeRRR-RgRFRX(R7R^tres((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRhs    cCsBt|tr.|j|}|jj|S|jj|SdS(svReturn the size in bytes of the argument. It can be a string naming a C type, or a 'cdata' instance. N(RRERfRtsizeof(R7R^tBType((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRmscCs1t|tr!|j|}n|jj|S(s\Return the natural alignment size in bytes of the C type given as a string. (RRERfRtalignof(R7R^((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRoscGs5t|tr!|j|}n|j||dS(s3Return the offset of the named field inside the given structure or array, which must be given as a C type name. You can give several field names in case of nested structures. You can also give numeric values which correspond to array items, in case of an array type. i(RRERft _typeoffsetof(R7R^tfields_or_indexes((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytoffsetofscCs4t|tr!|j|}n|jj||S(sAllocate an instance according to the specified C type and return a pointer to it. The specified C type must be either a pointer or an array: ``new('X *')`` allocates an X and returns a pointer to it, whereas ``new('X[n]')`` allocates an array of n X'es and returns an array referencing it (which works mostly like a pointer, like in C). You can also use ``new('X[]', n)`` to allocate an array of a non-constant length n. The memory is initialized following the rules of declaring a global variable in C: by default it is zero-initialized, but an explicit initializer can be given which can be used to fill all or part of the memory. When the returned object goes out of scope, the memory is freed. In other words the returned object has ownership of the value of type 'cdecl' that it points to. This means that the raw data can be used as long as this object is kept alive, but must not be used for a longer time. Be careful about that when copying the pointer to the memory somewhere else, e.g. into another structure. (RRERfRtnewp(R7R^tinit((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytnewscs=jj}|j|||dfd}|S(sReturn a new allocator, i.e. a function that behaves like ffi.new() but uses the provided low-level 'alloc' and 'free' functions. 'alloc' is called with the size as argument. If it returns NULL, a MemoryError is raised. 'free' is called with the result of 'alloc' as argument. Both can be either Python function or directly C functions. If 'free' is None, then no free function is called. If both 'alloc' and 'free' are None, the default is used. If 'should_clear_after_alloc' is set to False, then the memory returned by 'alloc' is assumed to be already cleared (or you are fine with garbage); otherwise CFFI will clear it. cs.t|tr!j|}n||S(N(RRERf(R^Rt(t allocatorR7(s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytallocatesN(RRt new_allocatorR(R7talloctfreetshould_clear_after_alloct compiled_ffiRw((RvR7s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRxs   cCs4t|tr!|j|}n|jj||S(sSimilar to a C cast: returns an instance of the named C type initialized with the given 'source'. The source is casted between integers or pointers of any type. (RRERfRR2(R7R^tsource((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyR2sicCs|jj||S(sReturn a Python string (or unicode string) from the 'cdata'. If 'cdata' is a pointer or array of characters or bytes, returns the null-terminated string. The returned string extends until the first null character, or at most 'maxlen' characters. If 'cdata' is an array then 'maxlen' defaults to its length. If 'cdata' is a pointer or array of wchar_t, returns a unicode string following the same rules. If 'cdata' is a single character or byte or a wchar_t, returns it as a string or unicode string. If 'cdata' is an enum, returns the value of the enumerator as a string, or 'NUMBER' if the value is out of range. (Rtstring(R7tcdatatmaxlen((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyR~%scCs|jj||S(sLUnpack an array of C data of the given length, returning a Python string/unicode/list. If 'cdata' is a pointer to 'char', returns a byte string. It does not stop at the first null. This is equivalent to: ffi.buffer(cdata, length)[:] If 'cdata' is a pointer to 'wchar_t', returns a unicode string. 'length' is measured in wchar_t's; it is not the size in bytes. If 'cdata' is a pointer to anything else, returns a list of 'length' items. This is a faster equivalent to: [cdata[i] for i in range(length)] (Rtunpack(R7Rtlength((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyR7scCs|jj|j|S(sReturn a that points to the data of the given Python object, which must support the buffer interface. Note that this is not meant to be used on the built-in types str or unicode (you can build 'char[]' arrays explicitly) but only on objects containing large quantities of raw data in some other format, like 'array.array' or numpy arrays. (Rt from_bufferR1(R7t python_buffer((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRUscCs|jj|||S(s_ffi.memmove(dest, src, n) copies n bytes of memory from src to dest. Like the C function memmove(), the memory areas may overlap; apart from that it behaves like the C function memcpy(). 'src' can be any cdata ptr or array, or any Python buffer object. 'dest' can be any cdata ptr or array, or a writable Python buffer object. The size to copy, 'n', is always measured in bytes. Unlike other methods, this one supports all Python buffer including byte strings and bytearrays---but it still does not support non-contiguous buffers. (Rtmemmove(R7tdesttsrctn((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyR_scs]fd}ttr?jdtn|dkrO|S||SdS(sReturn a callback object or a decorator making such a callback object. 'cdecl' must name a C function pointer type. The callback invokes the specified 'python_callable' (which may be provided either directly or via a decorator). Important: the callback object must be manually kept alive for as long as the callback may be invoked from the C level. cs4t|stdnjj|S(Ns.the 'python_callable' argument is not callable(tcallableRFRtcallback(tpython_callable(R^terrortonerrorR7(s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytcallback_decorator_wrapws ReN(RRERfRAR(R7R^RRRR((R^RRR7s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRos  RcCst|tr!|j|}n|j}|jdrdd|jj|dkrdd|}n#|r|ddkrd|}n|jj||S(s.Return a string giving the C type 'cdecl', which may be itself a string or a object. If 'replace_with' is given, it gives extra text to append (or insert for more complicated C types), like a variable name, or '*' to get actually the C type 'pointer-to-cdecl'. t*s&[t&s(%s)is[(t (RRERftstripR*Rtgetcname(R7R^t replace_with((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytgetctypes   cCs|jj|||S(sReturn a new cdata object that points to the same data. Later, when this new cdata object is garbage-collected, 'destructor(old_cdata_object)' will be called. The optional 'size' gives an estimate of the size, used to trigger the garbage collection more eagerly. So far only used on PyPy. It tells the GC that the returned object keeps alive roughly 'size' bytes of external memory. (Rtgcp(R7Rt destructortsize((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytgcs cCs|jjttksty|j|}WnJtk r{g}|j||}x"|D]}|j||q^WnX|S(N(RtacquiretFalseRRRRdtget_cached_btypeRM(R7RXRnRP((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyR-s  cKsyddlm}m}|jr/|j|n|p;|}||||||_|jj}|jj||S(sVerify that the current ffi signatures compile on this machine, and return a dynamic library object. The dynamic library can be used to call functions and access global variables declared in this 'ffi'. The library is compiled by the C compiler: it gives you C-level API compatibility (including calling macros). This is unlike 'ffi.dlopen()', which requires binary compatibility in the signatures. i(tVerifiert_caller_dir_pycache(tverifierRRR#t_apply_windows_unicodet load_libraryR RJ(R7R}ttmpdirtkwargsRRRU((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytverifys  cCs |jjS(N(Rt get_errno(R7((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt _get_errnoscCs|jj|dS(N(Rt set_errno(R7terrno((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt _set_errnoss(the value of 'errno' from/to the C callscCs|jj|S(N(Rt getwinerror(R7tcode((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRscCs$|jtj||SWdQXdS(N(RRt pointer_cache(R7tctype((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt _pointer_tos cGsy|jj|}Wn?tk rWdt|jkrQt|j||SnX|ry|j||\}}n$|jdkrtdnd}|j|}|jj |||S(sReturn the address of a . If 'fields_or_indexes' are given, returns the address of that field or array item in the structure or array, recursively in case of nested structures. t __addressof__tpointersaddressof(pointer)i( RRhRFRXRRRptkindRt rawaddressof(R7RRqRtoffsettctypeptr((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt addressofs cGs^|jj||\}}x6|D].}|jj||d\}}||7}q"W||fS(Ni(Rt typeoffsetof(R7Rtfield_or_indexRqRtfield1toffset1((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRps  c Cst|ts.tdt|jfn||krItdn|jk|j[|jj|j|j j d|j j |j |j j d|j j |WdQXWdQXdS(sIncludes the typedefs, structs, unions and enums defined in another FFI instance. Usage is similar to a #include in C, where a part of the program might include types defined in another part for its own usage. Note that the include() method has no effect on functions, constants and global variables, which must anyway be accessed directly from the lib object returned by the original FFI instance. sGffi.include() expects an argument that is also of type cffi.FFI, not %rsself.include(self)t[t]N( RRRFRXt__name__t ValueErrorRRtincludeR!RJtextendR"(R7tffi_to_include((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRs    cCs|jj|j|S(N(Rt newp_handleR/(R7R((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt new_handle scCs|jj|S(N(Rt from_handle(R7R((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyRscCsZ|jdk rtdnt|}|r@|jdn |jd||_dS(scWindows: if 'enabled_flag' is True, enable the UNICODE and _UNICODE defines in C, and declare the types like TCHAR and LPTCSTR to be (pointers to) wchar_t. If 'enabled_flag' is False, declare these types to be (pointers to) plain 8-bit characters. This is mostly for backward compatibility; you usually want True. s%set_unicode() can only be called oncestypedef wchar_t TBYTE;typedef wchar_t TCHAR;typedef const wchar_t *LPCTSTR;typedef const wchar_t *PCTSTR;typedef wchar_t *LPTSTR;typedef wchar_t *PTSTR;typedef TBYTE *PTBYTE;typedef TCHAR *PTCHAR;stypedef char TBYTE;typedef char TCHAR;typedef const char *LPCTSTR;typedef const char *PCTSTR;typedef char *LPTSTR;typedef char *PTSTR;typedef TBYTE *PTBYTE;typedef TCHAR *PTCHAR;N(R#RRtboolR?(R7t enabled_flag((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt set_unicodes  cCsZ|jdd}t|ttfs6tdnt|ddg}||d|dkrtjdkrtj d krtdnd|f}|dk r/d ||f}nt|n|j||S( NiRtcR t/isXdlopen(None) cannot work on Windows for Python 3 (see http://bugs.python.org/issue23606)sGctypes.util.find_library() did not manage to locate a library called %rs%s. Additionally, %s(i( RRRRRRtOSErrort ctypes.utiltutilt find_libraryR( R8R9RTRt first_errortetctypesRtmsg((s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pyt_load_backend_libs,   '   *  c se j}t||| fd fd fdd fditgi fd  fd dtf fd Y|dk rOy2t|ts*|jd }nd |_WqOt k rKqOXn j fS( NcsRd|}jj|\}}j|}j||}|j|R+R+(R9R_RQR8(t FFILibraryR9R:(RnR9R=R>s(/tmp/pip-install-KP2Jbq/cffi/cffi/api.pytaccessor_variable+s    csy |SWntk rj|krd|}jj|\}}j|}|jdkrtj|}nj||}||s$      z