
    \
j                        d dl Z d dlZd dlZd dlmZ d dlT d dlmZ ddlT d ej        d          z  dk    Z	 ej
                    d	k    Z ej
                    d
k    Z ee          dk    reZn ee          dk    reZ ej        d          ZedZe                    e          Ze                     ej        d                    Zdej        _        egej        _        eej        _        eeeeegej        _        eej         _        eej!        _        eegej!        _        eej"        _        eegej"        _         e#e          ej$        _        e e#e%          gej$        _         e#e          ej&        _        e e#e%          gej&        _         e#e          ej'        _        e e#e%          gej'        _         e#e          ej(        _        e e#e%          gej(        _        eej)        _        eegej)        _        eej*        _        eegej*        _        eej+        _        eegej+        _        eej,        _        eegej,        _        eej-        _        egej-        _        eej.        _        eegej.        _        eej/        _        egej/        _        eej0        _        eegej0        _        eseej1        _        eegej1        _        eej2        _        egej2        _        eej3        _        eegej3        _        eej4        _        egej4        _        e5ej6        _        egej6        _        eej7        _        egej7        _        eej8        _        egej8        _        eej9        _        eeeegej9        _        eej:        _        eegej:        _        dej;        _        eegej;        _        eej<        _        eegej<        _        dej=        _        ee5gej=        _        dej>        _        eegej>        _        eej?        _        egej?        _        eej@        _        egej@        _        eejA        _        egejA        _        eejB        _        ee%gejB        _         e#eC          ejD        _        egejD        _        dejE        _        eegejE        _        dejF        _        ee%eegejF        _        eejG        _        egejG        _        eejH        _        egejH        _        e%ejI        _        egejI        _        dejJ        _        eeegejJ        _        eejK        _        egejK        _        eejL        _        eegejL        _        eejM        _        eeegejM        _         e#e          ejN        _         e#e5          gejN        _        eejO        _        eegejO        _        eejP        _        egejP        _        e5ejQ        _        ee5gejQ        _        eejR        _        egejR        _        eejS        _        egejS        _        esdejT        _        esdejU        _        dejV        _        egejV        _        dejW        _        egejW        _        dejX        _        eeee5gejX        _        eejY        _        eegejY        _        eejZ        _        egejZ        _        eej[        _        egej[        _        eej\        _        egej\        _        eej]        _        eeegej]        _        eej^        _        eegej^        _        eej_        _        eegej_        _        eej`        _        deja        _        eeegeja        _        eejb        _        egejb        _        eejc        _        egejc        _        eejd        _        eegejd        _         G d dee          Zf e#ef          ejg        _        eee e#e%          gejg        _        eejh        _        e e#e%          gejh        _         e#e          e_i        e e#e%          geji        _        efejj        _        eeeegejj        _        eejk        _        egejk        _        eejl        _        egejl        _        eejm        _        eegejm        _        eejn        _        egejn        _        eejo        _        g ejo        _        dejp        _        egejp        _        d ZqdZrdZsd Ztd Zud Zvd Zwd Zxd Zyd Zzd Z{d Z| G d dee          Z} e#e}          Z~dddZi Zd  Zd! Zd" Zd# Zd$ Zd% Zd& Zd' Z G d( d)          Z G d* d+          Z G d, d-          Z G d. d/          Z e            ZegZd0 Z G d1 d2          Zd3 Zd4 Z G d5 d6          Z G d7 d8          Zd9 Zd: Zed;             ZdS )<    N)contextmanager)*)util      P@   i386arm64   objcz/usr/lib/libobjc.dylibcc                       e Zd ZdefdefgZdS )OBJC_METHOD_DESCRIPTIONnametypesN)__name__
__module____qualname__c_void_pc_char_p_fields_     \/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/libs/darwin/cocoapy/runtime.pyr   r   x  s"        "Wh$78HHHr   r   i  i  c                 Z    t          | t                    r| S |                     d          S )Nascii)
isinstancebytesencode)xs    r   ensure_bytesr"     s+    !U 88Gr   c                 j    t          t                              t          |                               S N)r   r   sel_registerNamer"   r   s    r   get_selectorr'     s&    D)),t*<*<==>>>r   c                 j    t          t                              t          |                               S r$   )r   r   objc_getClassr"   r&   s    r   	get_classr*     s&    D&&|D'9'9::;;;r   c                 P    t          t                              |                     S r$   )r   r   object_getClass)objs    r   get_object_classr.     s    D((--...r   c                 j    t          t                              t          |                               S r$   )r   r   objc_getMetaClassr"   r&   s    r   get_metaclassr1     s&    D**<+=+=>>???r   c                     t          t                              |                     }t          t                              |                    S r$   )r   r   r,   class_getSuperclass)r-   clss     r   get_superclass_of_objectr5     s9    
4'',,
-
-CD,,S11222r   c                     t          |           t          t                    k    rdS t          st          |           dk    rdS t          rt          |           dk    rdS dS )z=Try to figure out when a return type will be passed on stack.Fr      T)type	Structure__LP64__sizeofrestypes    r   x86_should_use_stretr>     s]    G}}Y''u w1,,u F7OOr))u4r   c                 ~    t           sdS t          r| t          k    rdS t          s| t          t          t          fv rdS dS )zLDetermine if objc_msgSend_fpret is required to return a floating point type.FT)__i386__r:   c_longdoublec_floatc_doubler<   s    r   should_use_fpretrD     sI     u G|++t GX|#DDDt5r   c                    t          | t                    rt          |           } t          |          }|                    dt
                    }|                    dg           }t          |          rE|t          j        _	        t
          t
          g|z   t          j        _
        t          j        | |g|R  }nt          |          rZt          |          t
          t
          g|z   t          j        _
         |            }t          j        t          |          | |g|R   n^|t          j        _	        t
          t
          g|z   t          j        _
        t          j        | |g|R  }|t
          k    rt          |          }|S )Nr=   argtypes)r   strr*   r'   getr   rD   r   objc_msgSend_fpretr=   rF   r>   POINTERobjc_msgSend_stretbyrefobjc_msgSend)receiverselNameargskwargsselectorr=   rF   results           r   send_messagerT     sX   (C   'X&&G$$HjjH--Gzz*b))H   &*1',4h+?(+J((8CdCCC	g	&	& 	&,3G,<,<h+QT\+\(fxIDIIIII$+!&.%9H%D""8X====hf%%FMr   c                       e Zd ZdefdefgZdS )
OBJC_SUPERrN   classN)r   r   r   r   r   r   r   r   rV   rV     s"        X&((;<HHHr   rV   )superclass_namec                   t          | d          r| j        } |t          |           }nt          |          }t	          | |          }t          |          }|                    dt                    }|                    dd           }	|t          j	        _
        |	r!t          t          g|	z   t          j	        _        nd t          j	        _        t          j	        t          |          |g|R  }
|t          k    rt          |
          }
|
S )N_as_parameter_r=   rF   )hasattrrZ   r5   r*   rV   r'   rH   r   r   objc_msgSendSuperr=   OBJC_SUPER_PTRrF   rL   )rN   rO   rX   rP   rQ   
superclasssuper_structrR   r=   rF   rS   s              r   
send_superr`     s    x)** +*-h77

//
h
33LG$$HjjH--Gzz*d++H%,D" /+98*Dx*O''*.'#E,$7$7IDIIIF(&!!Mr   c                    g }d}d}d}| D ]*}t          |t                    rt          |g          }|dk    r>|r1|dd         dk    r#|dk    r|dk    r|                    |           d}||z  }|dz  }l|dk    r||z  }|dz  }|dk    sJ |d	k    r>|r1|dd         dk    r#|dk    r|dk    r|                    |           d}||z  }|dz  }|d
k    r||z  }|dz  }|dk    sJ |s|r||z  }|dv r|dv r|dv r0|r|dd         dk    r||z  }|r|                    |           |},|r|                    |           |S )a  Takes a type encoding string and outputs a list of the separated type codes.
    Currently does not handle unions or bitfields and strips out any field width
    specifiers or type specifiers from the encoding.  For Python 3.2+, encoding is
    assumed to be a bytes object and not unicode.

    Examples:
    parse_type_encoding('^v16@0:8') --> ['^v', '@', ':']
    parse_type_encoding('{CGSize=dd}40@0:8{CGSize=dd}16Q32') --> ['{CGSize=dd}', '@', ':', '{CGSize=dd}', 'Q']
    r   r      {N   ^r      }   [   ]s
   0123456789s   rnNoORVs   ^cislqCISLQfdBv*@#:b?)r   intr   append)encodingtype_encodingsbrace_countbracket_counttypecoder   s         r   parse_type_encodingro   5  s!    NKMH . . a 	qc

A99 HRSSMT11kQ6F6F=\]K]K]%%h///MH1KK$YYMH1K1$$$$$$YY HRSSMT11kQ6F6F=\]K]K]%%h///MHQMM$YYMHQM!Q&&&&& 	M 	MHH-*__*** HRSSMT11A  4"))(333  (h'''r   c                    | t           v rt           |          S i dt          dt          dt          dt          dt
          dt          dt          dt          d	t          d
t          dt          dt          dt          dd dt          dt          dt          dt          t           t"          t$          t&          t(          t*          t,          t.          t0          t2          i}g }t5          |           D ]~}||v r|                    ||                    "|dd         dk    r=|dd          |v r1|                    t9          ||dd                                        mt;          d|z             t=          | }|t           | <   |S )N   c   i   s   l   q   C   I   S   L   Q   f   d   B   v   *   @   #   :r   r   rd   zunknown type encoding: )cfunctype_tablec_charc_intc_shortc_long
c_longlongc_ubytec_uintc_ushortc_ulongc_ulonglongrB   rC   c_boolr   r   NSPointEncodingNSPointNSSizeEncodingNSSizeNSRectEncodingNSRectNSRangeEncodingNSRangePyObjectEncoding	py_objectro   ri   rJ   	Exception	CFUNCTYPE)rj   	typecodesrF   code	cfunctypes        r   cfunctype_for_encodingr   }  s    ?""x((.v .tU .D' .4 .z .w. $f..2H.>BG.MQS^.w. $h.04f.>BD.JNPX. x. "&x. 26xRYRY!9. .I H#H-- > >9OOIdO,,,,!A#Y$48y#8#8OOGId122h$78899995<===8$I
 !*OHr   c                     t          | t                    rt          |           } t          t                              | t          |          d                    S Nr   )r   rG   r*   r   r   objc_allocateClassPairr"   )r^   r   s     r   create_subclassr     sJ    *c"" +z**
D//
L<N<NPQRRSSSr   c                 :    t                               |            d S r$   )r   objc_registerClassPair)subclasss    r   register_subclassr     s    )))))r   c                 0   t          |          }|d         dk    sJ |d         dk    sJ t          |          }t          |          } ||          }t          t          |t          gt
          j        _        t
                              | |||           |S )Nr   r      r   )ro   r'   r   r   r   r   class_addMethodrF   )r4   rO   methodr   rk   rR   r   imps           r   
add_methodr     s    (//N1%%%%1%%%%G$$H&u--I
)F

C%-xH$MD!hU333Jr   c           	          t                               | t          |          t          |          t	          |          t          |                    S r$   )r   class_addIvarr"   r;   	alignmentencoding_for_ctype)r4   r   vartypes      r   add_ivarr     s?    c<#5#5vg	RYHZHZ\nov\w\wxxxr   c                     t           t          |gt          j        _        t                              | t          |          |           d S r$   )r   r   r   object_setInstanceVariablerF   r"   )r-   varnamevaluer   s       r   set_instance_variabler     s:    08(G/LD#,##Cg)>)>FFFFFr   c                      |            }t                               | t          |          t          |                     |j        S r$   )r   object_getInstanceVariabler"   rL   r   )r-   r   r   variables       r   get_instance_variabler     s;    wyyH##Cg)>)>hPPP>r   c                       e Zd ZdZi dededededede	de
d	ed
edededededddddedededededeeeeeeeeeeei	Zi Zd Zd Z d Z!d Z"d Z#d Z$dS )
ObjCMethodz>This represents an unbound Objective-C method (really an IMP).rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   Ns   Vvr   r   r   r   s   ^v   ?c           	      j    t          t                              |                     _        t                               j                   _         j                            dd           _        t                              |           _	        t          
                    |          }t          |t                    j         _        t                              |           _        t          t                              |                     _        g  _        t)           j                  D ]Z}t+          d          }t                              |||t/          |                      j                            |j                   [	  fd j        D              _        n#  d _        Y nxY w	  j        dk    rt4           _        n7 j        dk    rt8           _        n                      j                   _        n#  d _        Y nxY wd _        t>                               |           dS )zInitialize with an Objective-C Method pointer.  We then determine
        the return type and argument type information of the method.r      _i   c                 :    g | ]}                     |          S r   )ctype_for_encoding).0tselfs     r   
<listcomp>z'ObjCMethod.__init__.<locals>.<listcomp>  s'    UUUAT44Q77UUUr   Nr   r   )!r   r   method_getNamerR   sel_getNamer   replacepynamemethod_getTypeEncodingrj   method_copyReturnTypecastr   r   return_typemethod_getNumberOfArgumentsnargsmethod_getImplementationr   argument_typesrangec_buffermethod_getArgumentTypelenri   rF   ObjCInstancer=   	ObjCClassr   funclibcfree)r   r   return_type_ptribuffers   `    r   __init__zObjCMethod.__init__  s    !!4!4V!<!<==$$T]33	i''d3333F;;44V<<::@55f==
D99&AABB tz"" 	5 	5Ac]]F''63v;;GGG&&v|4444	!UUUUATUUUDMM	! DMMM		 4''+!T))(#66t7GHH	 DLLL			/"""""s   F% %	F04AH 	Hc                 (   || j         v r| j         |         S |dd         dk    r3|dd         | j         v r"t          | j         |dd                            S |dd         dk    r|dd         t          t          fv rt          S |dd         dk    r&|dd         | j         v r| j         |dd                  S |dd         dk    r3|dd         | j         v r"t          | j         |dd                            S t          d| j        d	|          )
z3Return ctypes type for an encoded Objective-C type.r   r   rd   N   rr   s   r^zunknown encoding for z: )r   rJ   CGImageEncodingNSZoneEncodingr   r   r   )r   rj   s     r   r   zObjCMethod.ctype_for_encoding	  s,   t~%%>(++ac]d""x|t~'E'E4>(122,7888ac]d""x|7X'X'XOac]d""x|t~'E'E>(122,//ac]e##(F(F4>(122,7888)TYYYQRRRr   c                     | j         t          k    s| j         t          k    rt          t          g| j        R  | _        nt          | j         g| j        R  | _        | j        S )z*Returns a ctypes CFUNCTYPE for the method.)r=   r   r   r   r   rF   	prototyper   s    r   get_prototypezObjCMethod.get_prototype  s[    <<''4<9+D+D
 'x@$-@@@DNN&t|DdmDDDDN~r   c                 (    d| j         d| j        dS )Nz<ObjCMethod:  >)r   rj   r   s    r   __repr__zObjCMethod.__repr__'  s     (,			4===AAr   c                 "   | j         s|                                 }t          | j        |          | _         | j        t
          k    s| j        t          k    rt          | j         _        n| j        | j         _        | j        | j         _        | j         S )z6Returns a python-callable version of the method's IMP.)	r   r   r   r   r=   r   r   r   rF   )r   r   s     r   get_callablezObjCMethod.get_callable*  sv    y 	/**,,ITXy11DI||++t|y/H/H$,	!!$(L	!!%DIyr   c           	         |                                  }	  ||| j        g|R  }| j        t          k    rt          |          }n| j        t          k    rt	          |          }|S # t
          $ rX}|xj        dt          | j                  z   dt          | j	                  z   dt          | j
                  z   fz  c_         d}~ww xY w)zCall the method with the given id and arguments.  You do not need
        to pass in the selector as an argument since it will be automatically
        provided.zselector = z
argtypes =zencoding = N)r   rR   r=   r   r   ArgumentErrorrP   rG   r   rF   rj   )r   objc_idrP   frS   errors         r   __call__zObjCMethod.__call__6  s     	Qw5555F ||++%f--**"6**M 	 	 	JJ=3ty>>9'#dm*<*<<(3t}+=+==? ?JJ 	s   AA& &
C0ACC)%r   r   r   __doc__c_byter   r   r   r   r   r   r   r   r   rB   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sj       HH
.v .tU .D' .4 .z .w. $f..2H.>BG.MQS^.w. $h.04f.>BD.JOQU.W[]e. x. "&xxRVX` '>6>SY '!9. .I O(# (# (#VS S S$
 
 
B B B
 
 
    r   r   c                   $    e Zd ZdZd Zd Zd ZdS )ObjCBoundMethodzThis represents an Objective-C method (an IMP) which has been bound
    to some id which will be passed as the first parameter to the method.c                 "    || _         || _        dS )z>Initialize with a method and ObjCInstance or ObjCClass object.Nr   r   )r   r   r   s      r   r   zObjCBoundMethod.__init__R  s    r   c                 2    d| j         j        d| j        dS )Nz<ObjCBoundMethod z (z)>)r   r   r   r   s    r   r   zObjCBoundMethod.__repr__W  s      .2k.>.>.>MMr   c                 (     | j         | j        g|R  S )z)Call the method with the given arguments.r   )r   rP   s     r   r   zObjCBoundMethod.__call__Z  s    t{4</$////r   N)r   r   r   r   r   r   r   r   r   r   r   r   N  sP        M M  
N N N0 0 0 0 0r   r   c                   J     e Zd ZdZi Z fdZd Zd Zd Zd Z	d Z
d Z xZS )	r   z(Python wrapper for an Objective-C class.c                    t          |t                    r|}t          |          }n@|}t          |t                    st          |          }t                              |          }|| j        v r| j        |         S t          t          |           	                    |           }||_
        ||_        i |_        i |_        ||_        || j        |<   |                                 |                                 |S )zCreate a new ObjCClass instance or return a previously created
        instance for the given Objective-C class.  The argument may be either
        the name of the class to retrieve, or a pointer to the class.)r   rG   r*   r   r   class_getName_registered_classessuperr   __new__ptrr   instance_methodsclass_methodsrZ   cache_instance_methodscache_class_methods)r4   class_name_or_ptrr   r  
objc_class	__class__s        r   r  zObjCClass.__new__k  s    
 '-- 		+$DD//CC#C c8,, $smm%%c**D 3****400 9c**22377


&(
##%
 $'
! )3% 	))+++&&(((r   c                 L    d| j         dt          | j        j                  dS )Nz<ObjCClass: z at r   )r   rG   r  r   r   s    r   r   zObjCClass.__repr__  s'     *.)))S5H5H5H5HIIr   c                 D   t                      }t                              | j        t	          |                    }t          |j                  D ]5}t          ||                   }t          |          }|| j	        |j
        <   6t                              |           dS )zCreate and store python representations of all instance methods
        implemented by this class (but does not find methods of superclass).N)r   r   class_copyMethodListr  rL   r   r   r   r   r  r   r   r   r   countmethod_arrayr   r   objc_methods         r   r  z ObjCClass.cache_instance_methods  s     005<<HHu{## 	D 	DAl1o..F$V,,K8CD!+"455		,r   c                 t   t                      }t                              t                              | j                  t          |                    }t          |j                  D ]5}t          ||                   }t          |          }|| j
        |j        <   6t                              |           dS )zCreate and store python representations of all class methods
        implemented by this class (but does not find methods of superclass).N)r   r   r  r,   r  rL   r   r   r   r   r  r   r   r   r  s         r   r  zObjCClass.cache_class_methods  s     001E1Edh1O1OQVW\Q]Q]^^u{## 	A 	AAl1o..F$V,,K5@D{122		,r   c                    || j         v r| j         |         S t          |                    dd                    }t          t                              | j        |                    }|j        rt          |          }|| j         |<   |S dS )zReturns a python representation of the named instance method,
        either by looking it up in the cached list of methods or by searching
        for and creating a new method object.r   r   N)	r  r'   r   r   r   class_getInstanceMethodr  r   r   r   r   rR   r   r  s        r   get_instance_methodzObjCClass.get_instance_method  s     4((((.. $DLLt$<$<==Hd::48XNNOOF| #(00.9%d+""tr   c                    || j         v r| j         |         S t          |                    dd                    }t          t                              | j        |                    }|j        rt          |          }|| j         |<   |S dS )zReturns a python representation of the named class method,
        either by looking it up in the cached list of methods or by searching
        for and creating a new method object.r   r   N)	r  r'   r   r   r   class_getClassMethodr  r   r   r  s        r   get_class_methodzObjCClass.get_class_method  s     4%%%%d++ $DLLt$<$<==Hd77(KKLLF| #(00+6"4(""tr   c                     t          |          }|                     |          }|rt          || j                  S |                     |          }|r|S t          d| j        d|          )5Returns a callable method object with the given name.z
ObjCClass  has no attribute )r"   r  r   r  r  AttributeErrorr   r   r   r   s      r   __getattr__zObjCClass.__getattr__  s}     D!!&&t,, 	5"648444 ))$// 	Mn4999ddSTTTr   )r   r   r   r   r   r  r   r  r  r  r  r  __classcell__r	  s   @r   r   r   a  s        22 $ $ $ $ $LJ J J     
  
  
   "  "U U U U U U Ur   r   c                   6    e Zd Zd Zed             Zd Zd ZdS )_AutoreleasepoolManagerc                 $    d| _         d g| _        d S r   )currentpoolsr   s    r   r   z _AutoreleasepoolManager.__init__  s    V


r   c                 0    t          | j                  dz
  S )z,Number of total pools. Not including global.r   )r   r%  r   s    r   r  z_AutoreleasepoolManager.count  s     4:""r   c                 x    | j                             |           | j                             |          | _        d S r$   )r%  ri   indexr$  r   pools     r   createz_AutoreleasepoolManager.create  s3    
$z''--r   c                 r    | j                             |           t          | j                   dz
  | _        d S )Nr   )r%  remover   r$  r)  s     r   deletez_AutoreleasepoolManager.delete  s0    
$4:*r   N)r   r   r   r   propertyr  r+  r.  r   r   r   r"  r"    s\           # # X#. . .+ + + + +r   r"  c                     t          dd          }t          |d| t                    }t                              | ||t                     t          |d           d S )NDeallocationObserveralloczinitWithObject:)rF   release)rT   _dealloc_argtyper   objc_setAssociatedObjectOBJC_ASSOCIATION_RETAIN)objc_ptrobservers     r   _set_dealloc_observerr9    s_    
 2G<<HH'8(M]^^^H!!(Hh@WXXX
 9%%%%%r   c                   <     e Zd ZdZdZdZi Zd fd	Zd Zd Z	 xZ
S )	r   z+Python wrapper for an Objective-C instance.r   FTc                    t          |t                    st          |          }|j        sdS |r |j        | j        v r| j        |j                 S t	          t
          |                               |           }||_        ||_        t          t          
                    |                    }t          |          |_        |r<|| j        |j        <   t          j        rt          j        |_        nt!          |           |S )zCreate a new ObjCInstance or return a previously created one
        for the given object_ptr which should be an Objective-C id.N)r   r   r   _cached_objectsr   r   r  r  rZ   r   r,   r   r  _arp_managerr$  r*  r9  )r4   
object_ptrcacheobjc_instance	class_ptrr	  s        r   r  zObjCInstance.__new__  s    *h// 	.!*--J  	4  	9Z%)<<<&z'788 lC0088==&'1$T11*==>>	#,Y#7#7   	24AC
 01 # 2%1%9""%j111r   c                    | j         j        dk    rHddlm}  ||           }dt	          |           | j         j        |t          | j        j                  fz  S dt	          |           | j         j        t          | j        j                  fz  S )Ns
   NSCFStringr   )cfstring_to_stringz!<ObjCInstance %#x: %s (%s) at %s>z<ObjCInstance %#x: %s at %s>)r  r   	cocoalibsrC  idrG   r  r   )r   rC  strings      r   r   zObjCInstance.__repr__9  s    ?=00555555''--F6"T((DODXZ`befjfnftbubu9vvv-D4?;OQTUYU]UcQdQd0eeer   c                 "   t          |          }| j                            |          }|rt          ||           S | j                            |          }|rt          || j        j                  S t          d| j        j        d|          )r  zObjCInstance r  )r"   r  r  r   r  r  r  r   r  s      r   r  zObjCInstance.__getattr__B  s     D!!44T:: 	1"64000 11$77 	@"64?+>???ndoFZFZFZ\`\`abbbr   T)r   r   r   r   r*  retainedr<  r  r   r  r  r   s   @r   r   r   	  s{        55DHO' ' ' ' ' 'Rf f fc c c c c c cr   r   c                  R    d t           j                                        D             S )zfFor debug purposes, return a list of instance names.
    Useful for debugging if an object is leaking.c                 &    g | ]}|j         j        S r   )r  r   )r   r-   s     r   r   z(get_cached_instances.<locals>.<listcomp>[  s    QQQCCNQQQr   )r   r<  valuesr   r   r   get_cached_instancesrM  X  s(     RQ<+G+N+N+P+PQQQQr   c                 2   g }t          |           dd         }t          ||          D ]l\  }}|dk    r#|                    t          |                     .|dk    r#|                    t	          |                     W|                    |           m|S )zUsed by ObjCSubclass to convert Objective-C method arguments to
    Python values before passing them on to the Python-defined method.   Nr   r   )ro   zipri   r   r   )rj   rP   new_argsarg_encodingseas         r   convert_method_argumentsrU  ^  s     H'11!""5MM4((  199OOLOO,,,,$YYOOIaLL))))OOAOr   c                   D    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 ZdS )ObjCSubclasszUse this to create a subclass of an existing Objective-C class.
    It consists primarily of function decorators which you use to add methods
    to the subclass.Tc                     i | _         || _        t          ||          | _        | j        | _        |r|                                  d S d S r$   )
_imp_tabler   r   objc_clsrZ   register)r   r^   r   r[  s       r   r   zObjCSubclass.__init__  sN    	'
D99"m 	MMOOOOO	 	r   c                 v    t                               | j                   t          | j                  | _        dS )z4Register the new class with the Objective-C runtime.N)r   r   rZ  r1   r   objc_metaclassr   s    r   r[  zObjCSubclass.register  s0    ##DM222+DI66r   c                 .    t          | j        ||          S )zAdd instance variable named varname to the subclass.
        varname should be a string.
        vartype is a ctypes type.
        The class must be registered AFTER adding instance variables.)r   rZ  )r   r   r   s      r   r   zObjCSubclass.add_ivar  s    
 w888r   c                 H    t          | j        |||          }|| j        |<   d S r$   )r   rZ  rY  r   r   r   rj   r   s        r   r   zObjCSubclass.add_method  s)    fh?? #r   c                 H    t          | j        |||          }|| j        |<   d S r$   )r   r]  rY  r`  s        r   add_class_methodzObjCSubclass.add_class_method  s*    ,dFHEE #r   c                      t                    t                    }|                    dd           d                    |           fd}|S )zDecorator for instance methods without any fancy shenanigans.
        The function must have the signature f(self, cmd, *args)
        where both self and cmd are just pointers to objc objects.r      @:r   c                 l    | j                             dd          }                    | |           | S )N_:r   r   r   )r   r   rj   r   s     r   	decoratorz)ObjCSubclass.rawmethod.<locals>.decorator  s5    :%%c3//DOOAtX...Hr   r"   ro   insertjoinr   rj   r   ri  s   ``  r   	rawmethodzObjCSubclass.rawmethod  sm    
  ))'11	E"""88I&&	 	 	 	 	 	
 r   c                      t                    t                    }|                    dd           d                    |           fd}|S )z(Function decorator for instance methods.r   rd  r   c                 z      fd} j                             dd          }                    ||           |S )Nc                     t          | d          }||_        d|_        t          |          } |g|R  }t	          |t
                    r|j        j        }n!t	          |t                     r|j        j        }|S )NT)r   objc_cmdrI  rU  r   r   r  r   )	objc_selfrr  rP   py_selfrS   rj   r   s        r   r  z;ObjCSubclass.method.<locals>.decorator.<locals>.objc_method  s    &y$77#+ #' /$??7*T***fi00 .#Z-FF55 .#Z-Fr   rf  rg  rh  )r   r  r   rj   r   s   `  r   ri  z&ObjCSubclass.method.<locals>.decorator  sW    
 
 
 
 
 
 :%%c3//DOOKx888r   rj  rm  s   ``  r   r   zObjCSubclass.method  sm      ))'11	E"""88I&&	 	 	 	 	 	" r   c                      t                    t                    }|                    dd           d                    |           fd}|S )z%Function decorator for class methods.r   rd  r   c                 z      fd} j                             dd          }                    ||           |S )Nc                     t          |           }||_        t          |          } |g|R  }t          |t                     r|j        j        }n!t          |t                    r|j        j        }|S r$   )r   rr  rU  r   r  r   r   )rZ  rr  rP   py_clsrS   rj   r   s        r   objc_class_methodzFObjCSubclass.classmethod.<locals>.decorator.<locals>.objc_class_method  s|    "8,,"*/$??6)D)))fi00 .#Z-FF55 .#Z-Fr   rf  rg  )r   r   rb  )r   ry  r   rj   r   s   `  r   ri  z+ObjCSubclass.classmethod.<locals>.decorator  sZ    	 	 	 	 	 	 :%%c3//D!!"3T8DDD$$r   rj  rm  s   ``  r   classmethodzObjCSubclass.classmethod  sm      ))'11	E"""88I&&	% 	% 	% 	% 	% 	%  r   NrH  )r   r   r   r   r   r[  r   r   rb  rn  r   rz  r   r   r   rW  rW    s            7 7 79 9 9$ $ $
$ $ $  "  6    r   rW  c                   2   e Zd Z eddd          Ze                    de           e                                 e                    d          d             Z	e                    d          d	             Z
e                    d          d
             ZdS )#DeallocationObserver_ImplementationNSObjectr1  F)r[  observed_objectz@@c                 b    t          | d          } | j        } t          | d|t                     | S )Ninitr~  )r`   r   r   r   )r   cmdr7  s      r   initWithObject_z3DeallocationObserver_Implementation.initWithObject_  s2    $''zd$5xJJJr   vc                 &    t          | d           d S )Ndealloc_obj_observer_deallocr   r  s     r   r  z+DeallocationObserver_Implementation.dealloc%  s    dI.....r   c                 &    t          | d           d S )Nfinalizer  r  s     r   r  z,DeallocationObserver_Implementation.finalize)  s     	dJ/////r   N)r   r   r   rW  r1  r   r   r[  rn  r  r  r  r   r   r   r|  r|    s        '<
4JUZ[[[!!"3X>>>!!#####D))  *) ##C((/ / )(/ ##C((0 0 )(0 0 0r   r|  c                     t          | dt                    }|rBt                              || dt                     t
          j                            |d           t          | |           dS )zRemoves any cached ObjCInstances in Python to prevent memory leaks.
    Manually break association as it's not implicitly mentioned that dealloc would break an association,
    although we do not use the object after.
    r~  N)	r   r   r   r5  OBJC_ASSOCIATION_ASSIGNr   r<  popr`   )objc_obsselector_namer7  s      r   r  r  2  sh    
 %X/@(KKH 9%%h$@WXXX$((4888x'''''r   c                     t          t          j                                                  D ]5}t          j        |         }|j        du r|j        | k    rt          j        |= 6dS )a  Cleanup any ObjCInstance's created during an AutoreleasePool creation.
    See discussion and investigation thanks to mrJean with leaks regarding pools:
        https://github.com/mrJean1/PyCocoa/issues/6
    It was determined that objects in an AutoreleasePool are not guaranteed to call a dealloc, creating memory leaks.
    The DeallocObserver relies on this to free memory in the ObjCInstance._cached_objects.
        Solution is as follows:
    1) Do not observe any ObjCInstance's with DeallocObserver when non-global autorelease pool is in scope.
    2) Some objects such as ObjCSubclass's must be retained.
    3) When a pool is drained and dealloc'd, clear all ObjCInstances in that pool that are not retained.
    FN)listr   r<  keysrI  r*  )pool_id	cobjc_ptrcobjc_is      r   _clear_arp_objectsr  ?  sg     ,6;;==>> 8 8	.y9u$$)@)@,Y78 8r   c               #     K   t                                           } t                              |            	 dV  t	          t          j                            |                      t                               |            t                              |            dS # t	          t          j                            |                      t                               |            t                              |            w xY w)a<  Use objc_autoreleasePoolPush/Pop because NSAutoreleasePool is no longer recommended:
        https://developer.apple.com/documentation/foundation/nsautoreleasepool
    @autoreleasepool blocks are compiled into the below function calls behind the scenes.
    Call them directly to mimic the Objective C behavior.
    N)	r   objc_autoreleasePoolPushr=  r+  r  r%  r(  objc_autoreleasePoolPopr.  )r*  s    r   AutoReleasePoolr  P  s       ((**D"<-33D99:::$$T***D!!!!! 	<-33D99:::$$T***D!!!!s   B A"C?)sysplatformstruct
contextlibr   ctypesr   
cocoatypescalcsizer:   machiner@   	__arm64__r;   r   c_int32c_ptrdiff_tc_int64find_librarylibcdllLoadLibraryr   r   r   r=   rF   r   r   r   c_size_tc_uint8r   class_addProtocolclass_conformsToProtocolrJ   class_copyIvarListr   r  class_copyPropertyListclass_copyProtocolListclass_createInstancer  class_getClassVariabler  class_getInstanceSizeclass_getInstanceVariableclass_getIvarLayoutclass_getMethodImplementation#class_getMethodImplementation_stretr   class_getPropertyr3   r   class_getVersionclass_getWeakIvarLayoutclass_isMetaClassclass_replaceMethodclass_respondsToSelectorclass_setIvarLayoutclass_setSuperclassclass_setVersionclass_setWeakIvarLayoutivar_getNameivar_getOffsetivar_getTypeEncodingmethod_copyArgumentTyper   r   method_exchangeImplementationsr   r   r   r   method_getReturnTyper   method_setImplementationr   objc_copyProtocolListobjc_getAssociatedObjectr)   objc_getClassListr0   objc_getProtocolobjc_msgSendSuper_stretrK   r   objc_removeAssociatedObjectsr5  object_copyobject_disposer,   object_getClassNamer   object_getIvarobject_setClassr   object_setIvarproperty_getAttributesproperty_getNameprotocol_conformsToProtocolr9   r   "protocol_copyMethodDescriptionListprotocol_copyPropertyListprotocol_copyProtocolListprotocol_getMethodDescriptionprotocol_getNamer   sel_isEqualr%   r  r  r  r6  OBJC_ASSOCIATION_COPYr"   r'   r*   r.   r1   r5   r>   rD   rT   rV   r]   r`   r   ro   r   r   r   r   r   r   r   r   r   r   r"  r=  r4  r9  r   rM  rU  rW  r|  r  r  r  r   r   r   <module>r     s  @ 


   % % % % % %              $$$*H&(X7*		6(qKKVHK d ;
"C))#..// 	 Z	 
 $  '8WhO    &   "(  #+X"6   )/  %*2H)=  & #*'("3"3  $,ggfoo#>    %,GH$5$5  !&.%@  " '.gh&7&7  #(0''&//'B  $ '.gh&7&7  #(0''&//'B  $ %-  !&.%9  " %-  !&.%9  " '/  #(0(';  $ (0  $)18(<  % &.  "'/j  # *2  &+3X*>  ' $,   %-J  ! .6 " */7.B " +  M7?D,49A88LD,5 &  'j   "*  #+X"6   $,   %-J  ! !&  "*   (0  $)1
  % "(  #+*   $,   %-x8$L  ! )/  %*2H)=  & $(   %-x$8  ! $,   %-x$8  ! !%  "*E!2   (,  $)18(<  %
 %  &Z   *   (z   %-  !&.Z  " (0  $)16(:  % &-WV__  "'/j  # /3 # +08(/C # , '+  #(0&(H'M  $ )1  %*2  & '   (z   ,2   (-5J   ) %)  !&.(%C  " '/  #(0z  $ )1  %*2H)=  &
 '/  #(0(H'E  $
 &-WX%6%6  "'.wu~~&6  # )1  %*2H)=  & &  'j   "'  #+U"3   "*  #+*   !)  "*    0+/D (  +&*D# '+  #(0z  $ -1 ! ).6Z ! * )-  %*2Hh)N  &
 $  %x0   '   (z    (  !)
   $,   %-J  ! +3  ',4h+I  ( '   ((3    (  !)8 4   +3  ' #   ((H=  
 '/  #(0z  $ !)  "*  
 ,2   (-5x,@   )9 9 9 9 9i 9 9 9 3:':Q2R2R ' /4<ffggV\oo3^ ' 0 *2  &+3WWV__*E  ' ")!2!2 +3WWV__*E  ' .E " */766.R " + !)  "*  
 $  %J   "  %x0   !)  "*  
 )1  %)+  & (,  $)1
  %       ? ? ?< < </ / /@ @ @3 3 3  
 
 
*  2= = = = = = = = $$ :>     0 B B BP  JT T T* * *	 	 	y y yG G G
  { { { { { { { {@0 0 0 0 0 0 0 0&|U |U |U |U |U |U |U |UD+ + + + + + + +& '&((: & & &Lc Lc Lc Lc Lc Lc Lc Lc^R R R  Fg g g g g g g gp0 0 0 0 0 0 0 02
( 
( 
(8 8 8" " " " " "r   