diff --git a/mlir/lib/Bindings/Python/IRModules.cpp b/mlir/lib/Bindings/Python/IRModules.cpp --- a/mlir/lib/Bindings/Python/IRModules.cpp +++ b/mlir/lib/Bindings/Python/IRModules.cpp @@ -906,11 +906,31 @@ if (attributes) { mlirAttributes.reserve(attributes->size()); for (auto &it : *attributes) { - - auto name = it.first.cast(); - auto &attribute = it.second.cast(); - // TODO: Verify attribute originates from the same context. - mlirAttributes.emplace_back(std::move(name), attribute); + std::string key; + try { + key = it.first.cast(); + } catch (py::cast_error &err) { + std::string msg = "Invalid attribute key (not a string) when " + "attempting to create the operation \"" + + name + "\" (" + err.what() + ")"; + throw py::cast_error(msg); + } + try { + auto &attribute = it.second.cast(); + // TODO: Verify attribute originates from the same context. + mlirAttributes.emplace_back(std::move(key), attribute); + } catch (py::reference_cast_error &) { + // This exception seems thrown when the value is "None". + std::string msg = + "Found an invalid (`None`?) attribute value for the key \"" + key + + "\" when attempting to create the operation \"" + name + "\""; + throw py::cast_error(msg); + } catch (py::cast_error &err) { + std::string msg = "Invalid attribute value for the key \"" + key + + "\" when attempting to create the operation \"" + + name + "\" (" + err.what() + ")"; + throw py::cast_error(msg); + } } } // Unpack/validate successors. diff --git a/mlir/test/Bindings/Python/ir_operation.py b/mlir/test/Bindings/Python/ir_operation.py --- a/mlir/test/Bindings/Python/ir_operation.py +++ b/mlir/test/Bindings/Python/ir_operation.py @@ -551,3 +551,30 @@ # CHECK: "module"() ( { # CHECK: }) : () -> () run(testPrintInvalidOperation) + + +# CHECK-LABEL: TEST: testCreateWithInvalidAttributes +def testCreateWithInvalidAttributes(): + ctx = Context() + with Location.unknown(ctx): + try: + Operation.create("module", attributes={None:StringAttr.get("name")}) + except Exception as e: + # CHECK: Invalid attribute key (not a string) when attempting to create the operation "module" (Unable to cast Python instance of type to C++ type + print(e) + try: + Operation.create("module", attributes={42:StringAttr.get("name")}) + except Exception as e: + # CHECK: Invalid attribute key (not a string) when attempting to create the operation "module" (Unable to cast Python instance of type to C++ type + print(e) + try: + Operation.create("module", attributes={"some_key":ctx}) + except Exception as e: + # CHECK: Invalid attribute value for the key "some_key" when attempting to create the operation "module" (Unable to cast Python instance of type to C++ type 'mlir::python::PyAttribute') + print(e) + try: + Operation.create("module", attributes={"some_key":None}) + except Exception as e: + # CHECK: Found an invalid (`None`?) attribute value for the key "some_key" when attempting to create the operation "module" + print(e) +run(testCreateWithInvalidAttributes)