From 37b2a1bbaeb0d2729f5cef1efe7786cf98be1da7 Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Tue, 17 Sep 2024 23:57:26 +0200 Subject: [PATCH] Cleanup exceptions handling Remove unused module Remove ImagingError_Clear alias Do not set PyExc_TypeError after PySequence_Fast Remove ImagingError_OSError alias Use PyErr_Format when possible --- src/Tk/tkImaging.c | 6 +--- src/_imaging.c | 13 -------- src/libImaging/Convert.c | 19 +++-------- src/libImaging/Except.c | 72 ---------------------------------------- src/libImaging/File.c | 2 +- src/libImaging/Imaging.h | 4 --- src/libImaging/Storage.c | 2 +- 7 files changed, 8 insertions(+), 110 deletions(-) delete mode 100644 src/libImaging/Except.c diff --git a/src/Tk/tkImaging.c b/src/Tk/tkImaging.c index a36c3e0bd..0234d2c78 100644 --- a/src/Tk/tkImaging.c +++ b/src/Tk/tkImaging.c @@ -243,13 +243,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; } diff --git a/src/_imaging.c b/src/_imaging.c index 172d46c55..7dad9f80a 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -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(); @@ -250,11 +244,6 @@ ImagingError_ValueError(const char *message) { return NULL; } -void -ImagingError_Clear(void) { - PyErr_Clear(); -} - /* -------------------------------------------------------------------- */ /* HELPERS */ /* -------------------------------------------------------------------- */ @@ -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; @@ -1593,7 +1581,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) { diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 1a410891f..a5b2b89a7 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -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); @@ -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); diff --git a/src/libImaging/Except.c b/src/libImaging/Except.c deleted file mode 100644 index f42ff9aec..000000000 --- a/src/libImaging/Except.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * The Python Imaging Library - * $Id$ - * - * default exception handling - * - * This module is usually overridden by application code (e.g. - * _imaging.c for PIL's standard Python bindings). If you get - * linking errors, remove this file from your project/library. - * - * history: - * 1995-06-15 fl Created - * 1998-12-29 fl Minor tweaks - * 2003-09-13 fl Added ImagingEnter/LeaveSection() - * - * Copyright (c) 1997-2003 by Secret Labs AB. - * Copyright (c) 1995-2003 by Fredrik Lundh. - * - * See the README file for information on usage and redistribution. - */ - -#include "Imaging.h" - -/* exception state */ - -void * -ImagingError_OSError(void) { - fprintf(stderr, "*** exception: file access error\n"); - return NULL; -} - -void * -ImagingError_MemoryError(void) { - fprintf(stderr, "*** exception: out of memory\n"); - return NULL; -} - -void * -ImagingError_ModeError(void) { - return ImagingError_ValueError("bad image mode"); -} - -void * -ImagingError_Mismatch(void) { - return ImagingError_ValueError("images don't match"); -} - -void * -ImagingError_ValueError(const char *message) { - if (!message) { - message = "exception: bad argument to function"; - } - fprintf(stderr, "*** %s\n", message); - return NULL; -} - -void -ImagingError_Clear(void) { - /* nop */; -} - -/* thread state */ - -void -ImagingSectionEnter(ImagingSectionCookie *cookie) { - /* pass */ -} - -void -ImagingSectionLeave(ImagingSectionCookie *cookie) { - /* pass */ -} diff --git a/src/libImaging/File.c b/src/libImaging/File.c index 76d0abccc..901fe83ad 100644 --- a/src/libImaging/File.c +++ b/src/libImaging/File.c @@ -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; } diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index fbd67af28..945d861a5 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -237,8 +237,6 @@ ImagingSectionLeave(ImagingSectionCookie *cookie); /* Exceptions */ /* ---------- */ -extern void * -ImagingError_OSError(void); extern void * ImagingError_MemoryError(void); extern void * @@ -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 */ /* ------------------- */ diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index 522e9f375..634ad71bf 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -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);