Skip to content

Commit 35d5b50

Browse files
committed
[mlir][py] Mark all type caster from_{cpp,python} methods as noexcept
This is mentioned as a "must" in https://nanobind.readthedocs.io/en/latest/porting.html#type-casters when implementing type casters. While most of the existing `from_cpp` methods were already marked noexcept, many of the `from_python` methods were not. This commit adds the missing noexcept declarations to all type casters found in `NanobindAdaptors.h`.
1 parent db8d34d commit 35d5b50

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

mlir/include/mlir/Bindings/Python/NanobindAdaptors.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static nanobind::object mlirApiObjectToCapsule(nanobind::handle apiObject) {
6767
template <>
6868
struct type_caster<MlirAffineMap> {
6969
NB_TYPE_CASTER(MlirAffineMap, const_name("MlirAffineMap"))
70-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
70+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
7171
nanobind::object capsule = mlirApiObjectToCapsule(src);
7272
value = mlirPythonCapsuleToAffineMap(capsule.ptr());
7373
if (mlirAffineMapIsNull(value)) {
@@ -90,7 +90,7 @@ struct type_caster<MlirAffineMap> {
9090
template <>
9191
struct type_caster<MlirAttribute> {
9292
NB_TYPE_CASTER(MlirAttribute, const_name("MlirAttribute"))
93-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
93+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
9494
nanobind::object capsule = mlirApiObjectToCapsule(src);
9595
value = mlirPythonCapsuleToAttribute(capsule.ptr());
9696
return !mlirAttributeIsNull(value);
@@ -111,7 +111,7 @@ struct type_caster<MlirAttribute> {
111111
template <>
112112
struct type_caster<MlirBlock> {
113113
NB_TYPE_CASTER(MlirBlock, const_name("MlirBlock"))
114-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
114+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
115115
nanobind::object capsule = mlirApiObjectToCapsule(src);
116116
value = mlirPythonCapsuleToBlock(capsule.ptr());
117117
return !mlirBlockIsNull(value);
@@ -122,7 +122,7 @@ struct type_caster<MlirBlock> {
122122
template <>
123123
struct type_caster<MlirContext> {
124124
NB_TYPE_CASTER(MlirContext, const_name("MlirContext"))
125-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
125+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
126126
if (src.is_none()) {
127127
// Gets the current thread-bound context.
128128
// TODO: This raises an error of "No current context" currently.
@@ -142,7 +142,7 @@ struct type_caster<MlirContext> {
142142
template <>
143143
struct type_caster<MlirDialectRegistry> {
144144
NB_TYPE_CASTER(MlirDialectRegistry, const_name("MlirDialectRegistry"))
145-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
145+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
146146
nanobind::object capsule = mlirApiObjectToCapsule(src);
147147
value = mlirPythonCapsuleToDialectRegistry(capsule.ptr());
148148
return !mlirDialectRegistryIsNull(value);
@@ -162,7 +162,7 @@ struct type_caster<MlirDialectRegistry> {
162162
template <>
163163
struct type_caster<MlirLocation> {
164164
NB_TYPE_CASTER(MlirLocation, const_name("MlirLocation"))
165-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
165+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
166166
if (src.is_none()) {
167167
// Gets the current thread-bound context.
168168
src = nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
@@ -188,7 +188,7 @@ struct type_caster<MlirLocation> {
188188
template <>
189189
struct type_caster<MlirModule> {
190190
NB_TYPE_CASTER(MlirModule, const_name("MlirModule"))
191-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
191+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
192192
nanobind::object capsule = mlirApiObjectToCapsule(src);
193193
value = mlirPythonCapsuleToModule(capsule.ptr());
194194
return !mlirModuleIsNull(value);
@@ -209,12 +209,13 @@ template <>
209209
struct type_caster<MlirFrozenRewritePatternSet> {
210210
NB_TYPE_CASTER(MlirFrozenRewritePatternSet,
211211
const_name("MlirFrozenRewritePatternSet"))
212-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
212+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
213213
nanobind::object capsule = mlirApiObjectToCapsule(src);
214214
value = mlirPythonCapsuleToFrozenRewritePatternSet(capsule.ptr());
215215
return value.ptr != nullptr;
216216
}
217-
static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy, handle) {
217+
static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy,
218+
handle) noexcept {
218219
nanobind::object capsule = nanobind::steal<nanobind::object>(
219220
mlirPythonFrozenRewritePatternSetToCapsule(v));
220221
return nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("rewrite"))
@@ -228,7 +229,7 @@ struct type_caster<MlirFrozenRewritePatternSet> {
228229
template <>
229230
struct type_caster<MlirOperation> {
230231
NB_TYPE_CASTER(MlirOperation, const_name("MlirOperation"))
231-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
232+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
232233
nanobind::object capsule = mlirApiObjectToCapsule(src);
233234
value = mlirPythonCapsuleToOperation(capsule.ptr());
234235
return !mlirOperationIsNull(value);
@@ -250,7 +251,7 @@ struct type_caster<MlirOperation> {
250251
template <>
251252
struct type_caster<MlirValue> {
252253
NB_TYPE_CASTER(MlirValue, const_name("MlirValue"))
253-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
254+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
254255
nanobind::object capsule = mlirApiObjectToCapsule(src);
255256
value = mlirPythonCapsuleToValue(capsule.ptr());
256257
return !mlirValueIsNull(value);
@@ -273,7 +274,7 @@ struct type_caster<MlirValue> {
273274
template <>
274275
struct type_caster<MlirPassManager> {
275276
NB_TYPE_CASTER(MlirPassManager, const_name("MlirPassManager"))
276-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
277+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
277278
nanobind::object capsule = mlirApiObjectToCapsule(src);
278279
value = mlirPythonCapsuleToPassManager(capsule.ptr());
279280
return !mlirPassManagerIsNull(value);
@@ -284,7 +285,7 @@ struct type_caster<MlirPassManager> {
284285
template <>
285286
struct type_caster<MlirTypeID> {
286287
NB_TYPE_CASTER(MlirTypeID, const_name("MlirTypeID"))
287-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
288+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
288289
nanobind::object capsule = mlirApiObjectToCapsule(src);
289290
value = mlirPythonCapsuleToTypeID(capsule.ptr());
290291
return !mlirTypeIDIsNull(value);
@@ -306,7 +307,7 @@ struct type_caster<MlirTypeID> {
306307
template <>
307308
struct type_caster<MlirType> {
308309
NB_TYPE_CASTER(MlirType, const_name("MlirType"))
309-
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
310+
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
310311
nanobind::object capsule = mlirApiObjectToCapsule(src);
311312
value = mlirPythonCapsuleToType(capsule.ptr());
312313
return !mlirTypeIsNull(value);

0 commit comments

Comments
 (0)