
    pij                     l    d Z ddlZddlZddlm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   )graph_break_hints)unimplemented_v2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      g/opt/services/ai/voice_agent/venv/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_cls3   s    (E$6$677r   c                     t        | |fi |S r   )r   )proxyvalueoptionss      r   createz TorchScriptObjectVariable.create7   s    (AAAr   returnc                     t        |   |fi | || _        || j                  j                  j                  d<   || _        y )Nexample_value)super__init__r(   nodemetasource)selfr(   r)   r3   r   	__class__s        r   r0   z"TorchScriptObjectVariable.__init__;   s;    )&)
05

_-r   c                     | j                   S r   )r(   )r4   s    r   as_proxyz"TorchScriptObjectVariable.as_proxyA   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       }|Rt        dd| j                   d	| d
| j                   d| dd| d| j                   dgt        j                         t        |      s t        dd| j                   d	| ddg       |j                  | || j                  |      | |      S )Nr   )call_torchbindr   )
AttrSourcer   ) TorchHigherOrderOperatorVariablez.FakeScriptObject missing method implementationvalue=	, method=zTorchScript object z doesn't define the method .zEnsure the method z is implemented in gb_typecontextexplanationhintsz@Attempted to access non-callable attribute of TorchScript objectzWAttribute accesses of TorchScript objects to non-callable attributes are not supported.z-Use method calls instead of attribute access.)r3   script_obj_varmethod_name)!torch._higher_order_ops.torchbindr:   r3   r;   higher_order_opsr<   getattrr)   r   r   
USER_ERRORcallablemake)r4   txr8   r:   r;   r<   methods          r   var_getattrz%TorchScriptObjectVariable.var_getattrD   s     	E'FT40>H IdV<1$**=XY]X^^_`(.A$**QO&11	 Z IdV<uC	 044dkk40	 5 
 	
r   c                 L    t        dd| j                   d| d| ddg       y )Nz'Weird method call on TorchScript objectr=   r>   zThis particular method call (zn) is not supported (e.g. calling `__setattr__`). Most method calls to TorchScript objects should be supported.zAvoid calling this method.r@   )r   r)   )r4   rM   r8   r   r   s        r   call_methodz%TorchScriptObjectVariable.call_methodn   s@     	=TZZL	$8/v 6P P -
	
r   )r,   N)__name__
__module____qualname__r   dictint__annotations__classmethodtyper&   staticmethodr+   r0   r7   r   strr	   rO   rQ   __classcell__)r5   s   @r   r   r   0   s    HJtC)D$DEJ8t 8 8 B B &F!
C !
O !
!
N &F

r   r   )__doc__r   r#    r   excr   r   r   baser	   user_definedr
   r   r   r   r   r   <module>rb      s5   (     H H ! 3L
 9 L
r   