o
    ci                      @   s   d Z ddlZddlmZ ddlmZmZmZmZ ddl	Z	ddl
Z
ddlmZ ddlmZ edd ZG dd	 d	ZeG d
d dZdd Zdd Ze			ddedee dedee deeee f f
ddZdeeee f fddZdS )z-A utility for debugging serialization issues.    N)contextmanager)AnyOptionalSetTuple)DeveloperAPIc                 c   s(    |  j d7  _ d V  |  j d8  _ d S )N   )level)printer r   L/home/ubuntu/.local/lib/python3.10/site-packages/ray/util/check_serialize.py_indent   s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_Printerc                 C   s   d| _ || _d S )Nr   )r	   
print_file)selfr   r   r   r   __init__   s   
z_Printer.__init__c                 C   s   t | S N)r   r   r   r   r   indent   s   z_Printer.indentc                 C   s    d| j  }t|| | jd d S )Nz    )file)r	   printr   )r   msgr   r   r   r   r      s   
z_Printer.printN)__name__
__module____qualname__r   r   r   r   r   r   r   r      s    r   c                   @   s.   e Zd ZdZdededefddZdd Zd	S )
FailureTuplezRepresents the serialization 'frame'.

    Attributes:
        obj: The object that fails serialization.
        name: The variable name of the object.
        parent: The object that references the `obj`.
    objnameparentc                 C   s   || _ || _|| _d S r   )r   r   r   )r   r   r   r   r   r   r   r   +   s   
zFailureTuple.__init__c                 C   s   d| j  d| j d| j dS )Nz
FailTuple(z [obj=z	, parent=z]))r   r   r   r   r   r   r   __repr__0   s   zFailureTuple.__repr__N)r   r   r   __doc__r   strr   r   r   r   r   r   r   !   s    r   c              
   C   sF  t | sJ t | }d}|jrR|dt|j d | ) |j D ]\}}t|||d |||d\}	}
|p=|	 }|rB nq'W d   n1 sMw   Y  |j	r|dt|j	 d | ) |j	 D ]\}}t|||d |||d\}	}
|p|	 }|r nqkW d   n1 sw   Y  |s|d|  d	 |S )
AAdds the first-found non-serializable element to the failure_set.Fz	Detected z. global variables. Checking serializability...r   r   depthr   failure_setr
   Nz0 nonlocal variables. Checking serializability...1WARNING: Did not find non-serializable object in . This may be an oversight.)
inspect
isfunctiongetclosurevarsglobalsr   lenr   items_inspect_serializability	nonlocals)base_objr$   r   r%   r
   closurefoundr   r   serializable_r   r   r   _inspect_func_serialization4   s`   







r5   c              
   C   s.  t | rJ t j| t jd}d}| & |D ]\}}t|||d |||d\}	}
|p.|	 }|r3 nqW d   n1 s>w   Y  | ; t | }|D ]+\}}|dr]|dsbt |rcqOt|||d |||d\}	}
|pu|	 }|rz nqOW d   n1 sw   Y  |s|d|  d |S )	r"   )	predicateFr   r#   N__r&   r'   )	r(   r)   
getmembersr   r.   
startswithendswith	isbuiltinr   )r0   r$   r   r%   r
   	functionsr2   r   r   r3   r4   membersr   r   r   _inspect_generic_serializationg   sV   







r>      r0   r   r$   r   returnc                 C   s   t |}t| ||dd|S )a  Identifies what objects are preventing serialization.

    Args:
        base_obj: Object to be serialized.
        name: Optional name of string.
        depth: Depth of the scope stack to walk through. Defaults to 3.
        print_file: file argument that will be passed to print().

    Returns:
        bool: True if serializable.
        set[FailureTuple]: Set of unserializable objects.

    .. versionadded:: 1.1.0

    N)r   r.   )r0   r   r$   r   r
   r   r   r   inspect_serializability   s   rA   c                 C   sD  t   d}d}d}|d u r>d}t }d|  }|dtt|d  || |dtt|d  |d u r=t| }n|d| d|  d	 z
t|  d|fW S  t	y }	 z2|t j
j d
t j
j d|	  d}z|dkr}|t| || W n	 t	y   Y nw W Y d }	~	nd }	~	ww |dkrd|fS t| rt| || ||d n	t| || ||d |s|t| || |r|dtt|d  |s|d n&dt jj ddd |D  t jj d }
|d|
 d |d |dtt|d  |d |d |dtt|d  | |fS )NF TzChecking Serializability of =P   zSerializing 'z' z...z!!! FAILz serialization: r   )r$   r   r%   r
   zTNothing failed the inspect_serialization test, though serialization did not succeed.z

	
c                 s   s    | ]}t |V  qd S r   )r!   ).0kr   r   r   	<genexpr>   s    z+_inspect_serializability.<locals>.<genexpr>z

z
Variable: zowas found to be non-serializable. There may be multiple other undetected variables that were non-serializable. zConsider either removing the instantiation/imports of these variables or moving the instantiation into the scope of the function/class. zmCheck https://docs.ray.io/en/master/ray-core/objects/serialization.html#troubleshooting for more information.zIf you have any suggestions on how to improve this error message, please reach out to the Ray developers on github.com/ray-project/ray/issues/)coloramainitsetr   minr,   r!   cpdumps	ExceptionForeREDRESETaddr   r(   r)   r5   r>   StyleBRIGHTjoin	RESET_ALL)r0   r   r$   r   r%   r
   	top_leveldeclarationr2   e	fail_varsr   r   r   r.      s   






r.   )Nr?   N)r    r(   
contextlibr   typingr   r   r   r   rayrI   ray.cloudpicklecloudpicklerM   ray.util.annotationsr   r   r   r   r5   r>   r!   intboolrA   r.   r   r   r   r   <module>   sB    
3+