o
    2h"                     @   s   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T 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d ZdddZdd Zdd Zdd ZedgdG dd deZdS )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                 C   s   t | d dS )zDelegate to absltest.main.argvN)absltest_mainr    r   `/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/platform/googletest.pyg_main0   s   r   c                    s    fdd}t j| d d S )Nc                     s     } | d u r	t j} tjt| dS )N)mainr   )sysr   r   runr   )argsr   r   r   main_wrapper8   s   zmain.<locals>.main_wrapper)	true_mainr   )r   benchmarks_main)r   r   r   r   r   r   7   s   r   c                  C   s   t sGtjdrtjtjd d} n!t d d }tj	t
 tjt|} tj| dd} | dtj} | fdd}t| | a t S )	z.Return a temporary directory for tests to use.TEST_TMPDIR)prefixr   z.py/c              
   S   sH   zt |  W d S  tjy# } ztd| | W Y d }~d S d }~ww )NzError removing %s: %s)r   delete_recursivelyr   OpErrorloggingerror)dirnameer   r   r   delete_temp_dirP   s   z#GetTempDir.<locals>.delete_temp_dir)_googletest_temp_dirosenvirongettempfilemkdtempr	   stackpathjoin
gettempdirbasenamegetfilerstripreplacesepatexitregister)temp_dirfirst_framer#   r   r   r   
GetTempDirA   s   
r7   c                 C   s   t 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%   r+   r,   r&   )relative_pathr   r   r   test_src_dir_path]   s   
r:   c                   C   s   dS )NFr   r   r   r   r   StatefulSessionAvailablek      r;   ztest.StubOutForTesting)v1c                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )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                 C   s   g | _ g | _d S N)cachestubsselfr   r   r   __init__   s   
zStubOutForTesting.__init__c                 C   s   |    dS )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CleanUprB   r   r   r   __del__   s   zStubOutForTesting.__del__c                 C   s   | S r?   r   rB   r   r   r   	__enter__   r<   zStubOutForTesting.__enter__c                 C   s   |    d S r?   rE   )rC   unused_exc_typeunused_exc_value	unused_tbr   r   r   __exit__   s   zStubOutForTesting.__exit__c                 C   s   |    |   dS )zDUndoes all SmartSet() & Set() calls, restoring original definitions.N)SmartUnsetAllUnsetAllrB   r   r   r   rF      s   zStubOutForTesting.CleanUpc              	   C   s   t |\}}t|st|s||jv r|}t||}n<t|s,tt|j	}ntt|}|
  d}d}|D ]}	z|	}t||}d}W q= tyS   Y q=w |sZtd|j|}
|
durmt|
trmt|}| j|||f t||| dS )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staticmethodrA   appendsetattr)rC   obj	attr_namenew_attr_orig_obj	orig_attrmro
found_attrclsold_attributer   r   r   SmartSet   s8   



zStubOutForTesting.SmartSetc                 C   s"   t | jD ]}t|  qg | _dS )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)reversedrA   r\   )rC   r   r   r   r   rM      s   	

zStubOutForTesting.SmartUnsetAllc                 C   sR   t ||}|j|}|durt|trt|}| j|||f t||| dS )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)rS   rR   r'   rY   rZ   r@   r[   r\   )rC   parent
child_name	new_child	old_childrf   r   r   r   Set   s   
zStubOutForTesting.Setc                 C   s,   t | jD ]\}}}t||| qg | _dS )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)rh   r@   r\   )rC   ri   rl   rj   r   r   r   rN      s   
zStubOutForTesting.UnsetAllN)__name__
__module____qualname____doc__rD   rG   rH   rL   rF   rg   rM   rm   rN   r   r   r   r   r>   o   s    	?r>   r?   ) rq   r3   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   r7   r:   r;   objectr>   r   r   r   r   <module>   s0   


