torch-mlir/python/npcomp/pybind_utils.h

55 lines
1.5 KiB
C++

//===- pybind_utils.h - Utilities for interop with python -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <string>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <pybind11/stl.h>
#include "llvm/ADT/Optional.h"
namespace py = pybind11;
namespace pybind11 {
namespace detail {
template <typename T>
struct type_caster<llvm::Optional<T>> : optional_caster<llvm::Optional<T>> {};
} // namespace detail
} // namespace pybind11
namespace mlir {
namespace npcomp {
namespace python {
/// Raises a python exception with the given message.
/// Correct usage:
// throw RaiseValueError(PyExc_ValueError, "Foobar'd");
pybind11::error_already_set raisePyError(PyObject *exc_class,
const char *message);
/// Raises a value error with the given message.
/// Correct usage:
/// throw RaiseValueError("Foobar'd");
inline pybind11::error_already_set raiseValueError(const char *message) {
return raisePyError(PyExc_ValueError, message);
}
/// Raises a value error with the given message.
/// Correct usage:
/// throw RaiseValueError(message);
inline pybind11::error_already_set raiseValueError(const std::string &message) {
return raisePyError(PyExc_ValueError, message.c_str());
}
} // namespace python
} // namespace npcomp
} // namespace mlir