From 05a67d17de13d3f61c4653d5a35dc88b6502b102 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 727ee6bed51..670227c6cdd 100644 --- a/src/Tk/tkImaging.c +++ b/src/Tk/tkImaging.c @@ -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; } diff --git a/src/_imaging.c b/src/_imaging.c index b0116d7c00f..2aff085d431 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; @@ -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) { diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 1a410891f12..a5b2b89a7bc 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 f42ff9aec9e..00000000000 --- 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 76d0abccc4f..901fe83ad27 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 fb34b54de71..d09bce41e7c 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 522e9f37557..634ad71bfc4 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);