o
    ?e"                     @   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   f/home/www/facesmatcher.com/pyenv/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)Z	true_mainr   )r   Z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.Z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   Zdelete_recursivelyr   Z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_dirZfirst_framer   r   r   r   
GetTempDirA   s   
r1   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.
  ZTEST_SRCDIRzorg_tensorflow/tensorflow)r    r&   r'   r!   )Zrelative_pathr   r   r   test_src_dir_path]   s   
r2   c                   C   s   dS )NFr   r   r   r   r   StatefulSessionAvailablek      r3   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CleanUpr:   r   r   r   __del__   s   zStubOutForTesting.__del__c                 C   s   | S r7   r   r:   r   r   r   	__enter__   r4   zStubOutForTesting.__enter__c                 C   s   |    d S r7   r=   )r;   Zunused_exc_typeZunused_exc_valueZ	unused_tbr   r   r   __exit__   s   zStubOutForTesting.__exit__c                 C   s   |    |   dS )zDUndoes all SmartSet() & Set() calls, restoring original definitions.N)SmartUnsetAllUnsetAllr:   r   r   r   r>      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staticmethodr9   appendsetattr)r;   objZ	attr_namenew_attr_Zorig_objZ	orig_attrmroZ
found_attrclsold_attributer   r   r   SmartSet   s:   


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)reversedr9   rQ   )r;   r   r   r   r   rB      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)rH   rG   r"   rN   rO   r8   rP   rQ   )r;   parent
child_name	new_child	old_childrW   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)rY   r8   rQ   )r;   rZ   r]   r[   r   r   r   rC      s   
zStubOutForTesting.UnsetAllN)__name__
__module____qualname____doc__r<   r?   r@   rA   r>   rX   rB   r^   rC   r   r   r   r   r6   o   s    	?r6   r7   ) rb   r.   r    r   r#   Zabslr   Zabsl.testing.absltestZtensorflow.python.frameworkr   Ztensorflow.python.lib.ior   Ztensorflow.python.platformr   r   r   Ztensorflow.python.utilr   r	   Z tensorflow.python.util.tf_exportr
   ZTensorFlowBenchmarkZ	Benchmarkr   r   r   r   r1   r2   r3   objectr6   r   r   r   r   <module>   s0   


