o
    Á¿i¸  ã                   @   sT  U d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z
 d dlmZ d d	lmZ eeƒZi Zeeef ed
< dededdfdd„Zdedededef ddfdd„Z		d'dedededef deedf deeeef  defdd„Zdedededdfdd„Zd ee defd!d"„Zd ee d#ed$eddfd%d&„ZdS )(é    N)ÚAny)ÚCallable)ÚOptional)ÚFunctionWrapper)Úresolve_path)Ú_gc)Ú
get_logger)ÚModuleWatchdogÚ_DD_ORIGINAL_ATTRIBUTESÚmoduleÚnameÚreturnc              	   C   sr   z%t | |ƒ\}}}||ftv r#t||f }t|||ƒ t||f= W d S W d S  ttfy8   t d| |¡ Y d S w )NzERROR unwrapping %s.%s )r   r
   Úapply_patchÚModuleNotFoundErrorÚAttributeErrorÚlogÚdebug)r   r   ÚparentÚ	attributeÚ_Úoriginal© r   úO/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/appsec/_patch_utils.pyÚ
try_unwrap   s   ýÿr   Úmodule_nameÚwrapper.c                    s(   t  ˆ ¡dtdd f‡ ‡‡fdd„ƒ}d S )Nr   r   c              	      s>   zt | ˆtˆfƒ W d S  ttfy   t dˆ ˆ¡ Y d S w )NzModule %s.%s does not exist)Úwrap_objectr   ÚImportErrorr   r   r   )r   ©r   r   r   r   r   r      s
   ÿz$try_wrap_function_wrapper.<locals>._)r	   Úafter_module_importedr   )r   r   r   r   r   r   r   Útry_wrap_function_wrapper   s    r    r   ÚfactoryÚargsÚkwargsc                    sP   |d u ri }t | |ƒ\}}}||g|¢R i |¤Ž‰ t||ˆ ƒ ‡ fdd„ˆ _ˆ S )Nc                    s   ˆ S )Nr   )Úmemo©r   r   r   Ú<lambda>2   s    zwrap_object.<locals>.<lambda>)r   r   Ú__deepcopy__)r   r   r!   r"   r#   r   r   r   r   r%   r   r   &   s   r   r   r   Úreplacementc              	   C   s‚   z.t | |ƒ}t|tƒs|t| |f< nt|tƒr&t |dd ƒt |dd ƒu r&W d S t| ||ƒ W d S  ttfy@   t| ||ƒ Y d S w )NÚ_self_wrapper)ÚgetattrÚ
isinstancer   r
   ÚsetattrÚ	TypeErrorr   Úpatch_builtins)r   r   r(   Úcurrent_attributer   r   r   r   6   s   


ÿr   Úklassc                 C   s   t  | j¡}|d S )Nr   )ÚgcÚget_referentsÚ__dict__)r0   Úrefsr   r   r   Úpatchable_builtinF   s   r5   ÚattrÚvaluec              	   C   s’   t | ƒ}| |d¡}d| }|||< |r>|||< z|j|| _W n ttfy+   Y nw z|j|| _W n	 ty=   Y nw tj t 	| ¡¡ dS )a@  Based on forbiddenfruit package:
    https://github.com/clarete/forbiddenfruit/blob/master/forbiddenfruit/__init__.py#L421
    ---
    Patch a built-in `klass` with `attr` set to `value`

    This function monkey-patches the built-in python object `attr` adding a new
    attribute to it. You can add any kind of argument to the `class`.

    It's possible to attach methods as class methods, just do the following:

      >>> def myclassmethod(cls):
      ...     return cls(1.5)
      >>> curse(float, "myclassmethod", classmethod(myclassmethod))
      >>> float.myclassmethod()
      1.5

    Methods will be automatically bound, so don't forget to add a self
    parameter to them, like this:

      >>> def hello(self):
      ...     return self * 2
      >>> curse(str, "hello", hello)
      >>> "yo".hello()
      "yoyo"
    Nz_c_%s)
r5   ÚgetÚ__name__r   r-   Ú__qualname__ÚctypesÚ	pythonapiÚPyType_ModifiedÚ	py_object)r0   r6   r7   ÚdiktÚ	old_valueÚold_namer   r   r   r.   K   s"   ÿÿr.   )r   N)r;   Útypingr   r   r   Úwraptr   r   Úddtrace.internal._unpatchedr   r1   Úddtrace.internal.loggerr   Úddtrace.internal.moduler	   r9   r   r
   ÚdictÚ__annotations__Ústrr   r    Útupler   r   Útyper5   r.   r   r   r   r   Ú<module>   s>   
 "ûÿþ
ý
üû
ú"