
    AVh"                        d Z ddlZddlZddlZddlZddlmZ ddl 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j*                  ZeZdad ZddZd Zd Zd Z edg       G d de             Zy)z@Imports absltest as a replacement for testing.pybase.googletest.    N)app)*)errors)file_io)	benchmark)
tf_logging)tf_decorator)
tf_inspect)	tf_export c                     t        |        y)zDelegate to absltest.main.argvN)absltest_mainr   s    U/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/platform/googletest.pyg_mainr   0   s     T    c                 >      fd}t        j                  |        y )Nc                  b    } | t         j                  } t        j                  t        |       S )N)mainr   )sysr   r   runr   )argsr   s    r   main_wrapperzmain.<locals>.main_wrapper8   s'    D|XXd77T**r   )	true_mainr   )r   benchmarks_main)r   r   s   ` r   r   r   7   s    + l>r   c                  d   t         s$t        j                  j                  d      r(t	        j
                  t        j                  d         } nt        j                         d   d   }t        j                  j                  t	        j                         t        j                  j                  t        j                  |                  } t	        j
                  | j                  d            } | j                  dt        j                        } | fd}t!        j"                  |       | a t         S )z.Return a temporary directory for tests to use.TEST_TMPDIR)prefixr   z.py/c                     	 t        j                  |        y # t        j                  $ r!}t	        j
                  d| |       Y d }~y d }~ww xY w)NzError removing %s: %s)r   delete_recursivelyr   OpErrorloggingerror)dirnamees     r   delete_temp_dirz#GetTempDir.<locals>.delete_temp_dirP   s=    ;""7+^^ ;-w::;s    AAA)_googletest_temp_dirosenvirongettempfilemkdtempr
   stackpathjoin
gettempdirbasenamegetfilerstripreplacesepatexitregister)temp_dirfirst_framer)   s      r   
GetTempDirr=   A   s     
	zz~~m$!!M)BCh$$&r*1-kh113 gg..z/A/A+/NOQh!!)?@h RVV,H ( ; OOO$#	r   c                 f    t         j                  j                  t         j                  d   d|       S )zCreates an absolute test srcdir path given a relative path.

  Args:
    relative_path: a path relative to tensorflow root.
      e.g. "contrib/session_bundle/example".

  Returns:
    An absolute path to the linked in runfiles.
  TEST_SRCDIRzorg_tensorflow/tensorflow)r+   r1   r2   r,   )relative_paths    r   test_src_dir_pathrA   ]   s+     
bjj/1=
B Br   c                       y)NF rC   r   r   StatefulSessionAvailablerD   k   s    	r   ztest.StubOutForTesting)v1c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)StubOutForTestinga  Support class for stubbing methods out for unit testing.

  Sample Usage:

  You want os.path.exists() to always return true during testing.

     stubs = StubOutForTesting()
     stubs.Set(os.path, 'exists', lambda x: 1)
       ...
     stubs.CleanUp()

  The above changes os.path.exists into a lambda that returns 1.  Once
  the ... part of the code finishes, the CleanUp() looks up the old
  value of os.path.exists and restores it.
  c                      g | _         g | _        y N)cachestubsselfs    r   __init__zStubOutForTesting.__init__   s    DJDJr   c                 $    | j                          y)zDo not rely on the destructor to undo your stubs.

    You cannot guarantee exactly when the destructor will get called without
    relying on implementation details of a Python VM that may change.
    NCleanUprL   s    r   __del__zStubOutForTesting.__del__   s     	LLNr   c                     | S rI   rC   rL   s    r   	__enter__zStubOutForTesting.__enter__   s    Kr   c                 $    | j                          y rI   rP   )rM   unused_exc_typeunused_exc_value	unused_tbs       r   __exit__zStubOutForTesting.__exit__   s    LLNr   c                 D    | j                          | j                          y)zDUndoes all SmartSet() & Set() calls, restoring original definitions.N)SmartUnsetAllUnsetAllrL   s    r   rQ   zStubOutForTesting.CleanUp   s    MMOr   c                    t        j                  |      \  }}t        j                  |      s#t        j                  |      s||j
                  v r|}t        ||      }nt        j                  |      s)t        t        j                  |j                              }nt        t        j                  |            }|j                          d}d}|D ]  }		 |	}t        ||      }d} |st        d      |j
                  j                  |      }
|
t        |
t              rt        |      }| j                  j!                  ||f       t#        |||       y# t        $ r Y w xY w)a"  Replace obj.attr_name with new_attr.

    This method is smart and works at the module, class, and instance level
    while preserving proper inheritance. It will not stub out C types however
    unless that has been explicitly allowed by the type.

    This method supports the case where attr_name is a staticmethod or a
    classmethod of obj.

    Notes:
      - If obj is an instance, then it is its class that will actually be
        stubbed. Note that the method Set() does not do that: if obj is
        an instance, it (and not its class) will be stubbed.
      - The stubbing is using the builtin getattr and setattr. So, the __get__
        and __set__ will be called when stubbing (TODO: A better idea would
        probably be to manipulate obj.__dict__ instead of getattr() and
        setattr()).

    Args:
      obj: The object whose attributes we want to modify.
      attr_name: The name of the attribute to modify.
      new_attr: The new value for the attribute.

    Raises:
      AttributeError: If the attribute cannot be found.
    NFTzAttribute not found.)r	   unwrapr
   ismoduleisclass__dict__getattrlistgetmro	__class__reverseAttributeErrorr-   
isinstancestaticmethodrK   appendsetattr)rM   obj	attr_namenew_attr_orig_obj	orig_attrmro
found_attrclsold_attributes              r   SmartSetzStubOutForTesting.SmartSet   sB   6   %FAsC $cll)Bh#y)i$:$$S]]34:$$S)*	kkmij #	(c9-)*	 344 LL$$Y/M Z|%Ly)iJJxI67Hi*  	
	s   E	E%$E%c                 V    t        | j                        D ]
  }t        |   g | _        y)aK  Reverses SmartSet() calls, restoring things to original definitions.

    This method is automatically called when the StubOutForTesting()
    object is deleted; there is no need to call it explicitly.

    It is okay to call SmartUnsetAll() repeatedly, as later calls have
    no effect if no SmartSet() calls have been made.
    N)reversedrK   rk   )rM   r   s     r   r[   zStubOutForTesting.SmartUnsetAll   s,     $ tn DJr   c                     t        ||      }|j                  j                  |      }|t        |t              rt	        |      }| j
                  j                  |||f       t        |||       y)a  In parent, replace child_name's old definition with new_child.

    The parent could be a module when the child is a function at
    module scope.  Or the parent could be a class when a class' method
    is being replaced.  The named child is set to new_child, while the
    prior definition is saved away for later, when UnsetAll() is
    called.

    This method supports the case where child_name is a staticmethod or a
    classmethod of parent.

    Args:
      parent: The context in which the attribute child_name is to be changed.
      child_name: The name of the attribute to change.
      new_child: The new value of the attribute.
    N)rb   ra   r-   rh   ri   rJ   rj   rk   )rM   parent
child_name	new_child	old_childru   s         r   SetzStubOutForTesting.Set   sb    " 
+IOO''
3M Z|%Ly)iJJvy*56FJ	*r   c                 h    t        | j                        D ]  \  }}}t        |||        g | _        y)aB  Reverses Set() calls, restoring things to their original definitions.

    This method is automatically called when the StubOutForTesting()
    object is deleted; there is no need to call it explicitly.

    It is okay to call UnsetAll() repeatedly, as later calls have no
    effect if no Set() calls have been made.
    N)rx   rJ   rk   )rM   rz   r}   r{   s       r   r\   zStubOutForTesting.UnsetAll   s6     ,4DJJ+? -'Jfj),-DJr   N)__name__
__module____qualname____doc__rN   rR   rT   rY   rQ   rv   r[   r~   r\   rC   r   r   rG   rG   o   s5     
=+~+4r   rG   rI   ) r   r9   r+   r   r.   abslr   absl.testing.absltesttensorflow.python.frameworkr   tensorflow.python.lib.ior   tensorflow.python.platformr   r   r%   tensorflow.python.utilr	   r
    tensorflow.python.util.tf_exportr   TensorFlowBenchmark	Benchmarkr   r   r*   r   r=   rA   rD   objectrG   rC   r   r   <module>r      s     G  	 
   # / , 0 < / - 6 ))	  
?8B '()] ] *]r   