[BUG]: throw is not propagate from python in some circumstances
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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