From 2579548822f0ef5a861963b120e9406035d6a5c7 Mon Sep 17 00:00:00 2001 From: lizatretyakova Date: Mon, 31 Jan 2022 07:51:33 -0800 Subject: [PATCH] Remove the use of macros since they are used only in one place, and do not improve readability. PiperOrigin-RevId: 425365274 --- .../cc/pybind/python_file_object_adapter.cc | 119 +++++++++--------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/python/tink/cc/pybind/python_file_object_adapter.cc b/python/tink/cc/pybind/python_file_object_adapter.cc index 2886c60b8d..79ea9df397 100644 --- a/python/tink/cc/pybind/python_file_object_adapter.cc +++ b/python/tink/cc/pybind/python_file_object_adapter.cc @@ -16,6 +16,8 @@ #include "tink/cc/python_file_object_adapter.h" +#include + #include "absl/status/status.h" #include "pybind11/pybind11.h" #include "tink/cc/pybind/status_casters.h" @@ -23,78 +25,77 @@ namespace crypto { namespace tink { -// Defining PYBIND11_OVERLOAD macros here for now. -// TODO(b/146426040): Use future macros for OSS absl::Status, StatusOr -// when they become available. - -// Macro to populate pybind11 trampoline class virtual methods with a -// util::Status return type. Converts all exceptions derived from -// std::exception to a StatusNotOk return. std::abort is called for -// all other exceptions, to ensure exceptions don't unwind through code -// compiled with -fno-exceptions (or similar, depending on the platform). -// Modeled after PYBIND11_OVERLOAD_INT defined in pybind11/pybind11.h. -#define PYBIND11_OVERLOAD_PURE_STATUS_RETURN(cname, name, ...) \ - try { \ - pybind11::gil_scoped_acquire gil; \ - pybind11::function overload = \ - pybind11::get_overload(static_cast(this), name); \ - if (!overload) { \ - return util::Status(absl::StatusCode::kUnimplemented, \ - "No Python overload is defined for " name "."); \ - } \ - overload(__VA_ARGS__); /* Ignoring return value. */ \ - return util::Status(); \ - } catch (const std::exception &e) { \ - return util::Status(absl::StatusCode::kUnknown, e.what()); \ - } catch (...) { \ - std::abort(); \ - } - -// Macro to populate pybind11 trampoline class virtual methods with a -// util::StatusOr return type. Converts all exceptions as described for -// PYBIND11_OVERLOAD_PURE_STATUS_RETURN. -#define PYBIND11_OVERLOAD_PURE_STATUSOR_RETURN(statusor_payload_type, cname, \ - name, ...) \ - try { \ - pybind11::gil_scoped_acquire gil; \ - pybind11::function overload = \ - pybind11::get_overload(static_cast(this), name); \ - if (!overload) { \ - return util::Status(absl::StatusCode::kUnimplemented, \ - "No Python overload is defined for " name "."); \ - } \ - auto o = overload(__VA_ARGS__); \ - return o.cast(); \ - } catch (const std::exception &e) { \ - return util::Status(absl::StatusCode::kUnknown, e.what()); \ - } catch (...) { \ - std::abort(); \ - } - class Pybind11PythonFileObjectAdapter : public PythonFileObjectAdapter { public: // Inherit the constructors. using PythonFileObjectAdapter::PythonFileObjectAdapter; - // Trampoline for each virtual member function: + // Trampoline for each virtual member function. + + // The implementations are modeled after PYBIND11_OVERLOAD_INT + // defined in pybind11/pybind11.h, and convert all exceptions derived from + // std::exception to a StatusNotOk return. std::abort is called for all other + // exceptions, to ensure exceptions don't unwind through code compiled with + // -fno-exceptions (or similar, depending on the platform). - util::StatusOr Write(absl::string_view data) override{ - PYBIND11_OVERLOAD_PURE_STATUSOR_RETURN( - int, PythonFileObjectAdapter, "write", - pybind11::bytes(std::string(data)))} + util::StatusOr Write(absl::string_view data) override { + try { + pybind11::gil_scoped_acquire gil; + pybind11::function overload = pybind11::get_overload( + static_cast(this), "write"); + if (!overload) { + return util::Status(absl::StatusCode::kUnimplemented, + "No Python overload is defined for write."); + } + auto o = overload(pybind11::bytes(std::string(data))); + return o.cast(); + } catch (const std::exception &e) { + return util::Status(absl::StatusCode::kUnknown, e.what()); + } catch (...) { + std::abort(); + } + } - util::Status Close() override{ - PYBIND11_OVERLOAD_PURE_STATUS_RETURN(PythonFileObjectAdapter, "close")} + util::Status Close() override { + try { + pybind11::gil_scoped_acquire gil; + pybind11::function overload = pybind11::get_overload( + static_cast(this), "close"); + if (!overload) { + return util::Status(absl::StatusCode::kUnimplemented, + "No Python overload is defined for close."); + } + overload(); /* Ignoring return value. */ + return util::Status(); + } catch (const std::exception &e) { + return util::Status(absl::StatusCode::kUnknown, e.what()); + } catch (...) { + std::abort(); + } + } util::StatusOr Read(int size) override { - PYBIND11_OVERLOAD_PURE_STATUSOR_RETURN(std::string, PythonFileObjectAdapter, - "read", size) + try { + pybind11::gil_scoped_acquire gil; + pybind11::function overload = pybind11::get_overload( + static_cast(this), "read"); + if (!overload) { + return util::Status(absl::StatusCode::kUnimplemented, + "No Python overload is defined for read."); + } + auto o = overload(size); + return o.cast(); + } catch (const std::exception &e) { + return util::Status(absl::StatusCode::kUnknown, e.what()); + } catch (...) { + std::abort(); + } } }; -void PybindRegisterPythonFileObjectAdapter(pybind11::module* module) { +void PybindRegisterPythonFileObjectAdapter(pybind11::module *module) { namespace py = pybind11; - py::module& m = *module; + py::module &m = *module; // TODO(b/146492561): Reduce the number of complicated lambdas. py::class_