Maxim Koro
Maxim Koro

Reputation: 63

Python: pickle.loads failed for class instance

I need tzinfo class for datetime object in my class. I need to pickle my class. But pickle.loads(obj) failed. What's wrong with mytz class? If I do not use mytz class everything works. Why?

from datetime import datetime, tzinfo, timedelta

# Timezone class
class mytz (tzinfo):
    def __init__(self, offset, dst):
        self._tzname = offset + ' ' + dst
        self._offset = timedelta(hours=int(offset[0:3]), minutes=int(offset[0:1]+offset[3:]))
        self._dst = timedelta(hours=int(dst[0:3]), minutes=int(dst[0:1]+dst[3:]))
    def utcoffset(self, dt):
        return self._offset + self.dst(dt)
    def tzname(self, dt):
        return self._tzname
    def dst(self, dt):
        return self._dst

# Root class
class A:
    def __init__(self, val):
        self.val = val
        self.val2 = mytz(val, val)
    def __str__(self):
        return 'response'+str(self.val)
# Sample code
a = A('+0100')

import pickle
apickled = pickle.dumps(a)
print (a)
print (a.val2)
b = pickle.loads(apickled)
print (b)
print(b.val2)

here is output:

response+0100
<__main__.mytz object at 0x7fe2873b5748>
Traceback (most recent call last):
  File "/home/maksim/Projects/amodule/ttt.py", line 32, in <module>
    b = pickle.loads(apickled)
TypeError: __init__() missing 2 required positional arguments: 'offset' and 'dst'

Upvotes: 4

Views: 1441

Answers (1)

xi&#186;
xi&#186;

Reputation: 4687

We should add __getinitargs__() method to our custom class. And dump our arguments somewhere:

self.args = offset, dst

tzinfo is abstract class with __reduce__ method. __reduce__ trying to invoke __getinitars__() method. But always stuck with None value.

class mytz (tzinfo):
    def __init__(self, offset, dst):
        self.args = offset, dst
        self._tzname = offset + ' ' + dst
        self._offset = timedelta(
            hours=int(offset[0:3]), minutes=int(offset[0:1] + offset[3:]))
        self._dst = timedelta(
            hours=int(dst[0:3]), minutes=int(dst[0:1] + dst[3:]))

    def utcoffset(self, dt):
        return self._offset + self.dst(dt)

    def tzname(self, dt):
        return self._tzname

    def dst(self, dt):
        return self._dst

    def __getinitargs__(self):
        return self.args

Upvotes: 4

Related Questions