bpo-30197: Enhance functions swap_attr() and swap_item() in test.support. (#1341)

* bpo-30197: Enhance functions swap_attr() and swap_item() in test.support.

They now work when delete replaced attribute or item inside the with
statement.  The old value of the attribute or item (or None if it doesn't
exist) now will be assigned to the target of the "as" clause, if there is
one.

* Update docstrings.
This commit is contained in:
Serhiy Storchaka 2017-04-28 19:17:26 +03:00 committed by Victor Stinner
parent 80a3da4d4a
commit d1a1def7bf
4 changed files with 43 additions and 13 deletions

View file

@ -273,13 +273,12 @@ class TestGetDefaultTempdir(BaseTestCase):
tempfile._get_default_tempdir()
self.assertEqual(os.listdir(our_temp_directory), [])
open = io.open
def bad_writer(*args, **kwargs):
fp = open(*args, **kwargs)
fp = orig_open(*args, **kwargs)
fp.write = raise_OSError
return fp
with support.swap_attr(io, "open", bad_writer):
with support.swap_attr(io, "open", bad_writer) as orig_open:
# test again with failing write()
with self.assertRaises(FileNotFoundError):
tempfile._get_default_tempdir()