Skip to content

Commit 84c992d

Browse files
committed
Add PIL._imaging.NotSupportedError
1 parent 8f86212 commit 84c992d

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
@@ -381,6 +381,19 @@ ImagingError_ModeError(void) {
381381
return NULL;
382382
}
383383

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

4344+
if (ImagingNotSupportedError == NULL) {
4345+
// NotSupportedError derives from both NotImplementedError and ValueError,
4346+
// for compatibility with `except ValueError`.
4347+
PyObject *bases = PyTuple_Pack(2, PyExc_NotImplementedError, PyExc_ValueError);
4348+
if (bases == NULL) {
4349+
return -1;
4350+
}
4351+
ImagingNotSupportedError =
4352+
PyErr_NewException("PIL._imaging.NotSupportedError", bases, NULL);
4353+
Py_DECREF(bases);
4354+
if (ImagingNotSupportedError == NULL) {
4355+
return -1;
4356+
}
4357+
}
4358+
if (PyModule_AddObjectRef(m, "NotSupportedError", ImagingNotSupportedError) < 0) {
4359+
return -1;
4360+
}
4361+
43314362
#ifdef HAVE_LIBJPEG
43324363
{
43334364
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)