From cce0ae24ffe8a7c366c6908a8ab8b5d703d20e29 Mon Sep 17 00:00:00 2001 From: Jason Matthew Suhari Date: Fri, 20 Mar 2026 12:18:57 +0800 Subject: [PATCH] fix: capture stack checkpoint before pushing args in operator() (#469) --- include/pybind11/internal/function.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/pybind11/internal/function.h b/include/pybind11/internal/function.h index 2c84e756..c1c69dff 100644 --- a/include/pybind11/internal/function.h +++ b/include/pybind11/internal/function.h @@ -70,6 +70,7 @@ args_proxy interface::operator* () const { template template object interface::operator() (Args&&... args) const { + py_StackRef p0 = py_peek(0); // checkpoint before pushing so py_clearexc can safely rewind py_push(ptr()); py_pushnil(); @@ -108,7 +109,13 @@ object interface::operator() (Args&&... args) const { (foreach(std::forward(args)), ...); - raise_call(argc, kwargsc); + if(!py_vectorcall(argc, kwargsc)) { + py_matchexc(tp_Exception); + object e = object::from_ret(); + auto what = py_formatexc(); + py_clearexc(p0); + throw python_error(what, std::move(e)); + } return object::from_ret(); }