Skip to content

Commit 9fb6148

Browse files
Make DecodeError and ValidationError inherit from ValueError (#790)
* Add failing test * Make DecodeError a subclass of ValueError too * Fixup --------- Co-authored-by: Jim Crist-Harif <jcristharif@gmail.com>
1 parent f678049 commit 9fb6148

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

src/msgspec/_core.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22506,29 +22506,31 @@ PyInit__core(void)
2250622506
"Base class for all Msgspec exceptions",
2250722507
NULL, NULL
2250822508
);
22509-
if (st->MsgspecError == NULL)
22510-
return NULL;
22509+
if (st->MsgspecError == NULL) return NULL;
22510+
2251122511
st->EncodeError = PyErr_NewExceptionWithDoc(
2251222512
"msgspec.EncodeError",
2251322513
"An error occurred while encoding an object",
2251422514
st->MsgspecError, NULL
2251522515
);
22516-
if (st->EncodeError == NULL)
22517-
return NULL;
22516+
if (st->EncodeError == NULL) return NULL;
22517+
22518+
temp_obj = PyTuple_Pack(2, st->MsgspecError, PyExc_ValueError);
22519+
if (temp_obj == NULL) return NULL;
2251822520
st->DecodeError = PyErr_NewExceptionWithDoc(
2251922521
"msgspec.DecodeError",
2252022522
"An error occurred while decoding an object",
22521-
st->MsgspecError, NULL
22523+
temp_obj, NULL
2252222524
);
22523-
if (st->DecodeError == NULL)
22524-
return NULL;
22525+
Py_XDECREF(temp_obj);
22526+
if (st->DecodeError == NULL) return NULL;
22527+
2252522528
st->ValidationError = PyErr_NewExceptionWithDoc(
2252622529
"msgspec.ValidationError",
2252722530
"The message didn't match the expected schema",
2252822531
st->DecodeError, NULL
2252922532
);
22530-
if (st->ValidationError == NULL)
22531-
return NULL;
22533+
if (st->ValidationError == NULL) return NULL;
2253222534

2253322535
Py_INCREF(st->MsgspecError);
2253422536
if (PyModule_AddObject(m, "MsgspecError", st->MsgspecError) < 0)

0 commit comments

Comments
 (0)