mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 16:34:47 +01:00
added dummy swap
This commit is contained in:
parent
8cf955ecb5
commit
264c48c0b1
1 changed files with 22 additions and 0 deletions
|
@ -7,6 +7,7 @@ import ast
|
|||
import builtins
|
||||
import platform
|
||||
import subprocess
|
||||
import contextlib
|
||||
from collections import defaultdict
|
||||
from collections.abc import MutableMapping
|
||||
|
||||
|
@ -106,6 +107,27 @@ class DummyEnv(MutableMapping):
|
|||
def __iter__(self):
|
||||
yield from self._d
|
||||
|
||||
@contextlib.contextmanager
|
||||
def swap(self, other=None, **kwargs):
|
||||
old = {}
|
||||
# single positional argument should be a dict-like object
|
||||
if other is not None:
|
||||
for k, v in other.items():
|
||||
old[k] = self.get(k, NotImplemented)
|
||||
self[k] = v
|
||||
# kwargs could also have been sent in
|
||||
for k, v in kwargs.items():
|
||||
old[k] = self.get(k, NotImplemented)
|
||||
self[k] = v
|
||||
yield self
|
||||
# restore the values
|
||||
for k, v in old.items():
|
||||
if v is NotImplemented:
|
||||
del self[k]
|
||||
else:
|
||||
self[k] = v
|
||||
|
||||
|
||||
#
|
||||
# Execer tools
|
||||
#
|
||||
|
|
Loading…
Add table
Reference in a new issue