
    Vh                     `    d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
 d Z G d	 d
e
      Zy)a  
This module implements variable tracking for TorchScript objects during Dynamo tracing.

The TorchScriptObjectVariable class provides specialized handling for TorchScript
objects with strong safety guarantees by:
- Enforcing method-call-only access to prevent unsafe attribute manipulation
- Converting graph breaks into hard errors via _raise_hard_error_if_graph_break
- Proper proxy and source tracking for TorchScript method calls
- Integration with higher-order operators for method call handling

Key safety features:
- Strict validation that only method calls are allowed (no direct attribute access)
- Immediate error reporting for potentially unsafe operations
- Proper source tracking for debugging and guard installation
- Safe handling of TorchScript object method calls through torchbind

The module ensures that TorchScript objects are handled safely during tracing
by limiting operations to known-safe patterns and failing fast for unsafe usage.
    N   )unimplementedUnsafeScriptObjectErrorUnsupported   )VariableTracker)UserDefinedObjectVariablec                     d }|S )Nc                 B     t        j                          fd       }|S )Nc                  d    	  | i |S # t         $ r}t        |j                        |d }~ww xY wN)r   r   msg)argskwargsefns      U/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/variables/script_object.pygraph_break_as_hard_errorzQ_raise_hard_error_if_graph_break.<locals>.deco.<locals>.graph_break_as_hard_error#   s9    <4*6** <-aee4!;<s    	/*/)	functoolswraps)r   r   s   ` r   decoz._raise_hard_error_if_graph_break.<locals>.deco"   s&    			< 
	< )(     )reasonr   s     r    _raise_hard_error_if_graph_breakr   !   s    ) Kr   c                        e Zd ZU i Zeed f   ed<   edefd       Z	e
d        Zd fdZd Z ed      d	edefd
       Z ed      d        Z xZS )TorchScriptObjectVariable_fake_script_object_cacheuser_clsc                 6    t        |t        j                        S r   )
issubclasstorchScriptObject)clsr   s     r   is_matching_clsz)TorchScriptObjectVariable.is_matching_cls2   s    (E$6$677r   c                     t        | |fi |S r   )r   )proxyvalueoptionss      r   createz TorchScriptObjectVariable.create6   s    (AAAr   returnc                     t        |   |fi | || _        || j                  j                  j                  d<   || _        y )Nexample_value)super__init__r'   nodemetasource)selfr'   r(   r2   r   	__class__s        r   r/   z"TorchScriptObjectVariable.__init__:   s;    )&)
05

_-r   c                     | j                   S r   )r'   )r3   s    r   as_proxyz"TorchScriptObjectVariable.as_proxy@   s    zzr   z<Dynamo cannot safely trace script object due to graph break.namec                     ddl m} ddlm} ddlm} t        | j                  |d       }|t        d| d       t        |      st        d	       |j                  | || j                  |      | |
      S )Nr   )call_torchbindr   )
AttrSourcer   ) TorchHigherOrderOperatorVariablez'FakeScriptObject doesn't define method z3. Did you forget to implement it in the fake class?zvOnly method calls on TorchScript objects can be supported safely. Please use method calls instead of attribute access.)r2   script_obj_varmethod_name)!torch._higher_order_ops.torchbindr9   r2   r:   higher_order_opsr;   getattrr(   r   callablemake)r3   txr7   r9   r:   r;   methods          r   var_getattrz%TorchScriptObjectVariable.var_getattrC   s     	E'FT40>9$?rs H
 044dkk40	 5 
 	
r   c                 "    t        d| d       y )Nzcall method z on script object is not safe.)r   )r3   rC   r7   r   r   s        r   call_methodz%TorchScriptObjectVariable.call_methodc   s     	TF*HIJr   )r+   N)__name__
__module____qualname__r   dictint__annotations__classmethodtyper%   staticmethodr*   r/   r6   r   strr   rE   rG   __classcell__)r4   s   @r   r   r   /   s    HJtC)D$DEJ8t 8 8 B B &F
C 
O 

: &FKKr   r   )__doc__r   r"   excr   r   r   baser   user_definedr	   r   r   r   r   r   <module>rW      s2   (   E E ! 38K 9 8Kr   