Use s# in PyArg_ParseTuple

This commit is contained in:
Aleksandr Karpinskii 2024-09-02 02:00:07 +04:00
parent f916b5dc87
commit 7f48567002
2 changed files with 16 additions and 40 deletions

View File

@ -11,7 +11,6 @@
* See the README file for information on usage and redistribution.
*/
#include "Python.h"
#include "libImaging/Imaging.h"
#define LUT_SIZE (1 << 9)
@ -30,7 +29,7 @@
static PyObject *
apply(PyObject *self, PyObject *args) {
const char *lut;
PyObject *py_lut, *i0, *i1;
PyObject *i0, *i1;
Py_ssize_t lut_len;
Imaging imgin, imgout;
int width, height;
@ -38,25 +37,15 @@ apply(PyObject *self, PyObject *args) {
UINT8 **inrows, **outrows;
int num_changed_pixels = 0;
if (!PyArg_ParseTuple(args, "OOO", &py_lut, &i0, &i1)) {
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
if (!PyArg_ParseTuple(args, "s#OO", &lut, &lut_len, &i0, &i1)) {
return NULL;
}
if (!PyBytes_Check(py_lut)) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
return NULL;
}
lut_len = PyBytes_Size(py_lut);
if (lut_len < LUT_SIZE) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
return NULL;
}
lut = PyBytes_AsString(py_lut);
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC) ||
!PyCapsule_IsValid(i1, IMAGING_MAGIC)) {
PyErr_Format(
@ -137,39 +126,22 @@ apply(PyObject *self, PyObject *args) {
static PyObject *
match(PyObject *self, PyObject *args) {
const char *lut;
PyObject *py_lut, *i0;
PyObject *i0;
Py_ssize_t lut_len;
Imaging imgin;
int width, height;
int row_idx, col_idx;
UINT8 **inrows;
PyObject *ret = PyList_New(0);
if (ret == NULL) {
if (!PyArg_ParseTuple(args, "s#O", &lut, &lut_len, &i0)) {
return NULL;
}
if (!PyArg_ParseTuple(args, "OO", &py_lut, &i0)) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
return NULL;
}
if (!PyBytes_Check(py_lut)) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
return NULL;
}
lut_len = PyBytes_Size(py_lut);
if (lut_len < LUT_SIZE) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
return NULL;
}
lut = PyBytes_AsString(py_lut);
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC)) {
PyErr_Format(
PyExc_TypeError, "Expected PyCapsule with '%s' name.", IMAGING_MAGIC
@ -180,11 +152,15 @@ match(PyObject *self, PyObject *args) {
imgin = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
if (imgin->type != IMAGING_TYPE_UINT8 || imgin->bands != 1) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
return NULL;
}
PyObject *ret = PyList_New(0);
if (ret == NULL) {
return NULL;
}
inrows = imgin->image8;
width = imgin->xsize;
height = imgin->ysize;
@ -236,14 +212,8 @@ get_on_pixels(PyObject *self, PyObject *args) {
UINT8 **rows;
int row_idx, col_idx;
int width, height;
PyObject *ret = PyList_New(0);
if (ret == NULL) {
return NULL;
}
if (!PyArg_ParseTuple(args, "O", &i0)) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
return NULL;
}
@ -259,6 +229,11 @@ get_on_pixels(PyObject *self, PyObject *args) {
width = img->xsize;
height = img->ysize;
PyObject *ret = PyList_New(0);
if (ret == NULL) {
return NULL;
}
for (row_idx = 0; row_idx < height; row_idx++) {
UINT8 *row = rows[row_idx];
for (col_idx = 0; col_idx < width; col_idx++) {

View File

@ -7,6 +7,7 @@
* Copyright (c) Fredrik Lundh 1995-2003.
*/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
/* Check that we have an ANSI compliant compiler */