[Mlir-commits] [mlir] [mlir] Python: write bytecode to a file path (PR #127118)
Mehdi Amini
llvmlistbot at llvm.org
Mon Feb 24 14:27:34 PST 2025
================
@@ -128,33 +133,57 @@ struct PyPrintAccumulator {
}
};
-/// Accumulates int a python file-like object, either writing text (default)
-/// or binary.
+/// Accumulates into a file, either writing text (default)
+/// or binary. The file may be a Python file-like object or a path to a file.
class PyFileAccumulator {
public:
- PyFileAccumulator(const nanobind::object &fileObject, bool binary)
- : pyWriteFunction(fileObject.attr("write")), binary(binary) {}
+ PyFileAccumulator(const nanobind::object &fileOrStringObject, bool binary)
+ : binary(binary) {
+ std::string filePath;
+ if (nanobind::try_cast<std::string>(fileOrStringObject, filePath)) {
+ std::error_code ec;
+ writeTarget.emplace<llvm::raw_fd_ostream>(filePath, ec);
+ if (ec) {
+ throw nanobind::value_error("Unable to open file for writing");
----------------
joker-eph wrote:
I don't know if the `ec` can be used to provide a better error message? (like can we know if it is "permission denied" or "no space left on device" or...)
https://github.com/llvm/llvm-project/pull/127118
More information about the Mlir-commits
mailing list