o
    @cwH                     @   s(  d Z ddlZddlZddlZdZddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ eekrJedeeejdkrUddlmZ eZejdkrqejdkrqee jdd dk rqeZddlmZ m!Z"m#Z$m%Z& d}ddZ'd}ddZ(i Z)dd Z*ejdkrddlm+Z, ddlm-Z. i Z/dd Z0e0j re*j 1dde0_ nejdkrddlm2Z, ddlm3Z3m4Z4m5Z5m6Z6m7Z7 ddlm8Z8m9Z9 dd lm:Z: d}d!d"Z;G d#d$ d$e:Z<e;e<d% G d&d' d'e:Z=e;e= G d(d) d)e:Z>e;e> G d*d+ d+e:Z?e;e? G d,d- d-e:Z@e;e@ ed.ed/kr'e?ZAe@ZBnG d0d1 d1e:ZAe;eA G d2d3 d3e:ZBe;eB G d4d5 d5e:ZCe;eC G d6d7 d7e:ZDe;eD G d8d9 d9e:ZEe3eEe3eDkrjeDZEed/ed:krxe?ZFe@ZGnG d;d< d<e:ZFe;eF G d=d> d>e:ZGe;eG G d?d@ d@e:ZHeH eH_IeH_Je;eH G dAdB dBe:ZKeK eK_IeK_Je;eK G dCdD dDe:ZLeL eL_IeL_Je;eL G dEdF dFe:ZMe;eMd% G dGdH dHe:ZNeNZOe;eN G dIdJ dJe:ZPddKlmQZQmRZRmSZS G dLdM dMe:ZTG dNdO dOe:ZUdPdQ ZVd}dRdSZWdTdU ZXdVdW ZYG dXdY dYeZZ[G dZd[ d[e[Z\ejdkrJG d\d] d]e[Z]dd^lm^Z^m:Z: G d_d` d`e:Z_G dadb dbe[Z`G dcdd ddeZZaeae[Zbeae\Zcejdkrhe\dedejdZenejdfkrze\dgejfddh  Zene\dZeejdkreae]Zgeae`ZhegjijjZjddilmkZkmlZl d~djdkZme3eBe3eNkreBZneAZone3e@e3eNkre@Zne?Zone3eGe3eNkreGZneFZoddllmpZpmqZqmrZrmsZs e*eNeNeNenepZte*eNeNeAeneqZudmdn Zveve<eNe<e<esZwdodp Zxeve<eNeAerZyddrdsZzzddtlm{Z{ W n
 e|y   Y nw eve<eNeAe{Z}ddudvZ~ejdkr0dwdx Zdydz Zdd{lmZmZ eKZeHZe=eAe?eFfD ]"Ze3edhkrOeZqBe3ed|krZeZqBe3edkrceZqBe>eBe@eGfD ]"Ze3edhkrxeZqke3ed|kreZqke3edkreZqk[eV  dS )z,create and manipulate C data types in Python    Nz1.1.0)Union	StructureArray)_Pointer)CFuncPtr)__version__)
RTLD_LOCALRTLD_GLOBAL)ArgumentErrorcalcsizezVersion number mismatchnt)FormatErrorposixdarwin.   )FUNCFLAG_CDECLFUNCFLAG_PYTHONAPIFUNCFLAG_USE_ERRNOFUNCFLAG_USE_LASTERRORc                 C   sv   t | tr"|du rt| d }td| | t| }| }| |_|S t | tr7tdd|  t|  }| }|S t| )zcreate_string_buffer(aBytes) -> character array
    create_string_buffer(anInteger) -> character array
    create_string_buffer(aBytes, anInteger) -> character array
    N   zctypes.create_string_buffer)	
isinstancebyteslen_sysauditc_charvalueint	TypeErrorinitsizebuftypebuf r&   MC:\Users\Chris\AppData\Local\Programs\Python\Python310\lib\ctypes\__init__.pycreate_string_buffer0   s   

r(   c                 C   
   t | |S N)r(   )r"   r#   r&   r&   r'   c_bufferD   s   
r+   c                       t |ddrtO |ddrtO |r td|  zt f W S  tyF   G  fdddt}|t f< | Y S w )a  CFUNCTYPE(restype, *argtypes,
                 use_errno=False, use_last_error=False) -> function prototype.

    restype: the result type
    argtypes: a sequence specifying the argument types

    The function prototype can be called in different ways to create a
    callable object:

    prototype(integer address) -> foreign function
    prototype(callable) -> create and return a C callable function from callable
    prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method
    prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal
    prototype((function name, dll object)[, paramflags]) -> foreign function exported by name
    	use_errnoFuse_last_error!unexpected keyword argument(s) %sc                          e Zd Z ZZZdS )z CFUNCTYPE.<locals>.CFunctionTypeN__name__
__module____qualname__
_argtypes_	_restype__flags_r&   argtypesflagsrestyper&   r'   CFunctionTypef       r<   )	_FUNCFLAG_CDECLpop_FUNCFLAG_USE_ERRNO_FUNCFLAG_USE_LASTERROR
ValueErrorkeys_c_functype_cacheKeyError	_CFuncPtr)r;   r9   kwr<   r&   r8   r'   	CFUNCTYPEL   s   rH   )LoadLibrary)FUNCFLAG_STDCALLc                    r,   )Nr-   Fr.   r/   c                       r0   )z$WINFUNCTYPE.<locals>.WinFunctionTypeNr1   r&   r8   r&   r'   WinFunctionType~   r=   rK   )	_FUNCFLAG_STDCALLr?   r@   rA   rB   rC   _win_functype_cacherE   rF   )r;   r9   rG   rK   r&   r8   r'   WINFUNCTYPEr   s   rN   )dlopen)sizeofbyref	addressof	alignmentresize)	get_errno	set_errno)_SimpleCDatac                 C   sJ   ddl m} |d u r| j}t| ||}}||kr#td| ||f d S )Nr   r   z"sizeof(%s) wrong: %d instead of %d)structr   _type_rP   SystemError)typtypecoder   actualrequiredr&   r&   r'   _check_size   s   r_   c                       s    e Zd ZdZ fddZ  ZS )	py_objectOc                    s.   zt   W S  ty   dt| j  Y S w )Nz
%s(<NULL>))super__repr__rB   typer2   self	__class__r&   r'   rc      s
   zpy_object.__repr__)r2   r3   r4   rY   rc   __classcell__r&   r&   rg   r'   r`      s    r`   Pc                   @      e Zd ZdZdS )c_shorthNr2   r3   r4   rY   r&   r&   r&   r'   rl          rl   c                   @   rk   )c_ushortHNrn   r&   r&   r&   r'   rp      ro   rp   c                   @   rk   )c_longlNrn   r&   r&   r&   r'   rr      ro   rr   c                   @   rk   )c_ulongLNrn   r&   r&   r&   r'   rt      ro   rt   irs   c                   @   rk   )c_intrv   Nrn   r&   r&   r&   r'   rw      ro   rw   c                   @   rk   )c_uintINrn   r&   r&   r&   r'   rx      ro   rx   c                   @   rk   )c_floatfNrn   r&   r&   r&   r'   rz      ro   rz   c                   @   rk   )c_doubledNrn   r&   r&   r&   r'   r|      ro   r|   c                   @   rk   )c_longdoublegNrn   r&   r&   r&   r'   r~      ro   r~   qc                   @   rk   )
c_longlongr   Nrn   r&   r&   r&   r'   r      ro   r   c                   @   rk   )c_ulonglongQNrn   r&   r&   r&   r'   r      ro   r   c                   @   rk   )c_ubyteBNrn   r&   r&   r&   r'   r      ro   r   c                   @   rk   )c_bytebNrn   r&   r&   r&   r'   r      ro   r   c                   @   rk   )r   cNrn   r&   r&   r&   r'   r      ro   r   c                   @      e Zd ZdZdd ZdS )c_char_pzc                 C      d| j jt| jf S Nz%s(%s)rh   r2   c_void_pfrom_bufferr   re   r&   r&   r'   rc         zc_char_p.__repr__Nr2   r3   r4   rY   rc   r&   r&   r&   r'   r          r   c                   @   rk   )r   rj   Nrn   r&   r&   r&   r'   r      ro   r   c                   @   rk   )c_bool?Nrn   r&   r&   r&   r'   r      ro   r   )POINTERpointer_pointer_type_cachec                   @   r   )	c_wchar_pZc                 C   r   r   r   re   r&   r&   r'   rc      r   zc_wchar_p.__repr__Nr   r&   r&   r&   r'   r      r   r   c                   @   rk   )c_wcharuNrn   r&   r&   r&   r'   r     ro   r   c                   C   sF   t   t  tjdkrt  tjtt	_t
jtt_tt d < d S )Nr   )r   clearrD   _osnamerM   r   
from_paramr   r   r   r   r   r&   r&   r&   r'   _reset_cache  s   
r   c                 C   s   t | tr4|du r!ttdkrtdd | D d }nt| d }td| | t| }| }| |_|S t | t	rItdd|  t|  }| }|S t
| )zcreate_unicode_buffer(aString) -> character array
    create_unicode_buffer(anInteger) -> character array
    create_unicode_buffer(aString, anInteger) -> character array
    N   c                 s   s$    | ]}t |d krdndV  qdS )i  r   r   N)ord).0r   r&   r&   r'   	<genexpr>  s   " z(create_unicode_buffer.<locals>.<genexpr>r   zctypes.create_unicode_buffer)r   strrP   r   sumr   r   r   r   r   r    r!   r&   r&   r'   create_unicode_buffer  s    

r   c                 C   sL   t |d d urtdt| t vrtd| | | t |< t t| = d S )Nz%This type already exists in the cachezWhat's this???)r   getRuntimeErroridset_type)r   clsr&   r&   r'   SetPointerType/  s   
r   c                 C   s   | | S r*   r&   )r[   r   r&   r&   r'   ARRAY9  s   r   c                   @   sP   e Zd ZdZeZeZdZdZ	dZ
eddddfddZdd	 Zd
d Zdd ZdS )CDLLa  An instance of this class represents a loaded dll/shared
    library, exporting functions using the standard C calling
    convention (named 'cdecl' on Windows).

    The exported functions can be accessed as attributes, or by
    indexing with the function name.  Examples:

    <obj>.qsort -> callable object
    <obj>['qsort'] -> callable object

    Calling the functions releases the Python GIL during the call and
    reacquires it afterwards.
    z<uninitialized>r   NFc           	         s   |_ j |r tO  |r tO  tjdr,	 |r,|dr,d|v r,|tj	tj
B O }tjdkrS|d ur8|}ndd l}|j}d|v sGd|v rS|j _ ||jO }G  fdd	d	t}|_|d u rntj |_d S |_d S )
Naix)z.a(r   r   /\c                       s   e Zd Z ZjZdS )zCDLL.__init__.<locals>._FuncPtrN)r2   r3   r4   r7   _func_restype_r6   r&   r:   rf   r&   r'   _FuncPtrp  s    
r   )_name_func_flags_r@   rA   r   platform
startswithendswithr   RTLD_MEMBERRTLD_NOWr   r   !_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS_getfullpathname!_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIRrF   r   _dlopen_handle)	rf   r   modehandler-   r.   winmoder   r   r&   r   r'   __init__T  s.   


zCDLL.__init__c                 C   s8   d| j j| j| jtjd d @ t| tjd d @ f S )Nz<%s '%s', handle %x at %#x>r   r   )rh   r2   r   r   r   maxsizer   re   r&   r&   r'   rc   z  s   
zCDLL.__repr__c                 C   s6   | dr|drt|| |}t| || |S )N__)r   r   AttributeError__getitem__setattr)rf   r   funcr&   r&   r'   __getattr__  s
   
zCDLL.__getattr__c                 C   s"   |  || f}t|ts||_|S r*   )r   r   r   r2   )rf   name_or_ordinalr   r&   r&   r'   r     s   
zCDLL.__getitem__)r2   r3   r4   __doc__r>   r   rw   r   r   r   r   DEFAULT_MODEr   rc   r   r   r&   r&   r&   r'   r   ?  s    
&r   c                   @   s   e Zd ZdZeeB ZdS )PyDLLzThis class represents the Python library itself.  It allows
    accessing Python API functions.  The GIL is not released, and
    Python exceptions are handled correctly.
    N)r2   r3   r4   r   r>   _FUNCFLAG_PYTHONAPIr   r&   r&   r&   r'   r     s    r   c                   @      e Zd ZdZeZdS )WinDLLznThis class represents a dll exporting functions using the
        Windows stdcall calling convention.
        N)r2   r3   r4   r   rL   r   r&   r&   r&   r'   r     s    r   )_check_HRESULTrW   c                   @   r   )HRESULTrs   N)r2   r3   r4   rY   r   _check_retval_r&   r&   r&   r'   r     s    
r   c                   @   s   e Zd ZdZeZeZdS )OleDLLzThis class represents a dll exporting functions using the
        Windows stdcall calling convention, and returning HRESULT.
        HRESULT error values are automatically raised as OSError
        exceptions.
        N)r2   r3   r4   r   rL   r   r   r   r&   r&   r&   r'   r     s    r   c                   @   s6   e Zd Zdd Zdd Zdd Zdd Zeej	Z
d	S )
LibraryLoaderc                 C   s
   || _ d S r*   _dlltype)rf   dlltyper&   r&   r'   r        
zLibraryLoader.__init__c                 C   s.   |d dkr
t || |}t| || |S )Nr   _)r   r   r   )rf   r   dllr&   r&   r'   r     s
   
zLibraryLoader.__getattr__c                 C   r)   r*   )getattrrf   r   r&   r&   r'   r     r   zLibraryLoader.__getitem__c                 C   s
   |  |S r*   r   r   r&   r&   r'   rI     r   zLibraryLoader.LoadLibraryN)r2   r3   r4   r   r   r   rI   classmethod_typesGenericAlias__class_getitem__r&   r&   r&   r'   r     s    r   z
python dllcygwinzlibpython%d.%d.dllr   )get_last_errorset_last_errorc                 C   s0   | d u rt  } |d u rt|  }td |d | S r*   )GetLastErrorr   stripOSError)codedescrr&   r&   r'   WinError  s
   r   )_memmove_addr_memset_addr_string_at_addr
_cast_addrc                    s   G  fdddt }|S )Nc                       s   e Zd Z ZZeeB ZdS )z!PYFUNCTYPE.<locals>.CFunctionTypeN)r2   r3   r4   r5   r6   r>   r   r7   r&   r9   r;   r&   r'   r<     s    r<   )rF   )r;   r9   r<   r&   r   r'   
PYFUNCTYPE  s   r   c                 C   s   t | | |S r*   )_cast)objr[   r&   r&   r'   cast  s   r   c                 C   r)   )zAstring_at(addr[, size]) -> string

    Return the string at addr.)
_string_atptrr#   r&   r&   r'   	string_at     
r   )_wstring_at_addrc                 C   r)   )zFwstring_at(addr[, size]) -> string

        Return the string at addr.)_wstring_atr   r&   r&   r'   
wstring_at  r   r  c                 C   s<   zt dt t dg}W n
 ty   Y dS w || ||S )Ncomtypes.server.inprocserver*i)
__import__globalslocalsImportErrorDllGetClassObject)rclsidriidppvccomr&   r&   r'   r    s   r  c                  C   s4   zt dt t dg} W |  S  ty   Y dS w )Nr  r  r   )r  r  r  r  DllCanUnloadNow)r  r&   r&   r'   r    s   r  )BigEndianStructureLittleEndianStructure   r*   )NN)r   )r   osr   sysr   typesr   r   _ctypesr   r   r   r   r   rF   _ctypes_versionr   r	   r
   rX   r   	_calcsize	Exceptionr   r   r   r   r   unamereleasesplitr   r>   r   r   r   r@   r   rA   r(   r+   rD   rH   rI   r   rJ   rL   rM   rN   replacerO   rP   rQ   rR   rS   rT   rU   rV   rW   r_   r`   rl   rp   rr   rt   rw   rx   rz   r|   r~   r   r   r   __ctype_le____ctype_be__r   r   r   r   c_voidpr   r   r   r   r   r   r   r   r   r   objectr   r   r   r   r   r   r   cdllpydll	dllhandle	pythonapiversion_infowindlloledllkernel32r   r   r   r   c_size_t	c_ssize_tr   r   r   r   memmovememsetr   r   r   r   r   r   r  r   r  r  r  ctypes._endianr  r  c_int8c_uint8kindc_int16c_int32c_int64c_uint16c_uint32c_uint64r&   r&   r&   r'   <module>   s4   



!





N	



