Skip to content

Commit 05a67d1

Browse files
committed
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
1 parent b89f791 commit 05a67d1

File tree

7 files changed

+8
-110
lines changed

7 files changed

+8
-110
lines changed

src/Tk/tkImaging.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,9 @@ _dfunc(HMODULE lib_handle, const char *func_name) {
228228
* Set Python exception if we can't find `func_name` in `lib_handle`.
229229
* Returns function pointer or NULL if not present.
230230
*/
231-
232-
char message[100];
233-
234231
FARPROC func = GetProcAddress(lib_handle, func_name);
235232
if (func == NULL) {
236-
sprintf(message, "Cannot load function %s", func_name);
237-
PyErr_SetString(PyExc_RuntimeError, message);
233+
PyErr_Format(PyExc_RuntimeError, "Cannot load function %s", func_name);
238234
}
239235
return func;
240236
}

src/_imaging.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,6 @@ static const char *no_palette = "image has no palette";
219219
static const char *readonly = "image is readonly";
220220
/* static const char* no_content = "image has no content"; */
221221

222-
void *
223-
ImagingError_OSError(void) {
224-
PyErr_SetString(PyExc_OSError, "error when accessing file");
225-
return NULL;
226-
}
227-
228222
void *
229223
ImagingError_MemoryError(void) {
230224
return PyErr_NoMemory();
@@ -250,11 +244,6 @@ ImagingError_ValueError(const char *message) {
250244
return NULL;
251245
}
252246

253-
void
254-
ImagingError_Clear(void) {
255-
PyErr_Clear();
256-
}
257-
258247
/* -------------------------------------------------------------------- */
259248
/* HELPERS */
260249
/* -------------------------------------------------------------------- */
@@ -1534,7 +1523,6 @@ _putdata(ImagingObject *self, PyObject *args) {
15341523
} else {
15351524
seq = PySequence_Fast(data, must_be_sequence);
15361525
if (!seq) {
1537-
PyErr_SetString(PyExc_TypeError, must_be_sequence);
15381526
return NULL;
15391527
}
15401528
double value;
@@ -1597,7 +1585,6 @@ _putdata(ImagingObject *self, PyObject *args) {
15971585
/* 32-bit images */
15981586
seq = PySequence_Fast(data, must_be_sequence);
15991587
if (!seq) {
1600-
PyErr_SetString(PyExc_TypeError, must_be_sequence);
16011588
return NULL;
16021589
}
16031590
switch (image->type) {

src/libImaging/Convert.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,15 +1669,9 @@ convert(
16691669
}
16701670

16711671
if (!convert) {
1672-
#ifdef notdef
1673-
return (Imaging)ImagingError_ValueError("conversion not supported");
1674-
#else
1675-
static char buf[100];
1676-
snprintf(
1677-
buf, 100, "conversion from %.10s to %.10s not supported", imIn->mode, mode
1672+
return (Imaging)PyErr_Format(
1673+
PyExc_ValueError, "conversion from %s to %s not supported", imIn->mode, mode
16781674
);
1679-
return (Imaging)ImagingError_ValueError(buf);
1680-
#endif
16811675
}
16821676

16831677
imOut = ImagingNew2Dirty(mode, imOut, imIn);
@@ -1746,15 +1740,12 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
17461740
}
17471741
g = b = r;
17481742
} else {
1749-
static char buf[100];
1750-
snprintf(
1751-
buf,
1752-
100,
1753-
"conversion from %.10s to %.10s not supported in convert_transparent",
1743+
return (Imaging)PyErr_Format(
1744+
PyExc_ValueError,
1745+
"conversion from %s to %s not supported in convert_transparent",
17541746
imIn->mode,
17551747
mode
17561748
);
1757-
return (Imaging)ImagingError_ValueError(buf);
17581749
}
17591750

17601751
imOut = ImagingNew2Dirty(mode, imOut, imIn);

src/libImaging/Except.c

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/libImaging/File.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ ImagingSavePPM(Imaging im, const char *outfile) {
5454

5555
fp = fopen(outfile, "wb");
5656
if (!fp) {
57-
(void)ImagingError_OSError();
57+
PyErr_SetString(PyExc_OSError, "error when accessing file");
5858
return 0;
5959
}
6060

src/libImaging/Imaging.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ ImagingSectionLeave(ImagingSectionCookie *cookie);
237237
/* Exceptions */
238238
/* ---------- */
239239

240-
extern void *
241-
ImagingError_OSError(void);
242240
extern void *
243241
ImagingError_MemoryError(void);
244242
extern void *
@@ -247,8 +245,6 @@ extern void *
247245
ImagingError_Mismatch(void); /* maps to ValueError by default */
248246
extern void *
249247
ImagingError_ValueError(const char *message);
250-
extern void
251-
ImagingError_Clear(void);
252248

253249
/* Transform callbacks */
254250
/* ------------------- */

src/libImaging/Storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ ImagingNewInternal(const char *mode, int xsize, int ysize, int dirty) {
513513
return im;
514514
}
515515

516-
ImagingError_Clear();
516+
PyErr_Clear();
517517

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

0 commit comments

Comments
 (0)