pybind / pybind/pybind11

bug: pickle not working in 2.2.3

Open
#1,450 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I'm working with python 3.6.3 and pybind11 2.2.3, conda distribution, on ubuntu 17.10,

The following code produces an incorrect __setstate__ method. I've added printf in the __setstate__'s lambda or event directly into the pickle_factory's (line 327). It seems python calls never reach those.

Python code, second assert raising an error:

 a = C.toto()
 assert a.xx == 1
 a.__setstate__({'xx': 2, 'yy': 3})
 assert a.xx == 2

c++ code:

    struct Toto
    {

        int xx = 1;
        float yy = 1.0f;
    };

    template <typename T>
    inline T _get(char const * name, pybind11::dict & kwa, T deflt)
    { return  kwa.contains(name) ? kwa[name].cast<T>() : deflt; }

   PYBIND11_MODULE(example, mod) {
        using namespace pybind11::literals;
        pybind11::class_<Toto>(mod, "toto")
            .def(pybind11::init([](pybind11::kwargs d)-> Toto
                    { return { _get<int>("xx", d, 1), _get<float>("yy", d, 1.0f)};  }))
            .def_readwrite("xx", &Toto::xx)
            .def_readwrite("yy", &Toto::yy)
            .def(pybind11::pickle([](Toto const & self)
                    {
                        pybind11::dict x;
                        x["xx"] = self.xx;
                        x["yy"] = self.yy;
                        return x;
                    },
                    [](pybind11::dict d) -> Toto
                    { return { _get<int>("xx", d, 1), _get<float>("yy", d, 1.0f)};  }));
   }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Python assertions with the C++ binding on the reported pybind11 2.2.3 and Python 3.6.3 setup. Inspect pickle_factory around line 327, which the report identifies, and trace why the setstate callback is not reached. Done means the second assertion passes after calling setstate.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.