I'm subclassing logging.Logger
and although I'm implementing __getstate__
very safely, I get an error by the superclass saying you cannot pickle this. How to get around this?
Minimal reporducible code:
import logging
class MyLogger(logging.Logger):
def __getstate__(self): return {}
import pickle
pickle.dumps(MyLogger('name'))
Result:
PicklingError Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 pickle.dumps(MyLogger('name'))
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\logging\__init__.py:1739, in Logger.__reduce__(self)
1737 if getLogger(self.name) is not self:
1738 import pickle
-> 1739 raise pickle.PicklingError('logger cannot be pickled')
1740 return getLogger, (self.name,)
PicklingError: logger cannot be pickled
I solved it by overriding
__reduce__
from superclass. I'm not sure though why__getstate__
&__setstate__
where not sufficient in this instance. They usually are.