pybind / pybind/pybind11

[BUG]: throw is not propagate from python in some circumstances

Open
#4,023 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Required prerequisites
Problem description

Here is a code that throw sometimes but not other time.
c.go() throw, call_go(c) that call c.go() does not throw.
The call_go(c) go through https://github.com/pybind/pybind11/blob/master/include/pybind11/pytypes.h#L753-L759 because of https://github.com/pybind/pybind11/blob/master/include/pybind11/pybind11.h#L2659

Do you know if it is a "bug" or a normal behavior that I don't understand.

Regards

Reproducible example code
// C++ file
#include <string>
#include <pybind11/pybind11.h>
 
namespace py = pybind11;
class Animal {
public:
    virtual ~Animal() { }
    virtual std::string go() { return "aaa"; }
};
 
class PyAnimal : public Animal {
public:
    using Animal::Animal;
 
    std::string go() override {
        PYBIND11_OVERRIDE(std::string, Animal, go, );
    }
};
 
std::string call_go(Animal* animal) {
    return animal->go();
}
 
PYBIND11_MODULE(foo, m) {
    py::class_<Animal, PyAnimal>(m, "Animal")
        .def(py::init<>())
        .def("go", &Animal::go);
    m.def("call_go", &call_go);
}
 
// Python file
from foo import *
 
class Cat(Animal):
    def __getattribute__(self, _):
        print("*** raising an exception now ***")
        raise Exception("uh oh")
 
c = Cat()
# c.go() // throw
call_go(c) // no throw

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

Reproduce the example with the C++ binding and Python Cat class, then inspect include/pybind11/pytypes.h lines 753-759 and include/pybind11/pybind11.h around line 2659. Compare direct c.go() with call_go(c) and determine the expected exception-propagation behavior; done should establish whether this is intended or identify a reproducible fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.