Mercurial > cpython-lru_cache-weakref
diff Lib/test/support.py @ 68544:6b33aa811821 3.1
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.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Tue, 15 Mar 2011 21:02:59 +0100 |
| parents | f8a77fd675f5 |
| children | 92bc1518f20d 1bf4383f190a |
line wrap: on
line diff
--- a/Lib/test/support.py Tue Mar 15 14:01:18 2011 -0400 +++ b/Lib/test/support.py Tue Mar 15 21:02:59 2011 +0100 @@ -1096,3 +1096,36 @@ """ 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)
