mirror of
https://github.com/python/cpython.git
synced 2025-07-28 21:55:21 +00:00
Restore original sys.path when running TTK tests
This commit is contained in:
parent
e363b1434d
commit
7df72dcdf9
3 changed files with 33 additions and 8 deletions
|
@ -578,6 +578,31 @@ class EnvironmentVarGuard(UserDict.DictMixin):
|
|||
self._environ[k] = v
|
||||
|
||||
|
||||
class DirsOnSysPath(object):
|
||||
"""Context manager to temporarily add directories to sys.path.
|
||||
|
||||
This makes a copy of sys.path, appends any directories given
|
||||
as positional arguments, then reverts sys.path to the copied
|
||||
settings when the context ends.
|
||||
|
||||
Note that *all* sys.path modifications in the body of the
|
||||
context manager, including replacement of the object,
|
||||
will be reverted at the end of the block.
|
||||
"""
|
||||
|
||||
def __init__(self, *paths):
|
||||
self.original_value = sys.path[:]
|
||||
self.original_object = sys.path
|
||||
sys.path.extend(paths)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *ignore_exc):
|
||||
sys.path = self.original_object
|
||||
sys.path[:] = self.original_value
|
||||
|
||||
|
||||
class TransientResource(object):
|
||||
|
||||
"""Raise ResourceDenied if an exception is raised while the context manager
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue