| 37 | |
| 38 | @contextdecorator |
| 39 | def set_tzpath(tzpath, block_tzdata=False, clear_cache=True): |
| 40 | tzdata_modules = {} |
| 41 | with TZPATH_LOCK: |
| 42 | if block_tzdata: |
| 43 | tzdata_modules = pop_tzdata_modules() |
| 44 | sys.modules["tzdata"] = None |
| 45 | |
| 46 | old_tzpath = tuple(tz.TZPATH) |
| 47 | try: |
| 48 | tz._tzpath.reset_tzpath(to=tzpath) |
| 49 | # Zones already in the cache may have come from somewhere else |
| 50 | # (another TZPATH, or tzdata when it is now blocked), and zones |
| 51 | # cached in here must not leak out, so clear it on both ends. |
| 52 | if clear_cache: |
| 53 | tz.gettz.cache_clear() |
| 54 | yield |
| 55 | finally: |
| 56 | if block_tzdata: |
| 57 | sys.modules.pop("tzdata", None) |
| 58 | sys.modules.update(tzdata_modules) |
| 59 | |
| 60 | tz._tzpath.reset_tzpath(to=old_tzpath) |
| 61 | if clear_cache: |
| 62 | tz.gettz.cache_clear() |
| 63 | |
| 64 | |
| 65 | TZ_SOURCES = ("tzpath", "tzdata") |