Skip to content

Commit 58da170

Browse files
committed
Inspect arg as Python object, instead of using PyErr_Clear()
1 parent 095bbc3 commit 58da170

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

src/_imaging.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,11 @@ static PyObject *
10491049
_convert_matrix(ImagingObject *self, PyObject *args) {
10501050
char *mode_name;
10511051
float m[12];
1052-
if (!PyArg_ParseTuple(args, "s(ffff)", &mode_name, m + 0, m + 1, m + 2, m + 3)) {
1053-
PyErr_Clear();
1052+
PyObject *matrix;
1053+
if (!PyArg_ParseTuple(args, "sO", &mode_name, &matrix)) {
1054+
return NULL;
1055+
}
1056+
if (PyTuple_Size(matrix) == 12) {
10541057
if (!PyArg_ParseTuple(
10551058
args,
10561059
"s(ffffffffffff)",
@@ -1070,27 +1073,35 @@ _convert_matrix(ImagingObject *self, PyObject *args) {
10701073
)) {
10711074
return NULL;
10721075
}
1076+
} else if (!PyArg_ParseTuple(
1077+
args, "s(ffff)", &mode_name, m + 0, m + 1, m + 2, m + 3
1078+
)) {
1079+
return NULL;
10731080
}
10741081

10751082
const ModeID mode = findModeID(mode_name);
1076-
10771083
return PyImagingNew(ImagingConvertMatrix(self->image, mode, m));
10781084
}
10791085

10801086
static PyObject *
10811087
_convert_transparent(ImagingObject *self, PyObject *args) {
10821088
char *mode_name;
1083-
int r, g, b;
1084-
if (PyArg_ParseTuple(args, "s(iii)", &mode_name, &r, &g, &b)) {
1085-
const ModeID mode = findModeID(mode_name);
1086-
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, g, b));
1089+
int r, g = 0, b = 0;
1090+
PyObject *transparency;
1091+
if (!PyArg_ParseTuple(args, "sO", &mode_name, &transparency)) {
1092+
return NULL;
10871093
}
1088-
PyErr_Clear();
1089-
if (PyArg_ParseTuple(args, "si", &mode_name, &r)) {
1090-
const ModeID mode = findModeID(mode_name);
1091-
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, 0, 0));
1094+
1095+
if (PyTuple_Check(transparency)) {
1096+
if (!PyArg_ParseTuple(args, "s(iii)", &mode_name, &r, &g, &b)) {
1097+
return NULL;
1098+
}
1099+
} else if (!PyArg_ParseTuple(args, "si", &mode_name, &r)) {
1100+
return NULL;
10921101
}
1093-
return NULL;
1102+
1103+
const ModeID mode = findModeID(mode_name);
1104+
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, g, b));
10941105
}
10951106

10961107
static PyObject *

0 commit comments

Comments
 (0)