Your IP : 216.73.216.213
B
��\+ � @ s d Z ddlZG dd� de�ZdS )a�
This is a fork of the pymox library intended to work with Python 3.
The file was modified by quermit@gmail.com and dawid.fatyga@gmail.com
Previously, pyfakefs used just this file from the mox3 library.
However, mox3 will soon be decommissioned, yet standard mock cannot
be used because of the problem described in pyfakefs #182 and
mock issue 250 (https://github.com/testing-cabal/mock/issues/250).
Therefore just this file was forked from mox3 and incorporated
into pyfakefs.
� Nc @ s@ e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� Zdd
� Z dS )�StubOutForTestinga� Sample Usage:
You want os.path.exists() to always return true during testing.
stubs = StubOutForTesting()
stubs.Set(os.path, 'exists', lambda x: 1)
...
stubs.UnsetAll()
The above changes os.path.exists into a lambda that returns 1. Once
the ... part of the code finishes, the UnsetAll() looks up the old value
of os.path.exists and restores it.
c C s g | _ g | _d S )N)�cache�stubs)�self� r �F/opt/alt/python37/lib/python3.7/site-packages/pyfakefs/mox3_stubout.py�__init__. s zStubOutForTesting.__init__c C s | � � | �� d S )N)�smart_unset_all� unset_all)r r r r �__del__2 s zStubOutForTesting.__del__c C s� t �|�st �|�s.||jkr.|}t||�}nnt �|�sJtt �|j��}ntt �|��}|�� d}x6|D ].}y|}t||�}W qj t k
r� wjY qjX qjW |dkr�t d��|j�
|�}|dk r�t|t�r�t|�}| j
�|||f� t|||� dS )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()).
Raises AttributeError if the attribute cannot be found.
NzAttribute not found.)�inspect�ismodule�isclass�__dict__�getattr�list�getmro� __class__�reverse�AttributeError�get�
isinstance�staticmethodr �append�setattr) r �obj� attr_name�new_attrZorig_objZ orig_attr�mro�cls�
old_attributer r r � smart_set6 s.
zStubOutForTesting.smart_setc C s, | j �� x| j D ]}t|� qW g | _ dS )z�Reverses all the SmartSet() calls.
Restores things to their original definition. Its okay to call
SmartUnsetAll() repeatedly, as later calls have no effect if no
SmartSet() calls have been made.
N)r r r )r �argsr r r r n s
z!StubOutForTesting.smart_unset_allc C sh t ||�}|j�|�}|dk rFt|t�r2t|�}nt|t�rFt|j�}| j�|||f� t |||� dS )a Replace child_name's old definition with new_child.
Replace definition in the context of the given parent. 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 unset_all() is called.
This method supports the case where child_name is a staticmethod or a
classmethod of parent.
N)
r r r r r �classmethod�__func__r r r )r �parent�
child_name� new_child� old_childr r r r �set| s
zStubOutForTesting.setc C s6 | j �� x | j D ]\}}}t|||� qW g | _ dS )z�Reverses all the Set() calls.
Restores things to their original definition. Its okay to call
unset_all() repeatedly, as later calls have no effect if no Set()
calls have been made.
N)r r r )r r% r( r&