Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cython-Python pickling #171

Open
asvetlov opened this issue Oct 20, 2017 · 4 comments
Open

Cython-Python pickling #171

asvetlov opened this issue Oct 20, 2017 · 4 comments

Comments

@asvetlov
Copy link
Member

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

@gyermolenko
Copy link
Contributor

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
Copy link
Contributor

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?

@asvetlov
Copy link
Member Author

asvetlov commented Oct 6, 2018

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

@gyermolenko
Copy link
Contributor

gyermolenko commented Oct 17, 2018

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'>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants