Commit 99fb3277 authored by Last G's avatar Last G

Replace contextlib with decorator

parent 61143d61
...@@ -10,6 +10,7 @@ import shutil ...@@ -10,6 +10,7 @@ import shutil
import tempfile import tempfile
import site import site
import contextlib import contextlib
import functools
import tarfile import tarfile
import logging import logging
import itertools import itertools
...@@ -41,20 +42,24 @@ from . import contexts ...@@ -41,20 +42,24 @@ from . import contexts
from .textwrap import DALS from .textwrap import DALS
@contextlib.contextmanager def _mock_removing_patcher(obj, attr, newval):
def _mark_removing_patcher(obj, attr, newval): def decorator(fn):
setattr(obj, attr, newval) @functools.wraps(fn)
try: def wrapper(*args, **kwargs):
yield setattr(obj, attr, newval)
finally: try:
delattr(obj, attr) return fn(*args, **kwargs)
finally:
delattr(obj, attr)
return wrapper
return decorator
def magic_patch_object(obj, attr, newval): def magic_patch_object(obj, attr, newval):
if hasattr(obj, attr): if hasattr(obj, attr):
return mock.patch.object(obj, attr, newval) return mock.patch.object(obj, attr, newval)
else: else:
return _mark_removing_patcher(obj, attr, newval) return _mock_removing_patcher(obj, attr, newval)
class FakeDist(object): class FakeDist(object):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment