
    ia                     <   S r SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  \R                  " S5      r " S S\5      r\" 5       r " S	 S
\5      r " S S\5      r " S S\5      r\R&                  S   S:X  a  \rg\R&                  S   S:X  a  \rg\" S5      e)z5Function signature introspection and code generation.    )unicode_literalsN)InternalKGBError)get_defined_attr_valuekgbc                       \ rS rSrSrS rSrg)	_UnsetArg   z?Internal class for representation unset arguments on functions.c                     g)zZReturn a string representation of this object.

Returns:
    unicode:
    ``_UNSET_ARG``.

_UNSET_ARG selfs    L/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/kgb/signature.py__repr___UnsetArg.__repr__   s         r   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r   r      s
    Ir   r   c                   P    \ rS rSrSrSrSrSr\S4S jr	S r
S	 rS
 rS rS rSrg)BaseFunctionSig!   a  Base class for a function signature introspector.

This is responsible for taking a function (and a user-requested owner)
and determining the actual owner, function type, function name, and
arguments. It's also responsible for generating code that can be used
to help define functions or perform calls, for use in spy code generation.

How this is all done depends entirely on the version of Python. Subclasses
must implement all this logic.

Version Changed:
    5.0:
    Added support for the following attributes:

    * :py:attr:`defined_func`.
    * :py:attr:`has_getter`.
    * :py:attr:`has_setter`.
    * :py:attr:`is_slippery`.

Attributes:
    defined_func (callable or object):
        The actual function (or wrapping object) that's defined on
        somewhere in the owner's class hierarchy (or the function itself if
        this is a standalone function). This may differ from
        :py:attr:`func`.

    has_getter (bool):
        Whether this signature represents a descriptor with a ``__get__``
        method.

    has_setter (bool):
        Whether this signature represents a descriptor with a ``__set__``
        method.

    is_slippery (bool):
        Whether this represents a slippery function. This is a method on
        a class that returns a different function every time its attribute
        is accessed on an instance.

        This occurs when a method decorator is used that wraps a function
        on access and returns the wrapper function, but does not cache the
        wrapper function. These are returned as standard functions and
        not methods.

        Slippery functions can only be detected when an explicit owner is
        provided.
r         Nc                 H   Xl         U R                  U l        U=(       d    [        XR                  5      U l        SU l        [        US5      (       a  UR                  U l	        OXl	        / U l
        / U l        / U l        / U l        / U l        SU l        SU l        SU l        g)a  Initialize the signature.

Subclasses must override this to parse function types/ownership and
available arguments. They must call :py:meth:`finalize_state` once
they've calculated all signature state.

Args:
    func (callable):
        The function to use for the signature.

    owner (type or object, optional):
        The owning class, as provided when spying on the function.
        This is not stored directly (as it may be invalid), but can
        be used for informative purposes for subclasses.

    func_name (str, optional):
        An explicit name for the function. This will be used instead
        of the function's specified name, and is usually a sign of a
        bad decorator.

        Version Added:
            7.0
N__func__F)funcTYPE_FUNCTION	func_typegetattrFUNC_NAME_ATTR	func_nameownerhasattrr   	real_funcall_arg_names	arg_nameskwarg_namesargs_param_namekwargs_param_nameis_slippery
has_getter
has_setter)r   r    r&   r%   s       r   __init__BaseFunctionSig.__init___   s    0 	++"Hgd4G4G&H
4$$ "]]DN!N!!# r   c                    U R                   nUR                   nU R                  nUR                  nU(       a  U(       a  gU(       a  U(       a  U(       a  U(       d  gU R                  nUR                  n[        UR                  5      n[        UR
                  5      n	U R                  U R                  U R                  4;   a
  USS nUSS n[        U5      [        U5      :w  aU  [        U5      [        U5      :  a  U(       d  U	R                  U5      (       a  [        U5      [        U5      :  a  U(       d  gUR                  U R
                  5      (       d  U(       d  gg)aY  Return whether two function signatures are compatible.

This will check if the signature for a function (the ``call_fake``
passed in, technically) is compatible with another (the spied
function), to help ensure that unit tests with incompatible function
signatures don't blow up with strange errors later.

This will attempt to be somewhat flexible in what it considers
compatible. Basically, so long as all the arguments passed in to
the source function could be resolved using the argument list in the
other function (taking into account things like positional argument
names as keyword arguments), they're considered compatible.

Args:
    other_sig (BaseFunctionSig):
        The other signature to check for compatibility with.

Returns:
    bool:
    ``True`` if ``other_sig`` is considered compatible with this
    signature. ``False`` if it is not.
TFr   N)r,   r-   r*   setr)   r+   r"   TYPE_BOUND_METHODTYPE_UNBOUND_METHODlen
issuperset)
r   	other_sigsource_args_namecompat_args_namesource_kwargs_namecompat_kwargs_namesource_argscompat_argscompat_all_argscompat_kwargss
             r   is_compatible_with"BaseFunctionSig.is_compatible_with   s(   .  //$44!33&88 2&6(:nn))i556I112>>d44"668 8%ab/K%ab/KK 00+[!11:J**;77+[!11:J**4+;+;<<"r   c           	         U R                   nU R                   Vs/ s H  nU" U5      PM     snU R                   Vs/ s H  nU< SU" U5      < 3PM     sn-   nU R                  (       a$  UR	                  SU" U R                  5      -  5        U R
                  (       a$  UR	                  SU" U R
                  5      -  5        SR                  U5      $ s  snf s  snf )al  Format arguments to pass in for forwarding a call.

This will build a string for use in the forwarding call, which will
pass every positional and keyword parameter defined for the function
to forwarded function, along with the ``*args`` and ``**kwargs``,
if specified.

Returns:
    unicode:
    A string representing the arguments to pass when forwarding a call.
=z*%sz**%sz, )format_forward_call_argr*   r+   r,   appendr-   join)r   _format_argarg_nameresults       r   format_forward_call_args(BaseFunctionSig.format_forward_call_args   s     22
 !NN
* !*

 !,,
,  X!67,

 MM%+d.B.B"CCD!!MM&;t/E/E#FFGyy  

s
   CCc                     [         e)aG  Return a string used to reference an argument in a forwarding call.

Subclasses must implement this to return code the spy can use when
generating a function to forward arguments in a call.

Args:
    arg_name (unicode):
        The name of the argument.

Returns:
    unicode:
    The string used to format the argument call.
NotImplementedErrorr   rJ   s     r   rF   'BaseFunctionSig.format_forward_call_arg   s
     "!r   c                     [         e)at  Format the function's arguments for a new function definition.

This will build a list of parameters for a function definition based on
the argument specification found when introspecting a spied function.
This consists of all supported argument types for the version of
Python.

Returns:
    unicode:
    A string representing an argument list for a function definition.
rO   r   s    r   format_arg_specBaseFunctionSig.format_arg_spec   s
     "!r   c                    U R                   c  U R                  U l        O& [        U R                   U R                  5      U l        [        U R                  [        R                  [        R                  [        [        45      (       dF  [        U R                  S5      (       a  SU l        [        U R                  S5      (       a  SU l        ggg! [
         a    U R                  U l         Nf = f)zFinalize the state for the signature.

This will set any remaining values for the signature based on the
calculations already performed by the subclasses. This must be called
at the end of a subclass's :py:meth:`__init__`.
N__get__T__set__)r&   r    defined_funcr   r%   AttributeError
isinstancetypesFunctionType
MethodTypeclassmethodstaticmethodr'   r/   r0   r   s    r   finalize_stateBaseFunctionSig.finalize_state  s     :: $		D.$:4::;?>>%K! $++e.@.@.3.>.>.9.:.< = = t(()44"&t(()44"& 5= " . %)II!.s   %C C.-C.)r)   r*   r,   rY   r    r%   r"   r/   r0   r.   r+   r-   r&   r(   )r   r   r   r   r   r!   r5   r6   r   r1   rB   rL   rF   rT   ra   r   r   r   r   r   r   !   sE    .b M
 
 #- + Z7r!@" "'r   r   c                   X   ^  \ rS rSrSrSrSrSrSrSr	Sr
\S	4U 4S
 jjrS rS rSrU =r$ )FunctionSigPy2i   zFunction signature introspector for Python 2.

This supports introspecting functions and generating code for use in
spies when running on Python 2.
func_closure	func_codefunc_defaultsfunc_globalsr%   im_selfNc                   > [         [        U ]  UUUS9  U R                  n[        R
                  " U5      (       ai  UR                  nUcB  U R                  U l        UR                  U l
        U[        L a  [        R                  SU5        OU R                  U l        X@l
        OU[        Lax  [        X#S5      nUbi  XQL d  UR                   UR                   L aL  [        R"                  " U5      (       a  U R                  U l        OU R                  U l        X l
        XQLU l        [        R&                  " U5      nUR(                  nUR*                  nU(       a  U(       a  [-        U5      n	Xy* S n
USU	*  nOUn/ n
UR(                  U l        Xl        Xl        UR4                  U l        UR8                  U l        UR*                  U l        U R?                  5         g)a  Initialize the signature.

Subclasses must override this to parse function types/ownership and
available arguments.

Args:
    func (callable):
        The function to use for the signature.

    owner (type, optional):
        The owning class, as provided when spying on the function.
        The value is ignored for methods on which an owner can be
        calculated, favoring the calculated value instead.

    func_name (str, optional):
        An explicit name for the function. This will be used instead
        of the function's specified name, and is usually a sign of a
        bad decorator.

        Version Added:
            7.0
r    r&   r%   NzUnbound method owners can easily be determined on Python 2.x, but not on 3.x. Please pass owner= to spy_on() to set a specific owner for %r.) superrd   r1   r%   inspectismethodri   r6   r"   im_classr&   r   loggerwarningr5   r#   rf   isclassr.   
getargspecargsdefaultsr7   r)   r*   r+   varargsr,   keywordsr-   	_defaultsra   )r   r    r&   r%   method_owner
owner_funcargspecall_argsru   num_defaultskeyword_argspos_args	__class__s               r   r1   FunctionSigPy2.__init__.  s   . 	nd,$387@ 	- 	B NN	 D!!  <<L#!%!9!9!]]
J&NN $E $(	) "&!7!7)
*$ !48J&#%%7??5))%)%=%=DN%)%;%;DN"
#-#9  $$T*<<##x=L#MN3L,/HHL$\\!'&!(!1!1 ))r   c                     U$ )Return a string used to reference an argument in a forwarding call.

Args:
    arg_name (unicode):
        The name of the argument.

Returns:
    unicode:
    The string used to format the argument call.
r   rQ   s     r   rF   &FunctionSigPy2.format_forward_call_arg  s	     r   c                     [         R                  " U R                  U R                  U R                  U R
                  S S9SS $ )a  Format the function's arguments for a new function definition.

This will build a list of parameters for a function definition based on
the argument specification found when introspecting a spied function.
This consists of all positional arguments, keyword arguments, and the
special ``*args`` and ``**kwargs`` arguments.

Returns:
    unicode:
    A string representing an argument list for a function definition.
c                     g)Nz=_UNSET_ARGr   )values    r   <lambda>0FunctionSigPy2.format_arg_spec.<locals>.<lambda>  s    mr   )rt   rv   varkwru   formatvaluer   )rm   formatargspecr)   r,   r-   rx   r   s    r   rT   FunctionSigPy2.format_arg_spec  sH     $$##((((^^35
 67r; 	;r   )	rx   r)   r*   r,   r"   r.   r+   r-   r&   r   r   r   r   r   FUNC_CLOSURE_ATTRFUNC_CODE_ATTRFUNC_DEFAULTS_ATTRFUNC_GLOBALS_ATTRr$   METHOD_SELF_ATTRr   r1   rF   rT   r   __classcell__r   s   @r   rd   rd      sG     ' N(& N #- bH; ;r   rd   c                   X   ^  \ rS rSrSrSrSrSrSrSr	Sr
\S	4U 4S
 jjrS rS rSrU =r$ )FunctionSigPy3i  a}  Function signature introspector for Python 3.

This supports introspecting functions and generating code for use in
spies when running on Python 3.

There are some differences in function capabilities between Python 3.x
releases (such as the addition of positional-only keyword arguments).
This class provides compatibility for all these versions, currently up
through Python 3.8.
__closure____code____defaults____globals__r   __self__Nc                 8  > [         [        U ]  UUUS9  [        [        S5      (       d  [        S[        R                  SS -  5      eU R                  n[        R                  " U5      (       a$  U R                  U l        UR                  U l        GO9SUR                  ;   Ga(  U[        Lap  X l         U[        L=(       a    [!        X#5      ULU l        U[        L d  [        R*                  " U5      (       a  U R,                  U l        OU R                  U l        OSUR                  ;   a  U R.                  U l        OU R0                  n[        R2                  " U5      nUR                  R5                  S5      SS
  H  n [!        XV5      nM     Ub  U R,                  U l        XPl        [&        R)                  SXR                  5        [        R6                  " USS[        R8                  S9n/ n/ n	/ n
UR:                  R=                  5        H  nUR>                  nUR@                  nXRB                  L aP  URE                  U5        URF                  URH                  L a  U	RE                  U5        Mf  U
RE                  U5        My  XRJ                  L a$  URE                  U5        U	RE                  U5        M  XRL                  L a  U
RE                  U5        M  XRN                  L a  Xl(        M  XRR                  L d  M  Xl*        M     Xl+        Xl,        Xl-        Xpl.        U R_                  5         g! [$         aC    SUR                  ;   a  [&        R)                  SX#5         GN[&        R)                  S	X#5         GNf = f! [$         a    Sn   GM  f = f)aN  Initialize the signature.

Subclasses must override this to parse function types/ownership and
available arguments.

Args:
    func (callable):
        The function to use for the signature.

    owner (type, optional):
        The owning class, as provided when spying on the function.
        This is used only when spying on unbound or slippery methods.

    func_name (str, optional):
        An explicit name for the function. This will be used instead
        of the function's specified name, and is usually a sign of a
        bad decorator.

        Version Added:
            7.0
rk   _signature_from_callablez~Python %s.%s does not have inspect._signature_from_callable, which is needed in order to generate a Signature from a function.Nr   .z<locals>z%r doesn't have a function named "%s". This appears to be a decorator that doesn't preserve function names. Try passing func_name= when setting up the spy.zr%r doesn't have a function named "%s". It's not clear why this is. Try passing func_name= when setting up the spy.r   zqDetermined the owner of %r to be %r, but it may be wrong. Please pass owner= to spy_on() to set a specific owner.F)follow_wrapper_chainsskip_bound_argsigcls)0rl   r   r1   r'   rm   r   sysversion_infor%   rn   r5   r"   r   r&   r   r   r#   r.   rZ   rp   rq   rr   r6   r!   r(   	getmodulesplitr   	Signature
parametersvalueskindnamePOSITIONAL_OR_KEYWORDrG   defaultemptyPOSITIONAL_ONLYKEYWORD_ONLYVAR_POSITIONALr,   VAR_KEYWORDr-   r)   r*   r+   _sigra   )r   r    r&   r%   r(   ry   partsigr|   rt   kwargsparamr   r   r   s                 r   r1   FunctionSigPy3.__init__  s(   , 	nd,$387@ 	- 	B w :;;" ""2A&'( ( NN	( D!!!33DNDJD%%%J&"
.Z/ >1= $& J&'//%*@*@%)%=%=DN%)%;%;DNt000!%!3!3 NN	&00;%2288=crBD'.|'B C  +%)%=%=DN!-J  (  $ZZ	1 .."' $$	& ^^**,E::D::D222%==EKK/KK%MM$'...%D!+++d#---'+$***)-&1 -4 &!	k & .!T%6%66B ". 7 "	..: * '+s*   5!L7 N70N*NNNNc                     SSU0-  $ )r   z5_kgb_l["%(arg)s"] if "%(arg)s" in _kgb_l else %(arg)sargr   rQ   s     r   rF   &FunctionSigPy3.format_forward_call_arg^  s    " G8J
 
 	
r   c           	         / nU R                   R                  R                  5        Hu  nUR                  nUR                  UR
                  L a  X2R                  La  [        nUR                  [        R                  " UR                  UR                  US95        Mw     [        R                  " US9n[        U5      SS $ )a  Format the function's arguments for a new function definition.

This will build a list of parameters for a function definition based on
the argument specification found when introspecting a spied function.
This consists of all positional arguments, positional-only arguments,
keyword arguments, keyword-only arguments, and the special ``*args``
and ``**kwargs`` arguments.

Returns:
    unicode:
    A string representing an argument list for a function definition.
)r   r   r   )r   r   r   )r   r   r   r   r   r   r   r   rG   rm   	Parameterr   r   str)r   r   
orig_paramr   r   s        r   rT   FunctionSigPy3.format_arg_specs  s     
 ))..557J ((G:#C#CC///$g//____! " 8 :63x"~r   )	r   r)   r*   r,   r"   r.   r+   r-   r&   r   r   s   @r   r   r     sG    	 &N'%N!#- Vp
* r   r   r      zUnsupported Python version)r   
__future__r   rm   loggingr   r\   
kgb.errorsr   	kgb.utilsr   	getLoggerrp   objectr   r   r   rd   r   r   FunctionSig	Exceptionr   r   r   <module>r      s    ; '   
  ' , 
		5	!
 
 [
|'f |'~P;_ P;f__ _D A! KaA K
0
11r   