H.Rappeport
H.Rappeport

Reputation: 607

How to locate problematic (un-picklable) object?

Trying to pickle an object, I get the following error message:

TypeError: cannot serialize '_io.TextIOWrapper' object

The object class in question inherits another class (pytorch_lightning.LightningModule), so I assume the problematic object belongs to the super class. How can I locate it? The error trace only leads up to my pickling command, which is

with open(save_path, "wb") as p:
    pickle.dump(self, p)

Upvotes: 3

Views: 964

Answers (1)

Amiram
Amiram

Reputation: 1305

According to the pickle documentation you can influence on how an object is pickled by defining __getstate__ function.

Classes can further influence how their instances are pickled; if the class defines the method __getstate__(), it is called and the returned object is pickled as the contents for the instance, instead of the contents of the instance’s dictionary. If the __getstate__() method is absent, the instance’s __dict__ is pickled as usual.

Try to do the following:

def __getstate__(self):
    for variable_name, value in vars(self).items():
        try:
            pickle.dumps(value)
        except pickle.PicklingError:
            print(f'{variable_name} with value {value} is not pickable')

When you will try to pickle the object it should print out the un-picklable objects of your object.

Alternative way:

If the __getstate__() method is absent, the instance’s __dict__ is pickled as usual.

Go over the object __dict__ in the same way, try to pickle each one of the variables to find out the problematic ones.

Upvotes: 2

Related Questions