Skip to content

Commit 2f48a56

Browse files
committed
Add PIL._imaging.NotSupportedError
1 parent ba06125 commit 2f48a56

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/_imaging.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ ImagingError_ModeError(void) {
382382
return NULL;
383383
}
384384

385+
// Derives from both NotImplementedError and ValueError
386+
// (as the functions above raise ValueErrors).
387+
static PyObject *ImagingNotSupportedError = NULL;
388+
389+
void *
390+
ImagingError_NotSupportedError(const char *message) {
391+
PyErr_SetString(
392+
ImagingNotSupportedError ? ImagingNotSupportedError : PyExc_NotImplementedError,
393+
(message) ? (char *)message : "operation not supported"
394+
);
395+
return NULL;
396+
}
397+
385398
void *
386399
ImagingError_ValueError(const char *message) {
387400
PyErr_SetString(
@@ -4326,6 +4339,24 @@ setup_module(PyObject *m) {
43264339
return -1;
43274340
}
43284341

4342+
if (ImagingNotSupportedError == NULL) {
4343+
// NotSupportedError derives from both NotImplementedError and ValueError,
4344+
// for compatibility with `except ValueError`.
4345+
PyObject *bases = PyTuple_Pack(2, PyExc_NotImplementedError, PyExc_ValueError);
4346+
if (bases == NULL) {
4347+
return -1;
4348+
}
4349+
ImagingNotSupportedError =
4350+
PyErr_NewException("PIL._imaging.NotSupportedError", bases, NULL);
4351+
Py_DECREF(bases);
4352+
if (ImagingNotSupportedError == NULL) {
4353+
return -1;
4354+
}
4355+
}
4356+
if (PyModule_AddObjectRef(m, "NotSupportedError", ImagingNotSupportedError) < 0) {
4357+
return -1;
4358+
}
4359+
43294360
#ifdef HAVE_LIBJPEG
43304361
{
43314362
extern const char *ImagingJpegVersion(void);

src/libImaging/Imaging.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ extern void *
273273
ImagingError_Mismatch(void); /* maps to ValueError by default */
274274
extern void *
275275
ImagingError_ValueError(const char *message);
276+
extern void *
277+
ImagingError_NotSupportedError(
278+
const char *message
279+
); /* derives from NotImplementedError and ValueError */
276280

277281
/* Transform callbacks */
278282
/* ------------------- */

0 commit comments

Comments
 (0)