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
This commit is contained in:
Aleksandr Karpinskii 2024-09-17 23:57:26 +02:00
parent b89f791057
commit 05a67d17de
7 changed files with 8 additions and 110 deletions

View File

@ -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;
}

View File

@ -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) {

View File

@ -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);

View File

@ -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 */
}

View File

@ -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;
}

View File

@ -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 */
/* ------------------- */

View File

@ -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);