-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change pybind11 handling of callbacks with
Status
, StatusOr
retur…
…ns to capture C++ exceptions. This change builds on google/pybind11clif#30022. PiperOrigin-RevId: 597047935
- Loading branch information
1 parent
5199278
commit 970ac19
Showing
9 changed files
with
371 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// This code is meant to be built with C++ exception handling disabled: | ||
// the whole point of absl::Status, absl::StatusOr is to provide an alternative | ||
// to C++ exception handling. | ||
|
||
#ifndef PYBIND11_ABSEIL_TESTS_STATUS_TESTING_NO_CPP_EH_LIB_H_ | ||
#define PYBIND11_ABSEIL_TESTS_STATUS_TESTING_NO_CPP_EH_LIB_H_ | ||
|
||
#include <Python.h> | ||
|
||
#include <functional> | ||
#include <string> | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
|
||
namespace pybind11_abseil_tests { | ||
namespace status_testing_no_cpp_eh { | ||
|
||
inline std::string CallCallbackWithStatusReturn( | ||
const std::function<absl::Status()> &cb) { | ||
absl::Status cb_return_value = cb(); | ||
return cb_return_value.ToString(); | ||
} | ||
|
||
inline std::string CallCallbackWithStatusOrIntReturn( | ||
const std::function<absl::StatusOr<int>()> &cb) { | ||
absl::StatusOr<int> cb_return_value = cb(); | ||
if (cb_return_value.ok()) { | ||
return std::to_string(cb_return_value.value()); | ||
} | ||
return cb_return_value.status().ToString(); | ||
} | ||
|
||
inline PyObject *CallCallbackWithStatusOrObjectReturn( | ||
const std::function<absl::StatusOr<PyObject *>()> &cb) { | ||
absl::StatusOr<PyObject*> cb_return_value = cb(); | ||
if (cb_return_value.ok()) { | ||
return cb_return_value.value(); | ||
} | ||
return PyUnicode_FromString(cb_return_value.status().ToString().c_str()); | ||
} | ||
|
||
inline absl::Status GenerateErrorStatusNotOk() { | ||
return absl::AlreadyExistsError("Something went wrong, again."); | ||
} | ||
|
||
} // namespace status_testing_no_cpp_eh | ||
} // namespace pybind11_abseil_tests | ||
|
||
#endif // PYBIND11_ABSEIL_TESTS_STATUS_TESTING_NO_CPP_EH_LIB_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#if true // go/pybind11_include_order | ||
#include <pybind11/pybind11.h> | ||
#endif | ||
|
||
#include <pybind11/functional.h> | ||
#include <pybind11/type_caster_pyobject_ptr.h> | ||
|
||
#include "pybind11_abseil/import_status_module.h" | ||
#include "pybind11_abseil/status_caster.h" | ||
#include "pybind11_abseil/statusor_caster.h" | ||
#include "pybind11_abseil/tests/status_testing_no_cpp_eh_lib.h" | ||
|
||
namespace pybind11_abseil_tests { | ||
namespace status_testing_no_cpp_eh { | ||
|
||
PYBIND11_MODULE(status_testing_no_cpp_eh_pybind, m) { | ||
pybind11::google::ImportStatusModule(); | ||
|
||
m.def("CallCallbackWithStatusReturn", &CallCallbackWithStatusReturn); | ||
m.def("CallCallbackWithStatusOrIntReturn", | ||
&CallCallbackWithStatusOrIntReturn); | ||
m.def("CallCallbackWithStatusOrObjectReturn", | ||
&CallCallbackWithStatusOrObjectReturn, | ||
pybind11::return_value_policy::take_ownership); | ||
m.def("GenerateErrorStatusNotOk", &GenerateErrorStatusNotOk); | ||
} | ||
|
||
} // namespace status_testing_no_cpp_eh | ||
} // namespace pybind11_abseil_tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from absl.testing import absltest | ||
|
||
from pybind11_abseil.tests import status_testing_no_cpp_eh_pybind | ||
from pybind11_abseil.tests import status_testing_no_cpp_eh_test_lib as test_lib | ||
|
||
|
||
class _TestModuleMixin: | ||
|
||
def getTestModule(self): # pylint: disable=invalid-name | ||
return status_testing_no_cpp_eh_pybind | ||
|
||
|
||
class StatusReturnTest(test_lib.StatusReturnTest, _TestModuleMixin): | ||
pass | ||
|
||
|
||
class StatusOrReturnTest(test_lib.StatusOrReturnTest, _TestModuleMixin): | ||
pass | ||
|
||
|
||
class StatusOrPyObjectPtrTest( | ||
test_lib.StatusOrPyObjectPtrTest, _TestModuleMixin | ||
): | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
absltest.main() |
Oops, something went wrong.