o
    ۰i                     @   s   d dl Z d dlmZmZmZmZmZmZmZm	Z	 d dl
mZ 	ddedee defddZd	edee fd
dZdd Zdd ZddefddZdS )    N)ListTupleUnionOptionalCallableAnyDictSet)CodeTypecode_objoverwite_fn_namereturnc                 C   s   | j | j }| jd | }dd |D }dd l}| j|j@ r,|d| j|   |d7 }| j|j@ r@|d| j|   |d7 }d|}|d urK|n| j	}d| d	| d
}|S )Nc                 S   s&   g | ]}| d s|n|d dqS ).	comp_arg_)
startswithreplace).0x r   ?/home/ubuntu/.local/lib/python3.10/site-packages/depyf/utils.py
<listcomp>   s    z*get_function_signature.<locals>.<listcomp>r   *   z**z, zdef (z):
)
co_argcountco_kwonlyargcountco_varnamesinspectco_flags
CO_VARARGSappendCO_VARKEYWORDSjoinco_name)r   r   normal_arg_count	arg_namesr   args_strfn_nameheaderr   r   r   get_function_signature   s    
r)   codec                 C   s2   | g}| j D ]}t|t| r|t| q|S N)	co_consts
isinstancetypeextendcollect_all_code_objects)r*   code_objectsconstr   r   r   r0      s   
r0   c              
   C   sR   dd l }z
|j| dd W d S  ty( } z|j| s W Y d }~d S d }~ww )Nr   T)exist_ok)osmakedirsOSErrorpathisdir)r7   r4   er   r   r   safe_create_directory&   s   r:   c                 C   sj   ddl }	 t| dr| j} n&t| dr| j} nt| |jr!| j} nt| dr1t| jdr1| jj} n	 | S q)a  A callable object `fn` might have a __code__ attribute, which is a code object.
    However, `fn` might not be the owner of the code object. Only the code owner can change the code object.
    This function returns the owner of the code object.
    An example:
    class A:
        def func(self):
            return 1
    a = A()
    `a.func.__code__` is read-only. `A.func.__code__` is writable.
    We can change the code object via `a.func.__func__.__code__`.
    r   NT__func____wrapped____call__)	functoolshasattrr;   r<   r-   partialfuncr=   )fnr>   r   r   r   get_code_owner1   s   


rC   rB   c              
   C   sh   dd l }ddlm} z|| j|d}W |S  |y3 } zt| |d}|d }W Y d }~|S d }~ww )Nr   )DecompilationError)r   z    'Failed to decompile.'
)depyfdepyf.decompilerrD   
Decompiler	decompiler)   )rB   r   rE   rD   decompiled_source_coder9   r(   r   r   r   decompile_ensureQ   s   rJ   r+   )distypingr   r   r   r   r   r   r   r	   typesr
   strr)   r0   r:   rC   rJ   r   r   r   r   <module>   s    (
 