stub42/pytz

unpickling datetime with tzinfo built from file throws `UnknownTimeZoneError`

ds-cbo opened this issue · 0 comments

The following code:

import pytz
import datetime
import pickle
tzinfo = pytz.tzfile.build_tzinfo('local', open('/etc/localtime' ,'rb'))
dt = datetime.datetime(2023, 1, 1, tzinfo=tzinfo)
data = pickle.dumps(dt)
# b'\x80\x04\x95O\x00\x00\x00\x00\x00\x00\x00\x8c\x08datetime\x94\x8c\x08datetime\x94\x93\x94C\n\x07\xe7\x01\x01\x00\x00\x00\x00\x00\x00\x94\x8c\x04pytz\x94\x8c\x02_p\x94\x93\x94(\x8c\x05local\x94K\x00K\x00\x8c\x03WET\x94t\x94R\x94\x86\x94R\x94.'
pickle.loads(data)

Raises the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/pytz/__init__.py", line 309, in _p
    return unpickler(*args)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pytz/tzinfo.py", line 539, in unpickler
    tz = pytz.timezone(zone)
         ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pytz/__init__.py", line 188, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'local'

On Python 3.11 with pytz 2022.7

Is there a way to have pytz register where the local zone can be found during unpickling?