summaryrefslogtreecommitdiffstats
path: root/Lib/test/support.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-03-15 20:02:59 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-03-15 20:02:59 (GMT)
commit2c50a09ac47e701768a4e20f8a03d17263e8db8f (patch)
treec825f5ed587fe4229ec825f3371556738e284dd2 /Lib/test/support.py
parent7dedcb46441803444a3bdb0f9773d9d9b10186e9 (diff)
downloadcpython-2c50a09ac47e701768a4e20f8a03d17263e8db8f.zip
cpython-2c50a09ac47e701768a4e20f8a03d17263e8db8f.tar.gz
cpython-2c50a09ac47e701768a4e20f8a03d17263e8db8f.tar.bz2
On behalf of Tarek: Issue #11501: disutils.archive_utils.make_zipfile no
longer fails if zlib is not installed. Instead, the zipfile.ZIP_STORED compression is used to create the ZipFile. Patch by Natalia B. Bidart.
Diffstat (limited to 'Lib/test/support.py')
-rw-r--r--Lib/test/support.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py
index 9fb3ee0..bef7161 100644
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -1096,3 +1096,36 @@ def strip_python_stderr(stderr):
"""
stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
return stderr
+
+def patch(test_instance, object_to_patch, attr_name, new_value):
+ """Override 'object_to_patch'.'attr_name' with 'new_value'.
+
+ Also, add a cleanup procedure to 'test_instance' to restore
+ 'object_to_patch' value for 'attr_name'.
+ The 'attr_name' should be a valid attribute for 'object_to_patch'.
+
+ """
+ # check that 'attr_name' is a real attribute for 'object_to_patch'
+ # will raise AttributeError if it does not exist
+ getattr(object_to_patch, attr_name)
+
+ # keep a copy of the old value
+ attr_is_local = False
+ try:
+ old_value = object_to_patch.__dict__[attr_name]
+ except (AttributeError, KeyError):
+ old_value = getattr(object_to_patch, attr_name, None)
+ else:
+ attr_is_local = True
+
+ # restore the value when the test is done
+ def cleanup():
+ if attr_is_local:
+ setattr(object_to_patch, attr_name, old_value)
+ else:
+ delattr(object_to_patch, attr_name)
+
+ test_instance.addCleanup(cleanup)
+
+ # actually override the attribute
+ setattr(object_to_patch, attr_name, new_value)