diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-09-30 19:20:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-30 19:20:39 +0300 |
commit | ec4d917a6a68824f1895f75d113add9410283da7 (patch) | |
tree | 5e6e616f4be55cfef8928882f50ce37e6709fbb8 /Lib/test/test_xml_etree.py | |
parent | b07fddd527efe67174ce6b0fdbe8dac390b16e4e (diff) | |
download | cpython-ec4d917a6a68824f1895f75d113add9410283da7.tar.gz cpython-ec4d917a6a68824f1895f75d113add9410283da7.zip |
bpo-40173: Fix test.support.import_helper.import_fresh_module() (GH-28654)
* Work correctly if an additional fresh module imports other
additional fresh module which imports a blocked module.
* Raises ImportError if the specified module cannot be imported
while all additional fresh modules are successfully imported.
* Support blocking packages.
* Always restore the import state of fresh and blocked modules
and their submodules.
* Fix test_decimal and test_xml_etree which depended on an undesired
side effect of import_fresh_module().
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r-- | Lib/test/test_xml_etree.py | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index c79b5462b93..5a8824a78ff 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -26,7 +26,7 @@ from itertools import product, islice from test import support from test.support import os_helper from test.support import warnings_helper -from test.support import findfile, gc_collect, swap_attr +from test.support import findfile, gc_collect, swap_attr, swap_item from test.support.import_helper import import_fresh_module from test.support.os_helper import TESTFN @@ -167,12 +167,11 @@ class ElementTestCase: cls.modules = {pyET, ET} def pickleRoundTrip(self, obj, name, dumper, loader, proto): - save_m = sys.modules[name] try: - sys.modules[name] = dumper - temp = pickle.dumps(obj, proto) - sys.modules[name] = loader - result = pickle.loads(temp) + with swap_item(sys.modules, name, dumper): + temp = pickle.dumps(obj, proto) + with swap_item(sys.modules, name, loader): + result = pickle.loads(temp) except pickle.PicklingError as pe: # pyET must be second, because pyET may be (equal to) ET. human = dict([(ET, "cET"), (pyET, "pyET")]) @@ -180,8 +179,6 @@ class ElementTestCase: % (obj, human.get(dumper, dumper), human.get(loader, loader))) from pe - finally: - sys.modules[name] = save_m return result def assertEqualElements(self, alice, bob): |