2021-07-28 07:10:10 +08:00
|
|
|
//===- NpcompPybindUtils.h - Utilities for interop with python ------------===//
|
2020-05-01 07:00:00 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-07-28 07:10:10 +08:00
|
|
|
// TODO: Most of this lives upstream now and should be taken from there.
|
|
|
|
|
|
|
|
#ifndef NPCOMP_PYTHON_NPCOMP_PYBIND_UTILS_H
|
|
|
|
#define NPCOMP_PYTHON_NPCOMP_PYBIND_UTILS_H
|
2020-05-01 10:23:18 +08:00
|
|
|
|
2020-05-01 07:00:00 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <pybind11/pybind11.h>
|
|
|
|
#include <pybind11/pytypes.h>
|
|
|
|
#include <pybind11/stl.h>
|
|
|
|
|
2020-11-11 13:38:13 +08:00
|
|
|
#include "mlir-c/Bindings/Python/Interop.h"
|
|
|
|
#include "mlir-c/IR.h"
|
|
|
|
#include "mlir-c/Pass.h"
|
2021-08-23 03:17:12 +08:00
|
|
|
#include "mlir/Bindings/Python/PybindAdaptors.h"
|
2020-05-01 07:00:00 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
|
|
|
|
namespace py = pybind11;
|
|
|
|
|
2020-05-01 08:14:03 +08:00
|
|
|
namespace pybind11 {
|
2020-05-01 07:00:00 +08:00
|
|
|
|
|
|
|
/// Raises a python exception with the given message.
|
|
|
|
/// Correct usage:
|
|
|
|
// throw RaiseValueError(PyExc_ValueError, "Foobar'd");
|
2020-09-26 09:13:16 +08:00
|
|
|
inline pybind11::error_already_set raisePyError(PyObject *exc_class,
|
|
|
|
const char *message) {
|
|
|
|
PyErr_SetString(exc_class, message);
|
|
|
|
return pybind11::error_already_set();
|
|
|
|
}
|
2020-05-01 07:00:00 +08:00
|
|
|
|
|
|
|
/// 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());
|
|
|
|
}
|
|
|
|
|
2020-05-01 08:14:03 +08:00
|
|
|
} // namespace pybind11
|
2020-05-01 10:23:18 +08:00
|
|
|
|
2021-07-28 07:10:10 +08:00
|
|
|
#endif // NPCOMP_PYTHON_NPCOMP_PYBIND_UTILS_H
|