CIRCT  19.0.0git
PybindUtils.h
Go to the documentation of this file.
1 //===- PybindUtils.h - Utilities for interop with python ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file copied from NPCOMP project. Omissions will be added.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef CIRCT_BINDINGS_PYTHON_PYBINDUTILS_H
14 #define CIRCT_BINDINGS_PYTHON_PYBINDUTILS_H
15 
16 #include <string>
17 
18 #include <pybind11/pybind11.h>
19 #include <pybind11/pytypes.h>
20 #include <pybind11/stl.h>
21 
22 #include "mlir-c/Bindings/Python/Interop.h"
23 #include "mlir-c/IR.h"
24 #include "mlir-c/Pass.h"
25 
26 #include <optional>
27 
28 namespace py = pybind11;
29 
30 namespace circt {
31 namespace python {
32 
33 /// Taken from PybindUtils.h in MLIR.
34 /// Accumulates into a python file-like object, either writing text (default)
35 /// or binary.
37 public:
38  PyFileAccumulator(pybind11::object fileObject, bool binary)
39  : pyWriteFunction(fileObject.attr("write")), binary(binary) {}
40 
41  void *getUserData() { return this; }
42 
43  MlirStringCallback getCallback() {
44  return [](MlirStringRef part, void *userData) {
45  pybind11::gil_scoped_acquire();
46  PyFileAccumulator *accum = static_cast<PyFileAccumulator *>(userData);
47  if (accum->binary) {
48  // Note: Still has to copy and not avoidable with this API.
49  pybind11::bytes pyBytes(part.data, part.length);
50  accum->pyWriteFunction(pyBytes);
51  } else {
52  pybind11::str pyStr(part.data,
53  part.length); // Decodes as UTF-8 by default.
54  accum->pyWriteFunction(pyStr);
55  }
56  };
57  }
58 
59 private:
60  pybind11::object pyWriteFunction;
61  bool binary;
62 };
63 } // namespace python
64 } // namespace circt
65 
66 namespace pybind11 {
67 
68 /// Raises a python exception with the given message.
69 /// Correct usage:
70 // throw RaiseValueError(PyExc_ValueError, "Foobar'd");
71 inline pybind11::error_already_set raisePyError(PyObject *exc_class,
72  const char *message) {
73  PyErr_SetString(exc_class, message);
74  return pybind11::error_already_set();
75 }
76 
77 /// Raises a value error with the given message.
78 /// Correct usage:
79 /// throw RaiseValueError("Foobar'd");
80 inline pybind11::error_already_set raiseValueError(const char *message) {
81  return raisePyError(PyExc_ValueError, message);
82 }
83 
84 /// Raises a value error with the given message.
85 /// Correct usage:
86 /// throw RaiseValueError(message);
87 inline pybind11::error_already_set raiseValueError(const std::string &message) {
88  return raisePyError(PyExc_ValueError, message.c_str());
89 }
90 
91 } // namespace pybind11
92 
93 #endif // CIRCT_BINDINGS_PYTHON_PYBINDUTILS_H
Taken from PybindUtils.h in MLIR.
Definition: PybindUtils.h:36
pybind11::object pyWriteFunction
Definition: PybindUtils.h:60
MlirStringCallback getCallback()
Definition: PybindUtils.h:43
PyFileAccumulator(pybind11::object fileObject, bool binary)
Definition: PybindUtils.h:38
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
pybind11::error_already_set raiseValueError(const char *message)
Raises a value error with the given message.
Definition: PybindUtils.h:80
pybind11::error_already_set raisePyError(PyObject *exc_class, const char *message)
Raises a python exception with the given message.
Definition: PybindUtils.h:71