pWc@sddlZddlZddlmZyeWn*ek r\ddlmZdZnXyeWnek r~e ZnXde fdYZ de fd YZ d e fd YZd Zd ZdZdS(iNi(t allocate_lock(tCallablecCs t|tS(N(t isinstanceR(tx((s./usr/lib64/python2.7/site-packages/cffi/api.pyt stFFIErrorcBseZRS((t__name__t __module__(((s./usr/lib64/python2.7/site-packages/cffi/api.pyRst CDefErrorcBseZdZRS(cCsVyd|jdjjf}Wn tttfk r@d}nXd||jdfS(Ns line %d: its%s%si(targstcoordtlinetAttributeErrort TypeErrort IndexError(tselfR ((s./usr/lib64/python2.7/site-packages/cffi/api.pyt__str__s  (RRR(((s./usr/lib64/python2.7/site-packages/cffi/api.pyRstFFIcBseZdZd5dZeedZedZedZddZ dZ edZ d Z d Z d Zd Zd5d Zd5d5edZdZddZdZddZdZdZd5d5d5dZddZdZdZdd5dZdZdZe 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/dd5d0Z0d1Z1d2Z2d3Z3d4Z4RS(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")) cCsddlm}m}|dkrAddl}ddlm}n||_t|_|j |_ i|_ t j dj|_t j dj|_g|_g|_g|_g|_d|_i|_d|_d|_t|dr|j|nx<|jD]1}|jd r t||t||q q W|j/|j|j|_ |j|j!|_"WdQXt#|t j rtt$d s|j%|j d t$_&|j'\t$_(t$_)qn0|j%|j d |_&|j'\|_(|_)dS( s{Create an FFI instance. The 'backend' argument is used to select a non-default backend, mostly for tests. i(tcparsertmodeliN(t __version__t parsed_typest new_typestset_ffitRTLD_tNULLi(*R RRtNonet _cffi_backendRt_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 _embeddingthasattrRt startswithtsetattrtgetattrt_get_cached_btypet voidp_typetBVoidPtchar_array_typetBCharARRtcastRt _get_typestCDatatCType(RtbackendRRRtname((s./usr/lib64/python2.7/site-packages/cffi/api.pyt__init__0sB               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(RtcsourceR?R@((s./usr/lib64/python2.7/site-packages/cffi/api.pytcdefascCs8|j|d|dt|jdkr4d|_ndS(NR@t dllexportR (RAtTrueR.R(RRBR@((s./usr/lib64/python2.7/site-packages/cffi/api.pyt embedding_apiksc 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 basestringRtencodeRtobjectR-R tparseR)tappendR'tcleart _recompletetfinish_backend_type(RRBR?toptionstcachet finishlistttp((s./usr/lib64/python2.7/site-packages/cffi/api.pyRAps      icCsL|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(Rt_make_ffi_libraryR'RMR((RR=tflagstlibtfunction_cache((s./usr/lib64/python2.7/site-packages/cffi/api.pytdlopens  cCs|}||jkr |j|St|tsA|jd}n|jj|}|j}|rq|j}n|j|}||f}||j|<|S(NRG( R%RRHRJR t parse_typetis_raw_functiontas_function_pointerR3(Rtcdecltkeyttypetreally_a_function_typetbtypetresult((s./usr/lib64/python2.7/site-packages/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(R%tKeyErrorRRcR(RR]tconsider_function_as_funcptrRbRaR`((s./usr/lib64/python2.7/site-packages/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(RRIRfR:RttypeofR"tBuiltinFunctionTypet_builtin_function_typeRt FunctionTypeR/RR3RgRR_(RR]tres((s./usr/lib64/python2.7/site-packages/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(RRIRfRtsizeof(RR]tBType((s./usr/lib64/python2.7/site-packages/cffi/api.pyRmscCs1t|tr!|j|}n|jj|S(s\Return the natural alignment size in bytes of the C type given as a string. (RRIRfRtalignof(RR]((s./usr/lib64/python2.7/site-packages/cffi/api.pyRoscGs5t|tr!|j|}n|j||dS(s5Return 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(RRIRft _typeoffsetof(RR]tfields_or_indexes((s./usr/lib64/python2.7/site-packages/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. (RRIRfRtnewp(RR]tinit((s./usr/lib64/python2.7/site-packages/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(RRIRf(R]Rt(t allocatorR(s./usr/lib64/python2.7/site-packages/cffi/api.pytallocate sN(RRt new_allocatorR(Rtalloctfreetshould_clear_after_alloct compiled_ffiRw((RvRs./usr/lib64/python2.7/site-packages/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. (RRIRfRR8(RR]tsource((s./usr/lib64/python2.7/site-packages/cffi/api.pyR8sicCs|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(Rtcdatatmaxlen((s./usr/lib64/python2.7/site-packages/cffi/api.pyR~scCs|jj||S(sMUnpack 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(RRtlength((s./usr/lib64/python2.7/site-packages/cffi/api.pyR.scCs|jj||S(sReturn a read-write buffer object that references the raw C data pointed to by the given 'cdata'. The 'cdata' must be a pointer or an array. Can be passed to functions expecting a buffer, or directly manipulated with: buf[:] get a copy of it in a regular string, or buf[idx] as a single character buf[:] = ... buf[idx] = ... change the content (Rtbuffer(RRtsize((s./usr/lib64/python2.7/site-packages/cffi/api.pyR?s cCs|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, unicode, or bytearray (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_bufferR7(Rt python_buffer((s./usr/lib64/python2.7/site-packages/cffi/api.pyRLscCs|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(Rtdesttsrctn((s./usr/lib64/python2.7/site-packages/cffi/api.pyRVscs]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(tcallableRRtcallback(tpython_callable(R]terrortonerrorR(s./usr/lib64/python2.7/site-packages/cffi/api.pytcallback_decorator_wrapns ReN(RRIRfRER(RR]RRRR((R]RRRs./usr/lib64/python2.7/site-packages/cffi/api.pyRfs  R cCst|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 (RRIRftstripR0Rtgetcname(RR]t replace_with((s./usr/lib64/python2.7/site-packages/cffi/api.pytgetctype{s   c Csy|jj}Wntk r#nX|||S|jYy |j}Wn4tk r~ddlm}||}|_nX|j||SWdQXdS(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. i(t GcWeakrefsN(RtgcpR Rt gc_weakrefst gc_weakrefRtbuild(RRt destructorRRR((s./usr/lib64/python2.7/site-packages/cffi/api.pytgcs     cCsby|j|}WnJtk r]g}|j||}x"|D]}|j||q@WnX|S(N(R!Rdtget_cached_btypeRP(RR_RnRS((s./usr/lib64/python2.7/site-packages/cffi/api.pyR3s  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(RM(RR}ttmpdirtkwargsRRRW((s./usr/lib64/python2.7/site-packages/cffi/api.pytverifys  cCs |jjS(N(Rt get_errno(R((s./usr/lib64/python2.7/site-packages/cffi/api.pyt _get_errnoscCs|jj|dS(N(Rt set_errno(Rterrno((s./usr/lib64/python2.7/site-packages/cffi/api.pyt _set_errnoss(the value of 'errno' from/to the C callscCs|jj|S(N(Rt getwinerror(Rtcode((s./usr/lib64/python2.7/site-packages/cffi/api.pyRscCs4ddlm}|j|j||SWdQXdS(Ni(R(R RRt pointer_cache(RtctypeR((s./usr/lib64/python2.7/site-packages/cffi/api.pyt _pointer_tos cGs||jj|}|r3|j||\}}n$|jdkrQtdnd}|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. tpointersaddressof(pointer)i(RRhRptkindRRt rawaddressof(RRRqRtoffsettctypeptr((s./usr/lib64/python2.7/site-packages/cffi/api.pyt addressofscGs^|jj||\}}x6|D].}|jj||d\}}||7}q"W||fS(Ni(Rt typeoffsetof(RRtfield_or_indexRqRtfield1toffset1((s./usr/lib64/python2.7/site-packages/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( RRRR_Rt ValueErrorRR tincludeR)RMtextendR*(Rtffi_to_include((s./usr/lib64/python2.7/site-packages/cffi/api.pyRs    cCs|jj|j|S(N(Rt newp_handleR5(RR((s./usr/lib64/python2.7/site-packages/cffi/api.pyt new_handlescCs|jj|S(N(Rt from_handle(RR((s./usr/lib64/python2.7/site-packages/cffi/api.pyR scCsZ|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+RRtboolRC(Rt enabled_flag((s./usr/lib64/python2.7/site-packages/cffi/api.pyt set_unicode s  cCsZ|jdd}t|ttfs6tdnt|ddg}||dRRCRFRARYRcRfRhRmRoRrRuRERxR8R~RRRRRRRR3RRRtpropertyRRRRRpRRRRRRRRRRR RRRR$(((s./usr/lib64/python2.7/site-packages/cffi/api.pyRsb 1                             /     cCs|dkr4tjdkr+|jd|Sd}nyBd|kred|kretd|fn|j||SWnNtk rddl}|jj|}|dkrn|j||SXdS(NRtcR t/slibrary not found: %ri(RRRRtOSErrort ctypes.utiltutilt find_library(R<R=RVtctypesR((s./usr/lib64/python2.7/site-packages/cffi/api.pyt_load_backend_libs     csJddl}j}t||| fdfdd fditg fd  fd dtf fd Y|dk r4y2t|ts|jd }nd |_ Wq4t k r0q4Xn j fS( Nicsd|}jj|\}}j|}yj||}Wn)tk rp}td||fnX|j|s ( R-R RR RtitemsRtEnumTypetsplitt enumeratet enumeratorsR<R( RR^RTR0RR=RtenumnameRC(R;R5R=R9t accessorstaccessors_versionR3R4(s./usr/lib64/python2.7/site-packages/cffi/api.pytupdate_accessors.s$%     csujf|jks(|jkr,dS|kr]|kr]t|q]n||WdQXdS(N(RR$R (R=(R8RJR3R4RL(s./usr/lib64/python2.7/site-packages/cffi/api.pyt make_accessorFs   R8cs;eZfdZfdZfdZRS(cs|t||S(N(R2(RR=(RM(s./usr/lib64/python2.7/site-packages/cffi/api.pyt __getattr__Qs csXyt|j|}Wn+tk rC|t|||nX|j||dS(N(R2t __class__R R1t__set__(RR=RR&(RM(s./usr/lib64/python2.7/site-packages/cffi/api.pyt __setattr__Ts   cs%jjSWdQXdS(N(Rtkeys(R(RJR3RL(s./usr/lib64/python2.7/site-packages/cffi/api.pyt__dir__\s (RRRNRQRS((RJR3RMRL(s./usr/lib64/python2.7/site-packages/cffi/api.pyR8Pssutf-8s FFILibrary_%s( RRR.RRKRRRHRJRt UnicodeErrorR$(R3tlibnameRVRR<(( R8R;R5R=R9RJRKR2R3R4RMRLs./usr/lib64/python2.7/site-packages/cffi/api.pyRU s*     $ %   cCs{ddl}y0|j|j}|j}|j}||}Wntttfk rYdSX|j |j |SWdQXdS(Ni( RtmodulesRt_cffi_original_ffit_cffi_types_of_builtin_funcsRdR RRRR3(R RtmoduleR3ttypes_of_builtin_funcsRT((s./usr/lib64/python2.7/site-packages/cffi/api.pyRjks    (RR"tlockRRt NameErrort collectionsRRIRHt ExceptionRRRKRR.RURj(((s./usr/lib64/python2.7/site-packages/cffi/api.pyts$       `