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 committed by Александр Карпинский
parent 3a16a350cc
commit 37b2a1bbae
7 changed files with 8 additions and 110 deletions

View File

@ -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`. * Set Python exception if we can't find `func_name` in `lib_handle`.
* Returns function pointer or NULL if not present. * Returns function pointer or NULL if not present.
*/ */
char message[100];
FARPROC func = GetProcAddress(lib_handle, func_name); FARPROC func = GetProcAddress(lib_handle, func_name);
if (func == NULL) { if (func == NULL) {
sprintf(message, "Cannot load function %s", func_name); PyErr_Format(PyExc_RuntimeError, "Cannot load function %s", func_name);
PyErr_SetString(PyExc_RuntimeError, message);
} }
return func; 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 *readonly = "image is readonly";
/* static const char* no_content = "image has no content"; */ /* static const char* no_content = "image has no content"; */
void *
ImagingError_OSError(void) {
PyErr_SetString(PyExc_OSError, "error when accessing file");
return NULL;
}
void * void *
ImagingError_MemoryError(void) { ImagingError_MemoryError(void) {
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -250,11 +244,6 @@ ImagingError_ValueError(const char *message) {
return NULL; return NULL;
} }
void
ImagingError_Clear(void) {
PyErr_Clear();
}
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* HELPERS */ /* HELPERS */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
@ -1534,7 +1523,6 @@ _putdata(ImagingObject *self, PyObject *args) {
} else { } else {
seq = PySequence_Fast(data, must_be_sequence); seq = PySequence_Fast(data, must_be_sequence);
if (!seq) { if (!seq) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL; return NULL;
} }
double value; double value;
@ -1593,7 +1581,6 @@ _putdata(ImagingObject *self, PyObject *args) {
/* 32-bit images */ /* 32-bit images */
seq = PySequence_Fast(data, must_be_sequence); seq = PySequence_Fast(data, must_be_sequence);
if (!seq) { if (!seq) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL; return NULL;
} }
switch (image->type) { switch (image->type) {

View File

@ -1669,15 +1669,9 @@ convert(
} }
if (!convert) { if (!convert) {
#ifdef notdef return (Imaging)PyErr_Format(
return (Imaging)ImagingError_ValueError("conversion not supported"); PyExc_ValueError, "conversion from %s to %s not supported", imIn->mode, mode
#else
static char buf[100];
snprintf(
buf, 100, "conversion from %.10s to %.10s not supported", imIn->mode, mode
); );
return (Imaging)ImagingError_ValueError(buf);
#endif
} }
imOut = ImagingNew2Dirty(mode, imOut, imIn); imOut = ImagingNew2Dirty(mode, imOut, imIn);
@ -1746,15 +1740,12 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
} }
g = b = r; g = b = r;
} else { } else {
static char buf[100]; return (Imaging)PyErr_Format(
snprintf( PyExc_ValueError,
buf, "conversion from %s to %s not supported in convert_transparent",
100,
"conversion from %.10s to %.10s not supported in convert_transparent",
imIn->mode, imIn->mode,
mode mode
); );
return (Imaging)ImagingError_ValueError(buf);
} }
imOut = ImagingNew2Dirty(mode, imOut, imIn); 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"); fp = fopen(outfile, "wb");
if (!fp) { if (!fp) {
(void)ImagingError_OSError(); PyErr_SetString(PyExc_OSError, "error when accessing file");
return 0; return 0;
} }

View File

@ -237,8 +237,6 @@ ImagingSectionLeave(ImagingSectionCookie *cookie);
/* Exceptions */ /* Exceptions */
/* ---------- */ /* ---------- */
extern void *
ImagingError_OSError(void);
extern void * extern void *
ImagingError_MemoryError(void); ImagingError_MemoryError(void);
extern void * extern void *
@ -247,8 +245,6 @@ extern void *
ImagingError_Mismatch(void); /* maps to ValueError by default */ ImagingError_Mismatch(void); /* maps to ValueError by default */
extern void * extern void *
ImagingError_ValueError(const char *message); ImagingError_ValueError(const char *message);
extern void
ImagingError_Clear(void);
/* Transform callbacks */ /* Transform callbacks */
/* ------------------- */ /* ------------------- */

View File

@ -513,7 +513,7 @@ ImagingNewInternal(const char *mode, int xsize, int ysize, int dirty) {
return im; return im;
} }
ImagingError_Clear(); PyErr_Clear();
// Try to allocate the image once more with smallest possible block size // Try to allocate the image once more with smallest possible block size
MUTEX_LOCK(&ImagingDefaultArena.mutex); MUTEX_LOCK(&ImagingDefaultArena.mutex);