Search code examples
picklepython-multiprocessingplydillpython-re

TypeError("can't pickle re.Match objects") error when pickling using dill / pickle


I can't seem to figure out a way to pickle this, can anyone help? It's because of the way reduce function is written for re.match.

Code:

import re
x = re.match('abcd', 'abcd')
print(type(x))
print(x.__reduce_ex__(3))

Output:

<class 're.Match'>
Traceback (most recent call last):
  File "an.py", line 4, in <module>
    print(x.__reduce_ex__(3))
TypeError: can't pickle re.Match objects

My exact issue is that I am trying to pickle an object of a lex / yacc parser implementation class after submitting a string to it to parse.

If I try to pickle the class object without parsing any string via it, it is able to pickle. Problem arises only after I parse a string using it and then try to pickle the class object.


Solution

  • Match objects does not have a __getstate__ and __setstate__ thus cannot be pickled, the entire iterator could not be pickled.

    More about this subject can be found here: https://docs.python.org/3/library/pickle.html#pickle-picklable

    here is a further explanation on the desired objects: https://docs.python.org/3/library/re.html#match-objects

    An alternative solution is to implement __getstate__ and __setstate__ to help the pickling process, this will require you to create a custom class and implement this function, which seem to overcomplicated for this situation

    Hope that helped