multidict icon indicating copy to clipboard operation
multidict copied to clipboard

Cython-Python pickling

Open asvetlov opened this issue 7 years ago • 4 comments

Pickled pure Python MultiDicts should be unpickled as Cythonized if unpicker is executed on machine with Cython support. And vise versa.

asvetlov avatar Oct 20 '17 19:10 asvetlov

There are protocols 0..4 (inclusive). Maybe I could bump upper range in these places? gen_pickles.py conftest.py

ps after mentioned +1 number of tests turn from 36 to 44. And they pass

gyermolenko avatar Oct 06 '18 13:10 gyermolenko

Regarding the issue itself.. Could you share some thoughts about desirable implementation? Does it involve custom logic in __getnewargs__, __reduce__ and a bunch of other magic methods depending on USE_CYTHON variable?

gyermolenko avatar Oct 06 '18 14:10 gyermolenko

My idea is to look on CPython sources. Unfortunately, that's it for now

asvetlov avatar Oct 06 '18 17:10 asvetlov

Here is what I got playing around with special methods related to pickle protocol:

import pickle

USE_CYTHON = True
# USE_CYTHON = False

class A:
    def __getnewargs_ex__(self):
        # print('in __getnewargs_ex__ of A')
        if USE_CYTHON:
            print('selecting path B')
            return ((B,), {})
        else:
            print('selecting path A')
            return ((A,), {})

    def __new__(cls, *args, **kwargs):
        # print('In A __new__, *args, **kwargs: ', *args, **kwargs)
        if args:
            return args[0]()
        else:
            return super().__new__(cls)

class B: pass

if __name__ == '__main__':
    obj = A()
    pd = pickle.dumps(obj)
    pl = pickle.loads(pd)
    #                           USE_CYTHON = True          USE_CYTHON = False
    print(pl.__class__)       # <class '__main__.B'>  or   <class '__main__.A'>

gyermolenko avatar Oct 17 '18 14:10 gyermolenko