monkeypatch: ensure the internal _savesyspath attribute always exists

This commit is contained in:
Ronny Pfannschmidt 2015-09-14 07:42:23 +02:00
parent c7888d1d97
commit 2cfc183029
1 changed files with 4 additions and 3 deletions

View File

@ -72,6 +72,7 @@ class monkeypatch:
self._setattr = [] self._setattr = []
self._setitem = [] self._setitem = []
self._cwd = None self._cwd = None
self._savesyspath = None
def setattr(self, target, name, value=notset, raising=True): def setattr(self, target, name, value=notset, raising=True):
""" Set attribute value on target, memorizing the old value. """ Set attribute value on target, memorizing the old value.
@ -173,7 +174,7 @@ class monkeypatch:
def syspath_prepend(self, path): def syspath_prepend(self, path):
""" Prepend ``path`` to ``sys.path`` list of import locations. """ """ Prepend ``path`` to ``sys.path`` list of import locations. """
if not hasattr(self, '_savesyspath'): if self._savesyspath is None:
self._savesyspath = sys.path[:] self._savesyspath = sys.path[:]
sys.path.insert(0, str(path)) sys.path.insert(0, str(path))
@ -217,9 +218,9 @@ class monkeypatch:
else: else:
dictionary[name] = value dictionary[name] = value
self._setitem[:] = [] self._setitem[:] = []
if hasattr(self, '_savesyspath'): if self._savesyspath is not None:
sys.path[:] = self._savesyspath sys.path[:] = self._savesyspath
del self._savesyspath self._savesyspath = None
if self._cwd is not None: if self._cwd is not None:
os.chdir(self._cwd) os.chdir(self._cwd)