Skip to content

Compile issue when using nullptr for def_property #1856

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

Closed
romiphadte opened this issue Jul 22, 2019 · 4 comments
Closed

Compile issue when using nullptr for def_property #1856

romiphadte opened this issue Jul 22, 2019 · 4 comments

Comments

@romiphadte
Copy link

Make sure you've completed the following steps before submitting your issue -- thank you!

Issue description

According to the documentation I should be able to define properties and use nullptr to define write only properties. (Referenced from here https://pybind11.readthedocs.io/en/master/classes.html)

py::class_<Pet>(m, "Pet")
    .def(py::init<const std::string &>())
    .def_property("name", &Pet::getName, &Pet::setName)
    // ... remainder ...
Write only properties can be defined by passing nullptr as the input for the read function.

Unfortunately when I try to do this, I get a compiler error.

Here is the code to reproduce this error.

#include <pybind11/pybind11.h>

class Person {
  private:
    int age_;
  public:
    void setAge(int age) {
      age_ = age;
    }

};

namespace py = pybind11;

PYBIND11_MODULE(python_example, m) {
  py::class_<Person>(m,"Person")
  .def_property("age", nullptr, &Person::setAge);

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

Here is the stack trace:

    Complete output from command /mnt/nfs/opt/anaconda3/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-req-build-p6tq9ke4/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n'
);f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-o8t67x64/install-record.txt --single-version-externally-managed --compile --user --prefix=:
    running install
    running build
    running build_ext
    gcc -pthread -B /mnt/nfs/opt/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/mnt/nfs/opt/anaconda3/include/python3.6m -c /tmp/tmp1h1bqk39.cpp -o tmp/tmp1h1b
qk39.o -std=c++17
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    gcc -pthread -B /mnt/nfs/opt/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/mnt/nfs/opt/anaconda3/include/python3.6m -c /tmp/tmp3rvcnulo.cpp -o tmp/tmp3rvc
nulo.o -fvisibility=hidden
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    building 'python_example' extension
    creating build
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/src
    gcc -pthread -B /mnt/nfs/opt/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/mnt/nfs/opt/anaconda3/include/python3.6m -I/mnt/nfs/home/romi/.local/include/py
thon3.6m -I/mnt/nfs/opt/anaconda3/include/python3.6m -c src/main.cpp -o build/temp.linux-x86_64-3.6/src/main.o -DVERSION_INFO="0.0.1" -std=c++17 -fvisibility=hidden
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    In file included from /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/pytypes.h:12:0,
                     from /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/cast.h:13,
                     from /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/attr.h:13,
                     from /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/pybind11.h:43,
                     from src/main.cpp:1:
    /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/detail/common.h: In instantiation of ‘struct pybind11::detail::strip_function_object<std::nullptr_t>’:
    /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/detail/common.h:635:2:   required by substitution of ‘template<class Function, class F> using function_signature_t = std::conditional_t<std::is_function<_Arg>::value, F, typ
ename std::conditional<(std::is_pointer<_Dp>::value || std::is_member_pointer<F>::value), std::remove_pointer<F>, pybind11::detail::strip_function_object<F> >::type::type> [with Function = std::nullptr_t&; F = std::remove_refere
nce<std::nullptr_t&>::type]’
    /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/pybind11.h:65:9:   required from ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = std::nullptr_t&; Extra = {}; <template-parameter-1-3> = void]’
    /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/pybind11.h:1223:28:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_property(const char*, const Getter&, const pybind11::cpp_functio
n&, const Extra& ...) [with Getter = std::nullptr_t; Extra = {}; type_ = Person; options = {}]’
    /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/pybind11.h:1219:28:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_property(const char*, const Getter&, const Setter&, const Extra&
 ...) [with Getter = std::nullptr_t; Setter = void (Person::*)(int); Extra = {}; type_ = Person; options = {}]’
    src/main.cpp:17:48:   required from here
    /mnt/nfs/opt/anaconda3/include/python3.6m/pybind11/detail/common.h:622:49: error: ‘operator()’ is not a member of ‘std::nullptr_t’
         using type = typename remove_class<decltype(&F::operator())>::type;
                                                     ^
    error: command 'gcc' failed with exit status 1

This code is based off of https://github.com/pybind/python_example

@YannickJadoul
Copy link
Collaborator

Are you using the latest version of pybind11? I've just tested this on pybind11 v2.3.0, and your code seems to work, but indeed, on v2.2.4 it doesn't.

@YannickJadoul
Copy link
Collaborator

(Something's really weird in how #1144 was merged in 2017, as also show in the git blame, as well as listed in the changelog, but that one line of that commit is not present before v2.3.0.)

@wjakob
Copy link
Member

wjakob commented Aug 13, 2019

I don't recall the context, but probably something here seemed too major to include it in a patch release. commits on 'master' are simultaneously targeting the next x.0.0 or x.y.0 or x.y.z release, and they are then cherry-picked into specific branches.

@bstaletic
Copy link
Collaborator

I guess we don't need to keep issues around that are affecting pybind11 2.2.z.

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

No branches or pull requests

4 participants