forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new
backup_attributes
context manager for tests
This context manager will makes it possible to rollback an object state after leaving the context manager. This is a follow up of <kivy#1867 (comment)> Also address docstring format as suggested by @opacam in <kivy#1933 (comment)>
- Loading branch information
1 parent
3dabded
commit a37cf36
Showing
4 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from copy import deepcopy | ||
from contextlib import contextmanager | ||
|
||
|
||
@contextmanager | ||
def backup_attributes(obj, attributes): | ||
""" | ||
Makes a backup of the object attributes that gets restored later. | ||
""" | ||
obj_dict = obj.__dict__ | ||
# creates a subset dictionary of the attributes we're interested in | ||
attributes_backup = dict( | ||
(k, obj_dict[k]) for k in attributes if k in obj_dict) | ||
attributes_backup = deepcopy(attributes_backup) | ||
try: | ||
yield | ||
finally: | ||
for attribute in attributes: | ||
setattr(obj, attribute, attributes_backup[attribute]) |