diff options
author | Paul Ganssle <paul@ganssle.io> | 2020-05-17 21:55:11 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-17 21:55:11 -0400 |
commit | e527ec8abe0849e784ce100f53c2736986b670ae (patch) | |
tree | 1b638f564cbb69517ba7d9a5fe5d1cbd225bff40 /Lib/test/test_zoneinfo/_support.py | |
parent | 9681953c99b686cf23d1c476a2b26d2ddbec7694 (diff) | |
download | cpython-git-e527ec8abe0849e784ce100f53c2736986b670ae.tar.gz |
bpo-40536: Add zoneinfo.available_timezones (GH-20158)
This was not specified in the PEP, but it will likely be a frequently requested feature if it's not included.
This includes only the "canonical" zones, not a simple listing of every valid value of `key` that can be passed to `Zoneinfo`, because it seems likely that that's what people will want.
Diffstat (limited to 'Lib/test/test_zoneinfo/_support.py')
-rw-r--r-- | Lib/test/test_zoneinfo/_support.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Lib/test/test_zoneinfo/_support.py b/Lib/test/test_zoneinfo/_support.py index 6bd8d8dc0f..0fe162c258 100644 --- a/Lib/test/test_zoneinfo/_support.py +++ b/Lib/test/test_zoneinfo/_support.py @@ -66,11 +66,35 @@ class ZoneInfoTestBase(unittest.TestCase): super().setUpClass() @contextlib.contextmanager - def tzpath_context(self, tzpath, lock=TZPATH_LOCK): + def tzpath_context(self, tzpath, block_tzdata=True, lock=TZPATH_LOCK): + def pop_tzdata_modules(): + tzdata_modules = {} + for modname in list(sys.modules): + if modname.split(".", 1)[0] != "tzdata": # pragma: nocover + continue + + tzdata_modules[modname] = sys.modules.pop(modname) + + return tzdata_modules + with lock: + if block_tzdata: + # In order to fully exclude tzdata from the path, we need to + # clear the sys.modules cache of all its contents — setting the + # root package to None is not enough to block direct access of + # already-imported submodules (though it will prevent new + # imports of submodules). + tzdata_modules = pop_tzdata_modules() + sys.modules["tzdata"] = None + old_path = self.module.TZPATH try: self.module.reset_tzpath(tzpath) yield finally: + if block_tzdata: + sys.modules.pop("tzdata") + for modname, module in tzdata_modules.items(): + sys.modules[modname] = module + self.module.reset_tzpath(old_path) |