Factory function for unpickling pytz tzinfo instances. This is shared for both StaticTzInfo and DstTzInfo instances, because database changes could cause a zones implementation to switch between these two base classes and we can't break pickles on a pytz version upgrade.
(zone, utcoffset=None, dstoffset=None, tzname=None)
| 527 | |
| 528 | |
| 529 | def unpickler(zone, utcoffset=None, dstoffset=None, tzname=None): |
| 530 | """Factory function for unpickling pytz tzinfo instances. |
| 531 | |
| 532 | This is shared for both StaticTzInfo and DstTzInfo instances, because |
| 533 | database changes could cause a zones implementation to switch between |
| 534 | these two base classes and we can't break pickles on a pytz version |
| 535 | upgrade. |
| 536 | """ |
| 537 | # Raises a KeyError if zone no longer exists, which should never happen |
| 538 | # and would be a bug. |
| 539 | tz = pytz.timezone(zone) |
| 540 | |
| 541 | # A StaticTzInfo - just return it |
| 542 | if utcoffset is None: |
| 543 | return tz |
| 544 | |
| 545 | # This pickle was created from a DstTzInfo. We need to |
| 546 | # determine which of the list of tzinfo instances for this zone |
| 547 | # to use in order to restore the state of any datetime instances using |
| 548 | # it correctly. |
| 549 | utcoffset = memorized_timedelta(utcoffset) |
| 550 | dstoffset = memorized_timedelta(dstoffset) |
| 551 | try: |
| 552 | return tz._tzinfos[(utcoffset, dstoffset, tzname)] |
| 553 | except KeyError: |
| 554 | # The particular state requested in this timezone no longer exists. |
| 555 | # This indicates a corrupt pickle, or the timezone database has been |
| 556 | # corrected violently enough to make this particular |
| 557 | # (utcoffset,dstoffset) no longer exist in the zone, or the |
| 558 | # abbreviation has been changed. |
| 559 | pass |
| 560 | |
| 561 | # See if we can find an entry differing only by tzname. Abbreviations |
| 562 | # get changed from the initial guess by the database maintainers to |
| 563 | # match reality when this information is discovered. |
| 564 | for localized_tz in tz._tzinfos.values(): |
| 565 | if (localized_tz._utcoffset == utcoffset and |
| 566 | localized_tz._dst == dstoffset): |
| 567 | return localized_tz |
| 568 | |
| 569 | # This (utcoffset, dstoffset) information has been removed from the |
| 570 | # zone. Add it back. This might occur when the database maintainers have |
| 571 | # corrected incorrect information. datetime instances using this |
| 572 | # incorrect information will continue to do so, exactly as they were |
| 573 | # before being pickled. This is purely an overly paranoid safety net - I |
| 574 | # doubt this will ever been needed in real life. |
| 575 | inf = (utcoffset, dstoffset, tzname) |
| 576 | tz._tzinfos[inf] = tz.__class__(inf, tz._tzinfos) |
| 577 | return tz._tzinfos[inf] |
no test coverage detected