20
20
#define MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
21
21
22
22
#include < cstdint>
23
+ #include < optional>
23
24
24
25
#include " mlir-c/Diagnostics.h"
25
26
#include " mlir-c/IR.h"
@@ -43,18 +44,14 @@ namespace detail {
43
44
// / with a raw handle (unowned). The returned object's lifetime may not extend
44
45
// / beyond the apiObject handle without explicitly having its refcount increased
45
46
// / (i.e. on return).
46
- static nanobind::object mlirApiObjectToCapsule (nanobind::handle apiObject) {
47
+ static std::optional<nanobind::object>
48
+ mlirApiObjectToCapsule (nanobind::handle apiObject) {
47
49
if (PyCapsule_CheckExact (apiObject.ptr ()))
48
50
return nanobind::borrow<nanobind::object>(apiObject);
49
51
nanobind::object api =
50
52
nanobind::getattr (apiObject, MLIR_PYTHON_CAPI_PTR_ATTR, nanobind::none ());
51
- if (api.is_none ()) {
52
- std::string repr = nanobind::cast<std::string>(nanobind::repr (apiObject));
53
- throw nanobind::type_error (
54
- (llvm::Twine (" Expected an MLIR object (got " ) + repr + " )." )
55
- .str ()
56
- .c_str ());
57
- }
53
+ if (api.is_none ())
54
+ return std::nullopt;
58
55
return api;
59
56
}
60
57
@@ -68,11 +65,10 @@ template <>
68
65
struct type_caster <MlirAffineMap> {
69
66
NB_TYPE_CASTER (MlirAffineMap, const_name(" MlirAffineMap" ))
70
67
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
71
- nanobind::object capsule = mlirApiObjectToCapsule (src);
72
- value = mlirPythonCapsuleToAffineMap (capsule.ptr ());
73
- if (mlirAffineMapIsNull (value)) {
68
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
69
+ if (!capsule)
74
70
return false ;
75
- }
71
+ value = mlirPythonCapsuleToAffineMap (capsule. value (). ptr ());
76
72
return !mlirAffineMapIsNull (value);
77
73
}
78
74
static handle from_cpp (MlirAffineMap v, rv_policy,
@@ -91,8 +87,10 @@ template <>
91
87
struct type_caster <MlirAttribute> {
92
88
NB_TYPE_CASTER (MlirAttribute, const_name(" MlirAttribute" ))
93
89
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
94
- nanobind::object capsule = mlirApiObjectToCapsule (src);
95
- value = mlirPythonCapsuleToAttribute (capsule.ptr ());
90
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
91
+ if (!capsule)
92
+ return false ;
93
+ value = mlirPythonCapsuleToAttribute (capsule.value ().ptr ());
96
94
return !mlirAttributeIsNull (value);
97
95
}
98
96
static handle from_cpp (MlirAttribute v, rv_policy,
@@ -112,8 +110,10 @@ template <>
112
110
struct type_caster <MlirBlock> {
113
111
NB_TYPE_CASTER (MlirBlock, const_name(" MlirBlock" ))
114
112
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
115
- nanobind::object capsule = mlirApiObjectToCapsule (src);
116
- value = mlirPythonCapsuleToBlock (capsule.ptr ());
113
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
114
+ if (!capsule)
115
+ return false ;
116
+ value = mlirPythonCapsuleToBlock (capsule.value ().ptr ());
117
117
return !mlirBlockIsNull (value);
118
118
}
119
119
};
@@ -132,8 +132,8 @@ struct type_caster<MlirContext> {
132
132
.attr (" Context" )
133
133
.attr (" current" );
134
134
}
135
- nanobind::object capsule = mlirApiObjectToCapsule (src);
136
- value = mlirPythonCapsuleToContext (capsule.ptr ());
135
+ std::optional< nanobind::object> capsule = mlirApiObjectToCapsule (src);
136
+ value = mlirPythonCapsuleToContext (capsule.value (). ptr ());
137
137
return !mlirContextIsNull (value);
138
138
}
139
139
};
@@ -143,8 +143,10 @@ template <>
143
143
struct type_caster <MlirDialectRegistry> {
144
144
NB_TYPE_CASTER (MlirDialectRegistry, const_name(" MlirDialectRegistry" ))
145
145
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
146
- nanobind::object capsule = mlirApiObjectToCapsule (src);
147
- value = mlirPythonCapsuleToDialectRegistry (capsule.ptr ());
146
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
147
+ if (!capsule)
148
+ return false ;
149
+ value = mlirPythonCapsuleToDialectRegistry (capsule.value ().ptr ());
148
150
return !mlirDialectRegistryIsNull (value);
149
151
}
150
152
static handle from_cpp (MlirDialectRegistry v, rv_policy,
@@ -169,8 +171,8 @@ struct type_caster<MlirLocation> {
169
171
.attr (" Location" )
170
172
.attr (" current" );
171
173
}
172
- nanobind::object capsule = mlirApiObjectToCapsule (src);
173
- value = mlirPythonCapsuleToLocation (capsule.ptr ());
174
+ std::optional< nanobind::object> capsule = mlirApiObjectToCapsule (src);
175
+ value = mlirPythonCapsuleToLocation (capsule.value (). ptr ());
174
176
return !mlirLocationIsNull (value);
175
177
}
176
178
static handle from_cpp (MlirLocation v, rv_policy,
@@ -189,8 +191,10 @@ template <>
189
191
struct type_caster <MlirModule> {
190
192
NB_TYPE_CASTER (MlirModule, const_name(" MlirModule" ))
191
193
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
192
- nanobind::object capsule = mlirApiObjectToCapsule (src);
193
- value = mlirPythonCapsuleToModule (capsule.ptr ());
194
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
195
+ if (!capsule)
196
+ return false ;
197
+ value = mlirPythonCapsuleToModule (capsule.value ().ptr ());
194
198
return !mlirModuleIsNull (value);
195
199
}
196
200
static handle from_cpp (MlirModule v, rv_policy,
@@ -210,8 +214,10 @@ struct type_caster<MlirFrozenRewritePatternSet> {
210
214
NB_TYPE_CASTER (MlirFrozenRewritePatternSet,
211
215
const_name (" MlirFrozenRewritePatternSet" ))
212
216
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
213
- nanobind::object capsule = mlirApiObjectToCapsule (src);
214
- value = mlirPythonCapsuleToFrozenRewritePatternSet (capsule.ptr ());
217
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
218
+ if (!capsule)
219
+ return false ;
220
+ value = mlirPythonCapsuleToFrozenRewritePatternSet (capsule.value ().ptr ());
215
221
return value.ptr != nullptr ;
216
222
}
217
223
static handle from_cpp (MlirFrozenRewritePatternSet v, rv_policy,
@@ -230,8 +236,10 @@ template <>
230
236
struct type_caster <MlirOperation> {
231
237
NB_TYPE_CASTER (MlirOperation, const_name(" MlirOperation" ))
232
238
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
233
- nanobind::object capsule = mlirApiObjectToCapsule (src);
234
- value = mlirPythonCapsuleToOperation (capsule.ptr ());
239
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
240
+ if (!capsule)
241
+ return false ;
242
+ value = mlirPythonCapsuleToOperation (capsule.value ().ptr ());
235
243
return !mlirOperationIsNull (value);
236
244
}
237
245
static handle from_cpp (MlirOperation v, rv_policy,
@@ -252,8 +260,10 @@ template <>
252
260
struct type_caster <MlirValue> {
253
261
NB_TYPE_CASTER (MlirValue, const_name(" MlirValue" ))
254
262
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
255
- nanobind::object capsule = mlirApiObjectToCapsule (src);
256
- value = mlirPythonCapsuleToValue (capsule.ptr ());
263
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
264
+ if (!capsule)
265
+ return false ;
266
+ value = mlirPythonCapsuleToValue (capsule.value ().ptr ());
257
267
return !mlirValueIsNull (value);
258
268
}
259
269
static handle from_cpp (MlirValue v, rv_policy,
@@ -275,8 +285,10 @@ template <>
275
285
struct type_caster <MlirPassManager> {
276
286
NB_TYPE_CASTER (MlirPassManager, const_name(" MlirPassManager" ))
277
287
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
278
- nanobind::object capsule = mlirApiObjectToCapsule (src);
279
- value = mlirPythonCapsuleToPassManager (capsule.ptr ());
288
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
289
+ if (!capsule)
290
+ return false ;
291
+ value = mlirPythonCapsuleToPassManager (capsule.value ().ptr ());
280
292
return !mlirPassManagerIsNull (value);
281
293
}
282
294
};
@@ -286,8 +298,10 @@ template <>
286
298
struct type_caster <MlirTypeID> {
287
299
NB_TYPE_CASTER (MlirTypeID, const_name(" MlirTypeID" ))
288
300
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
289
- nanobind::object capsule = mlirApiObjectToCapsule (src);
290
- value = mlirPythonCapsuleToTypeID (capsule.ptr ());
301
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
302
+ if (!capsule)
303
+ return false ;
304
+ value = mlirPythonCapsuleToTypeID (capsule.value ().ptr ());
291
305
return !mlirTypeIDIsNull (value);
292
306
}
293
307
static handle from_cpp (MlirTypeID v, rv_policy,
@@ -308,8 +322,10 @@ template <>
308
322
struct type_caster <MlirType> {
309
323
NB_TYPE_CASTER (MlirType, const_name(" MlirType" ))
310
324
bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
311
- nanobind::object capsule = mlirApiObjectToCapsule (src);
312
- value = mlirPythonCapsuleToType (capsule.ptr ());
325
+ std::optional<nanobind::object> capsule = mlirApiObjectToCapsule (src);
326
+ if (!capsule)
327
+ return false ;
328
+ value = mlirPythonCapsuleToType (capsule.value ().ptr ());
313
329
return !mlirTypeIsNull (value);
314
330
}
315
331
static handle from_cpp (MlirType t, rv_policy,
0 commit comments