Skip to content

Commit

Permalink
Cleanup exceptions handling
Browse files Browse the repository at this point in the history
Remove unused module
Remove ImagingError_Clear alias
Do not set PyExc_TypeError after PySequence_Fast
Remove ImagingError_OSError alias
Use PyErr_Format when possible
  • Loading branch information
homm committed Sep 18, 2024
1 parent b89f791 commit 05a67d1
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 110 deletions.
6 changes: 1 addition & 5 deletions src/Tk/tkImaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,9 @@ _dfunc(HMODULE lib_handle, const char *func_name) {
* Set Python exception if we can't find `func_name` in `lib_handle`.
* Returns function pointer or NULL if not present.
*/

char message[100];

FARPROC func = GetProcAddress(lib_handle, func_name);
if (func == NULL) {
sprintf(message, "Cannot load function %s", func_name);
PyErr_SetString(PyExc_RuntimeError, message);
PyErr_Format(PyExc_RuntimeError, "Cannot load function %s", func_name);
}
return func;
}
Expand Down
13 changes: 0 additions & 13 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,6 @@ static const char *no_palette = "image has no palette";
static const char *readonly = "image is readonly";
/* static const char* no_content = "image has no content"; */

void *
ImagingError_OSError(void) {
PyErr_SetString(PyExc_OSError, "error when accessing file");
return NULL;
}

void *
ImagingError_MemoryError(void) {
return PyErr_NoMemory();
Expand All @@ -250,11 +244,6 @@ ImagingError_ValueError(const char *message) {
return NULL;
}

void
ImagingError_Clear(void) {
PyErr_Clear();
}

/* -------------------------------------------------------------------- */
/* HELPERS */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -1534,7 +1523,6 @@ _putdata(ImagingObject *self, PyObject *args) {
} else {
seq = PySequence_Fast(data, must_be_sequence);
if (!seq) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
double value;
Expand Down Expand Up @@ -1597,7 +1585,6 @@ _putdata(ImagingObject *self, PyObject *args) {
/* 32-bit images */
seq = PySequence_Fast(data, must_be_sequence);
if (!seq) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
switch (image->type) {
Expand Down
19 changes: 5 additions & 14 deletions src/libImaging/Convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -1669,15 +1669,9 @@ convert(
}

if (!convert) {
#ifdef notdef
return (Imaging)ImagingError_ValueError("conversion not supported");
#else
static char buf[100];
snprintf(
buf, 100, "conversion from %.10s to %.10s not supported", imIn->mode, mode
return (Imaging)PyErr_Format(
PyExc_ValueError, "conversion from %s to %s not supported", imIn->mode, mode
);
return (Imaging)ImagingError_ValueError(buf);
#endif
}

imOut = ImagingNew2Dirty(mode, imOut, imIn);
Expand Down Expand Up @@ -1746,15 +1740,12 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
}
g = b = r;
} else {
static char buf[100];
snprintf(
buf,
100,
"conversion from %.10s to %.10s not supported in convert_transparent",
return (Imaging)PyErr_Format(
PyExc_ValueError,
"conversion from %s to %s not supported in convert_transparent",
imIn->mode,
mode
);
return (Imaging)ImagingError_ValueError(buf);
}

imOut = ImagingNew2Dirty(mode, imOut, imIn);
Expand Down
72 changes: 0 additions & 72 deletions src/libImaging/Except.c

This file was deleted.

2 changes: 1 addition & 1 deletion src/libImaging/File.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ImagingSavePPM(Imaging im, const char *outfile) {

fp = fopen(outfile, "wb");
if (!fp) {
(void)ImagingError_OSError();
PyErr_SetString(PyExc_OSError, "error when accessing file");
return 0;
}

Expand Down
4 changes: 0 additions & 4 deletions src/libImaging/Imaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ ImagingSectionLeave(ImagingSectionCookie *cookie);
/* Exceptions */
/* ---------- */

extern void *
ImagingError_OSError(void);
extern void *
ImagingError_MemoryError(void);
extern void *
Expand All @@ -247,8 +245,6 @@ extern void *
ImagingError_Mismatch(void); /* maps to ValueError by default */
extern void *
ImagingError_ValueError(const char *message);
extern void
ImagingError_Clear(void);

/* Transform callbacks */
/* ------------------- */
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/Storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ ImagingNewInternal(const char *mode, int xsize, int ysize, int dirty) {
return im;
}

ImagingError_Clear();
PyErr_Clear();

// Try to allocate the image once more with smallest possible block size
MUTEX_LOCK(&ImagingDefaultArena.mutex);
Expand Down

0 comments on commit 05a67d1

Please sign in to comment.