
    Yh                         d dl Z d dlZddlmZ ddlmZ ddlmZ 	 e 	 e  e       Z G d d	e      Zd
 Zd Zd Zy# e	$ r d dl
mZ d ZY /w xY w# e	$ r eZY 9w xY w)    N   )allocate_lock)	CDefError)model)Callablec                 "    t        | t              S N)
isinstancer   )xs    Y/var/www/django-portfolio.reubendavern.link/venv/lib/python3.12/site-packages/cffi/api.py<lambda>r      s    Ax0     c                   p   e Zd ZdZd4dZd5dZd6dZd7dZd8dZd	 Z	d
 Z
d7dZd Zd Zd Zd Zd4dZ	 	 d9dZd Zd:dZd ZedfdZd Zd;dZd<dZd8dZd Zd=dZd Zd Z eeedd      Z d:dZ!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'd& Z(d' Z)d( Z*d) Z+d>d*Z,	 d>d+Z-d?d,Z.d- Z/d. Z0d@d/Z1d0 Z2d1 Z3d2 Z4d3 Z5y)AFFIa  
    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"))
    Nc                    |xddl }ddlm} |j                  |k7  r_t        |d      r0t	        d|dt
        d|j                  d|j
                  d		      t	        d|dt
        d
|j                  d	      ddlm} || _        t               | _	        |j                         | _        i | _        t        j                  d      j                  | _        t        j                  d      j                  | _        g | _        g | _        g | _        g | _        d| _        i | _        d| _        d| _        t5        j6                  |      | _        t        |d      r|j;                  |        t=        |j                        D ]+  }|j?                  d      stA        | |tC        ||             - | j                  5  | jE                  t4        jF                        | _$        | jE                  t4        jJ                        | _&        ddd       tO        |t        j                        r[t        tP        d      s| jS                  | jH                  d      tP        _*        |jW                         \  tP        _,        tP        _-        n>| jS                  | jH                  d      | _*        |jW                         \  | _,        | _-        |j\                  | _.        y# 1 sw Y   xY w)z{Create an FFI instance.  The 'backend' argument is used to
        select a non-default backend, mostly for tests.
        Nr   r   )__version____file__z5Version mismatch: this is the 'cffi' package version z, located in zQ.  When we import the top-level '_cffi_backend' extension module, we get version z=.  The two versions should be equal; check your installation.zS.  This interpreter comes with a built-in '_cffi_backend' module, which is version )cparserparsed_types	new_typesset_ffiRTLD_NULL)/_cffi_backend r   hasattr	Exceptionr   r   _backendr   _lockParser_parser_cached_btypestypes
ModuleType__dict___parsed_types
_new_types_function_caches
_libraries_cdefsources_included_ffis_windows_unicode_init_once_cache_cdef_version
_embeddingr   get_typecache
_typecacher   list
startswithsetattrgetattr_get_cached_btype
voidp_typeBVoidPchar_array_typeBCharAr
   r   castr   
_get_typesCDataCTypebuffer)selfbackendr   r   names        r   __init__zFFI.__init__)   sG    ? ,%""k17J/##X++W-=-=%? @ @
 $#Xw/B/B%D E E 	"_
~~' "--n=FF**;7@@ "  $ "!--g67I&OOD!))* 	<Dw'dGGT$:;	< ZZ 	H001A1ABDK001F1FGDK	H gu//03'99T[[!4'.'9'9';$	39 		$++q1DI%,%7%7%9"DJ
nn	H 	Hs   9A	KKFc                 .    | j                  ||||       y)a[  Parse 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.
        Alternatively, 'pack' can be a small integer, and requests for
        alignment greater than that are ignored (pack=1 is equivalent to
        packed=True).
        )overridepackedpackN)_cdef)r@   csourcerE   rF   rG   s        r   cdefzFFI.cdefe   s     	

7Xf4
Hr   c                 V    | j                  |||d       | j                  d| _        y y )NT)rF   rG   	dllexportr   )rH   r/   )r@   rI   rF   rG   s       r   embedding_apizFFI.embedding_apir   s,    

76
E??" DO #r   c                    t        |t              s,t        |t              st        d      |j	                  d      }| j
                  5  t               | _         | j                  j                  |fd|i| | j                  j                  |       |r!| j                  D ]  }|j                           | j                  j                  }|r*g | j                  _        |D ]  }|j                  | |        d d d        y # 1 sw Y   y xY w)Nz cdef() argument must be a stringasciirE   )r
   str
basestring	TypeErrorencoder   objectr.   r!   parser*   appendr(   clear_recompletefinish_backend_type)r@   rI   rE   optionscache
finishlisttps          r   rH   z	FFI._cdefw   s    '3'gz2 BCCnnW-GZZ 	=!'DDLLwEEWE$$W-!22 "EKKM"11J+-($ =B**4<=	= 	= 	=s   	B/DD
c                 <   t        |t              s#|!t        || j                        st        d      | j                  5  t        | ||      \  }}| j                  j                  |       | j                  j                  |       ddd       |S # 1 sw Y   S xY w)aH  Load 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.
        NzRdlopen(name): name must be a file name, None, or an already-opened 'void *' handle)	r
   rQ   r=   rR   r   _make_ffi_libraryr(   rV   r)   )r@   rB   flagslibfunction_caches        r   dlopenz
FFI.dlopen   s     4,4, C D DZZ 	("3D$"FC!!((8OO""3'	( 
		( 
s    ABBc                 8    t        |      j                  |       y)zClose 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)type__cffi_close__)r@   ra   s     r   dlclosezFFI.dlclose   s    
 	S	  %r   c                 @   |}|| j                   v r| j                   |   S t        |t              s|j                  d      }| j                  j                  |      }|j                  }|r|j                         }| j                  |      }||f}|| j                   |<   |S )NrO   )	r&   r
   rP   rS   r!   
parse_typeis_raw_functionas_function_pointerr6   )r@   cdeclkeyre   really_a_function_typebtyperesults          r   _typeof_lockedzFFI._typeof_locked   s    $$$$%%c**%%LL)E||&&u-!%!5!5!++-D&&t,.."(3r   c                     	 | j                   |   }\  }}|r|st	        d|d      |S # t        $ r6 | j                  5  | j                  |      }d d d        n# 1 sw Y   nxY wY Xw xY w)Nz	the type z3 is a function type, not a pointer-to-function type)r&   KeyErrorr   rq   r   )r@   rl   consider_function_as_funcptrrp   ro   rn   s         r   _typeofzFFI._typeof   s    	4''.F
 )/%%!*F:?B C C  	4 4,,U34 4 4	4s'   + A* A	A*A$	 A*)A*c                    t        |t              r| j                  |      S t        || j                        r| j                  j                  |      S t        |t        j                        rt        |      }||S t        |t        j                        r=t        |d      r1| j                  5  | j                  |j                        cddd       S t        t        |            # 1 sw Y   xY w)zParse the C type given as a string and return the
        corresponding <ctype> object.
        It can also be used on 'cdata' instance to get its C type.
        N_cffi_base_type)r
   rQ   ru   r=   r   typeofr#   BuiltinFunctionType_builtin_function_typeFunctionTyper   r   r6   rw   rR   re   )r@   rl   ress      r   rx   z
FFI.typeof   s    
 eZ(<<&&eTZZ(==''..eU667(/C
ue001E#45 E--e.C.CDE EU$$E Es   .C''C0c                     t        |t              r,| j                  |      }| j                  j	                  |      S | j                  j	                  |      S )zvReturn the size in bytes of the argument.  It can be a
        string naming a C type, or a 'cdata' instance.
        )r
   rQ   ru   r   sizeof)r@   rl   BTypes      r   r~   z
FFI.sizeof   sF     eZ(LL'E==''..==''..r   c                 z    t        |t              r| j                  |      }| j                  j	                  |      S )z\Return the natural alignment size in bytes of the C type
        given as a string.
        )r
   rQ   ru   r   alignof)r@   rl   s     r   r   zFFI.alignof   s1     eZ(LL'E}}$$U++r   c                 p    t        |t              r| j                  |      } | j                  |g| d   S )a3  Return 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.
        r   )r
   rQ   ru   _typeoffsetof)r@   rl   fields_or_indexess      r   offsetofzFFI.offsetof   s:     eZ(LL'E!t!!%<*;<Q??r   c                 |    t        |t              r| j                  |      }| j                  j	                  ||      S )a  Allocate 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 <cdata> object goes out of scope, the memory
        is freed.  In other words the returned <cdata> 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.
        )r
   rQ   ru   r   newp)r@   rl   inits      r   newzFFI.new   s3    . eZ(LL'E}}!!%..r   c                 r      j                   j                         }|j                  |||      d fd	}|S )a  Return 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.
        c                 X    t        | t              rj                  |       }  | |      S r	   r
   rQ   ru   )rl   r   	allocatorr@   s     r   allocatez#FFI.new_allocator.<locals>.allocate  s(    %,U+UD))r   r	   )r   r   new_allocator)r@   allocfreeshould_clear_after_alloccompiled_ffir   r   s   `     @r   r   zFFI.new_allocator  s;     }}((* ..ud/GI		* r   c                 |    t        |t              r| j                  |      }| j                  j	                  ||      S )zSimilar 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.
        )r
   rQ   ru   r   r;   )r@   rl   sources      r   r;   zFFI.cast%  s3    
 eZ(LL'E}}!!%00r   c                 :    | j                   j                  ||      S )a  Return 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.
        )r   string)r@   cdatamaxlens      r   r   z
FFI.string.  s      }}##E622r   c                 :    | j                   j                  ||      S )aL  Unpack 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)]
        )r   unpack)r@   r   lengths      r   r   z
FFI.unpack@  s     }}##E622r   c                     |t         u r| j                  |}}n!t        |t              r| j	                  |      }| j
                  j                  |||      S )a  Return a cdata of the given type pointing 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.

        The first argument is optional and default to 'char[]'.
        )_unspecifiedr:   r
   rQ   ru   r   from_buffer)r@   rl   python_bufferrequire_writables       r   r   zFFI.from_buffer^  sO     L(#';;=Ez*LL'E}}(()9; 	;r   c                 <    | j                   j                  |||      S )a_  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.
        )r   memmove)r@   destsrcns       r   r   zFFI.memmovep  s     }}$$T322r   c                 x      fd}t        t              r j                  d      ||S  ||      S )a  Return 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.
        c                 l    t        |       st        d      j                  j                  |       S )Nz.the 'python_callable' argument is not callable)callablerR   r   callback)python_callablerl   erroronerrorr@   s    r   callback_decorator_wrapz-FFI.callback.<locals>.callback_decorator_wrap  s=    O, !2 3 3==))%*/: :r   T)rt   r   )r@   rl   r   r   r   r   s   `` `` r   r   zFFI.callback  s=    	: eZ(LLTLJE"***?;;r   c                 "   t        |t              r| j                  |      }|j                         }|j	                  d      r$d| j
                  j                  |d      v rd|z  }n|r|d   dvrd|z   }| j
                  j                  ||      S )a.  Return a string giving the C type 'cdecl', which may be itself
        a string or a <ctype> 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'.
        *z&[&z(%s)r   z[( )r
   rQ   ru   stripr3   r   getcname)r@   rl   replace_withs      r   getctypezFFI.getctype  s     eZ(LL'E#))+##C(DMM225#>>!L0L,q/T"9-L}}%%e\::r   c                 <    | j                   j                  |||      S )a  Return 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.
        )r   gcp)r@   r   
destructorsizes       r   gczFFI.gc  s     }}  
D99r   c                     | j                   j                  d      du sJ 	 | j                  |   }|S # t        $ r1 g }|j	                  | |      }|D ]  }|j                  | |        Y |S w xY w)NF)r   acquirer"   rs   get_cached_btyperY   )r@   re   r   r\   s       r   r6   zFFI._get_cached_btype  s    zz!!%(E111	;''-E   	;J))$
;E" ;((z:;	;s   2 6A,+A,c                     ddl m}m} | j                  r| j	                  |       |xs  |       } || ||fi || _         | j                   j                         }| j                  j                  |       |S )a  Verify 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.
        r   )Verifier_caller_dir_pycache)verifierr   r   r,   _apply_windows_unicodeload_libraryr)   rV   )r@   r   tmpdirkwargsr   r   ra   s          r   verifyz
FFI.verify  sr     	<   ''/
 0.0 !vv@@mm((* 	s#
r   c                 6    | j                   j                         S r	   )r   	get_errno)r@   s    r   
_get_errnozFFI._get_errno  s    }}&&((r   c                 :    | j                   j                  |       y r	   )r   	set_errno)r@   errnos     r   
_set_errnozFFI._set_errno  s    &r   z(the value of 'errno' from/to the C callsc                 8    | j                   j                  |      S r	   )r   getwinerror)r@   codes     r   r   zFFI.getwinerror  s    }}((..r   c                 r    | j                   5  t        j                  | |      cd d d        S # 1 sw Y   y xY wr	   )r   r   pointer_cache)r@   ctypes     r   _pointer_tozFFI._pointer_to  s.    ZZ 	4&&tU3	4 	4 	4s   -6c                    	 | j                   j                  |      }|r | j                  |g| \  }}n|j                  dk(  rt        d      d}| j                  |      }| j                   j                  |||      S # t        $ r7 dt        |      j                  v r t        |      j
                  |g| cY S  w xY w)zReturn the address of a <cdata 'struct-or-union'>.
        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.
        __addressof__pointerzaddressof(pointer)r   )
r   rx   rR   re   r%   r   r   kindr   rawaddressof)r@   r   r   r   offsetctypeptrs         r   	addressofzFFI.addressof  s    	MM((/E
 .D..uI7HIME6zzY& 455F##E*}}))(E6BB  	$u+"6"660tE{00K9JKK	s   B   <C >C c                     | j                   j                  ||      \  }}|D ]'  }| j                   j                  ||d      \  }}||z  }) ||fS )Nr   )r   typeoffsetof)r@   r   field_or_indexr   r   field1offset1s          r   r   zFFI._typeoffsetof  s]    225.Iv' 	F!]]77vqINE7gF	 f}r   c                 >   t        |t              s!t        dt        |      j                        || u rt        d      |j                  5  | j                  5  | j                  j                  |j                         | j                  j                  d       | j                  j                  |j                         | j                  j                  d       | j                  j                  |       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)a  Includes 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.
        zEffi.include() expects an argument that is also of type cffi.FFI, not zself.include(self)[]N)r
   r   rR   re   __name__
ValueErrorr   r!   includer*   rV   extendr+   )r@   ffi_to_includes     r   r   zFFI.include  s     .#. $^ 4 = =@ A A T!122!! 	; ;$$^%;%;<!!((-!!(()D)DE!!((-##**>:;	; 	;; ;	; 	;s%   DBD6DD	DDc                 N    | j                   j                  | j                  |      S r	   )r   newp_handler8   r@   r   s     r   
new_handlezFFI.new_handle  s    }}((a88r   c                 8    | j                   j                  |      S r	   )r   from_handler   s     r   r   zFFI.from_handle  s    }}((++r   c                 :    | j                   j                  |       y r	   )r   releaser   s     r   r   zFFI.release"  s    a r   c                     | j                   t        d      t        |      }|r| j                  d       || _         y| j                  d       || _         y)ac  Windows: 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.
        Nz%set_unicode() can only be called onceztypedef 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;ztypedef 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;)r,   r   boolrJ   )r@   enabled_flags     r   set_unicodezFFI.set_unicode%  sa       ,DEEL)II / 0" !- II / 0 !-r   c                     |j                  dd      }t        |t        t        f      st	        d      t        |      ddgz   }||d<   y )Ndefine_macros z''define_macros' must be a list or tuple)UNICODE1)_UNICODEr   )getr
   r2   tuplerR   )r@   kwds	defmacross      r   r   zFFI._apply_windows_unicodeC  sO    HH_b1	)dE]3EFFO'7'8': :	 )_r   c                     fd}dt         j                  v rdd l}t         j                  dk(  radj	                  t         j
                        }t        t         d      r |d|j                  j                  t         j                  d             nYt         j
                  d	k  rd
}nd}t        t         d      r1 |d|j                  j                  t         j                  d             t        t         d      r |d|j                  j                  t         j                  dd             nt         j                  dk(  rd}t        t         d      r2|dz  }n,	 dd l
}d}|j                  d      r||j                  d      z  }|t         j                  dz	  t         j                  dz	  dz  fz  }t        t         d      r|t         j                  z  } |d|       t         j                  dk(  r
 |dd       y y # t        $ r	 ddlm
} Y w xY w)Nc                 V    j                  | g       }||vr|j                  |       y y r	   )
setdefaultrV   )rm   valuelstr   s      r   ensurez(FFI._apply_embedding_fix.<locals>.ensureM  s,    //#r*CC

5!  r   __pypy__r   win32zpython{0[0]}{0[1]}prefixlibrary_dirslibs   zpypy-czpypy3-cbinpypygoalz
python%d%dgettotalrefcount_d)	sysconfigzpython%d.%d	DEBUG_EXT         abiflags	librariesextra_link_argsz	/MANIFEST)sysbuiltin_module_namesosplatformformatversion_infor   pathjoinr	  r  ImportErrorcffi._shimmed_dist_utilsget_config_var
hexversionr  )r@   r   r  r  	pythonlibtemplater  s    `     r   _apply_embedding_fixzFFI._apply_embedding_fixK  s   	"
 111||w& 1778H8HI	3)>277<<

F+KL
 ##d* (I )I3)>277<<

E+JK sH%~rww||CJJ'OP||w&'3 23$HC$ )++K8	 8 8 EEH!^^r)CNNb,@D+HIJIsJ'S\\)	{I&<<7"$k2 # # CBCs   G; ;HHc                     dd l }t        | d      rt        d      t        |t              st        d      |j                  |v s|j                  r|j                  |v rt        d      t        |      |||f| _	        y )Nr   _assigned_sourcez:set_source() cannot be called several times per ffi objectz'module_name' must be a stringzY'module_name' must not contain '/': use a dotted name to make a 'package.module' location)
r  r   r   r
   rQ   rR   sepaltseprP   r+  )r@   module_namer   source_extensionr   r  s         r   
set_sourcezFFI.set_source}  s    4+, . / /+z2<==66[ RYY2993K H I I!$[!16!14!9r   c                     ddl m} t        |t              st	        d      |j                  |      }|j                  ||        | j                  |||fi | y )Nr   )	pkgconfigz;the pkgconfig_libs argument must be a list of package names)r   r2  r
   r2   rR   flags_from_pkgconfigmerge_flagsr0  )r@   r.  pkgconfig_libsr   r/  r   r2  kwds2s           r   set_source_pkgconfigzFFI.set_source_pkgconfig  sY    .$/ / 0 0..~>dE*V-=FFr   c                    ddl m} ddlm} t	        | d      s1t	        | d      r| j
                  j                         S t        d      | j                  \  }}}}|t        d       ||        || ||f|||d	d
|\  }	}
|rd|
r2t        j                  j                  d|	j                  d   d       |	S t        j                  j                  d|	j                  d   d       |	S )Nr   )mkpathr   	recompiler+  r   z8set_source() must be called before distutils_extension()zadistutils_extension() is only for C extension modules, not for dlopen()-style pure Python modulesF)r   extradirr/  call_c_compilerzregenerated: 
znot modified: )r$  r9  
recompilerr;  r   r   get_extensionr   r+  rR   r  stderrwritesources)r@   r   verboser9  r;  r.  r   r/  r   extupdateds              r   distutils_extensionzFFI.distutils_extension  s    3)t/0tZ(}}2244 6 7 76:6K6K3V-t> & ' ' 	v {!'@062B16@ ;?@W 

  A!HI 
 

  Q!IJ
r   c                     ddl m} t        | d      st        d      | j                  \  }}}}|t        d       || ||f|ddd| y )Nr   r:  r+  0set_source() must be called before emit_c_code()zYemit_c_code() is only for C extension modules, not for dlopen()-style pure Python modulesFc_filer=  uses_ffiplatformr?  r;  r   r   r+  rR   r@   filenamer;  r.  r   r/  r   s          r   emit_c_codezFFI.emit_c_code  sn    )t/0OPP6:6K6K3V-t> I J J$V 	2!5#(	2,0	2r   c                     ddl m} t        | d      st        d      | j                  \  }}}}|t        d       || ||f|ddd| y )Nr   r:  r+  rI  z^emit_python_code() is only for dlopen()-style pure Python modules, not for C extension modulesFrJ  rM  rN  s          r   emit_python_codezFFI.emit_python_code  so    )t/0OPP6:6K6K3V-t O P P$V 	2!5#(	2,0	2r   c           	          ddl m} t        | d      st        d      | j                  \  }}}}	 || ||f|||||d|	S )a  The 'target' argument gives the final file name of the
        compiled DLL.  Use '*' to force distutils' choice, suitable for
        regular CPython C API modules.  Use a file name ending in '.*'
        to ask for the system's default extension for dynamic libraries
        (.so/.dll/.dylib).

        The default is '*' when building a non-embedded C API extension,
        and (module_name + '.*') when building an embedded library.
        r   r:  r+  z,set_source() must be called before compile())r   targetr/  compiler_verbosedebug)r?  r;  r   r   r+  )
r@   r   rD  rT  rV  r;  r.  r   r/  r   s
             r   compilezFFI.compile  sd     	*t/0KLL6:6K6K3V-t{F H6 &9I*1HBFH 	Hr   c                 N   	 | j                   |   }|d   r|d   S |d   5  | j                   |   }|d   r|d   cd d d        S  |       }d|f| j                   |<   d d d        |S # t        $ r) | j                   j                  |dt               f      }Y w xY w# 1 sw Y   S xY w)NFr   r   T)r-   rs   r  r   )r@   functagr   rp   s        r   	init_oncezFFI.init_once  s    	P%%c*A Q4Q4KqT 	8%%c*Att		8 	8 VF*.D!!#&	8   	P%%00umo6NOA	P	8 s"   A% BB%/BBB$c                 >   | j                   rt        d      dd l}|j                  d|      }|r||j	                         d  }|j                         xs dg}|j                  d|d         j                         }t        dt        |            D ]A  }||   }|j                         s|j                  |      r+|d d }|j                  |      sC t        |      }|D cg c]
  }||d  dz    }}dj                  |      }t        |d	d
       || _         y c c}w )Nz-embedding_init_code() can only be called oncer   z\s*\nr   z\s*r   r>  	cffi_initexec)r/   r   rematchend
splitlinesgrouprangelenrstripr3   r"  rW  )r@   pysourcer`  ra  linesr	  ilines           r   embedding_init_codezFFI.embedding_init_code  s   ??LMM
 	8,		-H##%-"&%(+113q#e*% 	)A8D{{}//&1#CR[F //&1	)
 K+014ab$11775>+v." 2s   #Dc                     t        d      )Nz:ffi.def_extern() is only available on API-mode FFI objects)r   )r@   argsr   s      r   
def_externzFFI.def_extern  s     # $ 	$r   c                    g }g }g }| j                   j                  D ]t  }|j                  d      r|j                  |dd        )|j                  d      r|j                  |dd        O|j                  d      sa|j                  |dd        v |j	                          |j	                          |j	                          |||fS )zReturns the user type names known to this FFI instance.
        This returns a tuple containing three lists of names:
        (typedef_names, names_of_structs, names_of_unions)
        ztypedef    Nzstruct    zunion    )r!   _declarationsr3   rV   sort)r@   typedefsstructsunionsrm   s        r   
list_typeszFFI.list_types  s    
 <<-- 	'C~~j)AB(	*s12w')c!"g&	' 	'6**r   r	   )FFN)FN)F)r   )NNT)r]  )NNN)r   )r   N)z.c)buildT).r   NN)6r   
__module____qualname____doc__rC   rJ   rM   rH   rc   rg   rq   ru   rx   r~   r   r   r   r   r;   r   r   r   r   r   r   r   r   r6   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   r)  r0  r7  rG  rP  rR  rW  r[  rl  ro  ry  r   r   r   r   r      s"   ":%xI!
=$$&$%&/,	@/6 .2/3013$3< 0<%*;$3 <*; 
:
<)'ZT?AE/4C*;.9,!-<*03d9 /3G422H&0#4$+r   r   c                    dd l }t        |t              s)t        j                  dk7  s|| j                  ||      S d}d }d|v sd|v s|j                  |v r	 | j                  ||      S dd l}|j                  j                  |      }|O|dk(  r1t        j                  dk(  rt        j                  dk\  rt        d      d|}||d	|}t        |      | j                  ||      S # t        $ r}|}Y d }~d }~ww xY w)
Nr   r  cr{  /r  zXdlopen(None) cannot work on Windows for Python 3 (see http://bugs.python.org/issue23606)zEctypes.util.find_library() did not manage to locate a library called z.  Additionally, )r  r
   rQ   r  r  r   r,  OSErrorctypes.utilutilfind_libraryr   )	rA   rB   r`   r  first_errorectypesr!  msgs	            r   _load_backend_libr  &  s   dJ'<<7"d&6''e44K
d{cTkRVVt^	''e44 ;;##D)D|3;3<<72s7G7G47O D E E 379"-8#>Ccle,,  	K	s   C+ +	D 4C;;D c           
         	
  j                   }t        |||       fd fd fdd  fdi 	dg
i 	
 fd	 fd G 	 fd	d
t              t        |t              r,	 t        |t
              s|j                  d      }d|z  _                j                  fS # t        $ r Y  w xY w)Nc                     d| z   }j                   j                  |   \  }}j                  |      }j                  ||       }|j                  | <   y )Nz	function )r!   rt  r6   load_functionr%   )	rB   rm   r]   _r   r  
backendlibffilibrarys	         r   accessor_functionz,_make_ffi_library.<locals>.accessor_functionD  sW    D ))#.A%%b)((5!&r   c           
           d z   }	j                   j                  |   \  }}	j                  |      j                  j                  t         t         fd fd             y )N	variable c                            S r	   r   )r@   r   rB   read_variables    r   r   z>_make_ffi_library.<locals>.accessor_variable.<locals>.<lambda>R  s    ud3 r   c                      |      S r	   r   )r@   r  r   rB   write_variables     r   r   z>_make_ffi_library.<locals>.accessor_variable.<locals>.<lambda>S  s    udE B r   )r!   rt  r6   r  r  r4   r  )
rB   rm   r]   r  r   r  r  
FFILibraryr  r  s
   `   @@@r   accessor_variablez,_make_ffi_library.<locals>.accessor_variableK  sf    D ))#.A%%b)"00#22
D(3B#D 	Er   c                 `   	 |    S # t         $ r j                  5  | vrnd| z   }j                  j                  |   \  }}j	                  |      }|j
                  dk7  rt        j                  |      }j                  ||       }|| <   d d d        n# 1 sw Y   nxY w|    cY S w xY w)Nr  array)	rs   r   r!   rt  r6   r   r   r   r  )	rB   rm   r]   r  r   paddr_variablesr  r  s	         r   addressof_varz(_make_ffi_library.<locals>.addressof_varU  s    	(!$'' 
	( -~-%,CKK55c:EB11"5EzzW, % 3 3C ?"00=A+,N4(- - - "$''
	(s'    B-A3B	B-B"	B-,B-c                      t        d| d      )Nznon-integer constant 'z,' cannot be accessed from a dlopen() library)NotImplementedError)rB   s    r   accessor_constantz,_make_ffi_library.<locals>.accessor_constantd  s    !HL#O P 	Pr   c                 R    j                   j                  |    j                  | <   y r	   )r!   _int_constantsr%   )rB   r  r  s    r   accessor_int_constantz0_make_ffi_library.<locals>.accessor_int_constanth  s"    !$!;!;D!Ar   Fc                     d   j                   u ry j                  j                  j                         D ]  \  } \  }}t	        |t
        j                        s7| j                  dd      \  }}|dk(  r	|<   C|dk(  r|<   N|dk(  sT|<   Zt        |j                        D ]  \  }}||ffd	}||<     j                  j                  D ]  }j                  |
        j                   d<   y )Nr   r   r   functionvariableconstantc                 ^    |j                          |j                  |   j                  | <   y r	   )check_not_partial
enumvaluesr%   )rB   r]   rj  r  s      r   accessor_enumzB_make_ffi_library.<locals>.update_accessors.<locals>.accessor_enum~  s(    ,,.13q1A((.r   )r.   r!   rt  itemsr
   r   EnumTypesplit	enumerateenumeratorsr  r  )rm   r]   r  rZ  rB   rj  enumnamer  r  r  r  r  	accessorsaccessors_versionr  r  s           r   update_accessorsz+_make_ffi_library.<locals>.update_accessorso  s   Q3#4#44KK55;;= 	8LC"ab%..1IIc1-	T*$&7IdOJ&&7IdOJ&&7IdO#,R^^#< 8KAx/1Q B +8Ih'	8	8 KK.. 	>D  '<=	>"00!r   c                     j                   5  | j                  v s| j                  v r
	 d d d        y | vr         | vrt        |        |    |        d d d        y # 1 sw Y   y xY wr	   )r   r%   AttributeError)rB   r  r  r  r  r  s    r   make_accessorz(_make_ffi_library.<locals>.make_accessor  sx    YY 	"w'''4:3F3F+F	" 	" 9$ "y((..IdOD!	" 	" 	"s   A#%A##A,c                   J    e Zd ZfdZfdZfdZ fdZfdZy)%_make_ffi_library.<locals>.FFILibraryc                 ,     |       t        | |      S r	   )r5   )r@   rB   r  s     r   __getattr__z1_make_ffi_library.<locals>.FFILibrary.__getattr__  s    $4&&r   c                     	 t        | j                  |      }|j                  | |       y # t        $ r  |       t	        | ||       Y y w xY wr	   )r5   	__class____set__r  r4   )r@   rB   r  r  r  s       r   __setattr__z1_make_ffi_library.<locals>.FFILibrary.__setattr__  sO    ."4>>48
   u-	 " +d#dE*+s   , AAc                 v    j                   5           j                         cd d d        S # 1 sw Y   y xY wr	   )r   keys)r@   r  r  r  s    r   __dir__z-_make_ffi_library.<locals>.FFILibrary.__dir__  s/     ( " ~~'( ( (s   /8c                     |j                   v rj                   |   S |j                   v r |      S  |       |j                   v rj                   |   S |j                   v r |      S t        d|d      )Nz7cffi library has no function or global variable named '')r%   r  )r@   rB   r  r  r  r  s     r   r   z3_make_ffi_library.<locals>.FFILibrary.__addressof__  s    w'''''--z***$T**$w'''''--z***$T** AE"H I Ir   c                 Z    j                          | j                  j                          y r	   )	close_libr%   rW   )r@   r  s    r   rf   z4_make_ffi_library.<locals>.FFILibrary.__cffi_close__  s      "MM!r   N)r   r|  r}  r  r  r  r   rf   )r  r  r  r  r  r  r  r  s   r   r  r    s    	'	.	(	I	"r   r  zutf-8zFFILibrary_%s)
r   r  rT   r
   rQ   rP   rS   r   UnicodeErrorr%   )r  libnamer`   rA   r  r  r  r  r  r  r  r  r  r  r  r  r  s   `   @@@@@@@@@@@@@r   r_   r_   @  s    llG"7GU;J'E(PB IN1 1." "" "V "@ ':&	gs+!..1"1G";J lGG$$$$  		s   +C 	CCc                    dd l }	 |j                  | j                     }|j                  }|j                  }||    }|j
                  5  |j                  |      cd d d        S # 1 sw Y   y xY w# t        t        t        f$ r Y y w xY w)Nr   )
r  modulesr|  _cffi_original_ffi_cffi_types_of_builtin_funcsr   r6   rs   r  rR   )rY  r  moduler  types_of_builtin_funcsr]   s         r   rz   rz     s     	-T__-''!'!D!D#D) YY 	-((,	- 	- 	- ni0 s   6A/ A##A,/BB)r  r#   lockr   r   r   r   r   r   	NameErrorcollectionsr   rQ   rP   rT   r   r   r  r_   rz   r   r   r   <module>r     sy       1
 xL+& L+^-4x%t-e  1$0H1  Js    = A AAAA