Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Remove the use of macros since they are used only in one place, and d…
Browse files Browse the repository at this point in the history
…o not improve readability.

PiperOrigin-RevId: 425365274
  • Loading branch information
LizaTretyakova authored and copybara-github committed Jan 31, 2022
1 parent 0d59673 commit 2579548
Showing 1 changed file with 60 additions and 59 deletions.
119 changes: 60 additions & 59 deletions python/tink/cc/pybind/python_file_object_adapter.cc
Original file line number Diff line number Diff line change
@@ -16,85 +16,86 @@

#include "tink/cc/python_file_object_adapter.h"

#include <string>

#include "absl/status/status.h"
#include "pybind11/pybind11.h"
#include "tink/cc/pybind/status_casters.h"

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<const cname *>(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<const cname *>(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<statusor_payload_type>(); \
} 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<int> Write(absl::string_view data) override{
PYBIND11_OVERLOAD_PURE_STATUSOR_RETURN(
int, PythonFileObjectAdapter, "write",
pybind11::bytes(std::string(data)))}
util::StatusOr<int> Write(absl::string_view data) override {
try {
pybind11::gil_scoped_acquire gil;
pybind11::function overload = pybind11::get_overload(
static_cast<const PythonFileObjectAdapter *>(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<int>();
} 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<const PythonFileObjectAdapter *>(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<std::string> 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<const PythonFileObjectAdapter *>(this), "read");
if (!overload) {
return util::Status(absl::StatusCode::kUnimplemented,
"No Python overload is defined for read.");
}
auto o = overload(size);
return o.cast<std::string>();
} 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_<PythonFileObjectAdapter, Pybind11PythonFileObjectAdapter,

0 comments on commit 2579548

Please sign in to comment.