Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling conventions of some Python foreign functions mismatch with their C implementations #5487

Closed
Elaphurus opened this issue May 11, 2021 · 1 comment · Fixed by #5488
Closed

Comments

@Elaphurus
Copy link

For example, Python foreign function ImagingCore.isblock() takes no arguments, as in its C implementation, arguments passed from the Python side will not be used:

Pillow/src/_imaging.c

Lines 2097 to 2100 in d393cfb

static PyObject *
_isblock(ImagingObject *self, PyObject *args) {
return PyBool_FromLong(self->image->block != NULL);
}

parameter args is not used in the function body.

However, its calling convention given by Python/C API when declaring the foreign function is METH_VARARGS (0x0001), which should be METH_NOARGS instead in this case:

{"isblock", (PyCFunction)_isblock, 1},

This mismatch makes parameter of any type can be passed to this foreign function designed to be parameter free without getting a TypeError:

from PIL import Image
im = Image.open("./test.png")
ic = im.getdata()
ic.isblock('str', 1) # passing anything is legal because of the mismatch

The same mismatch can be found for foreign functions ImagingCore.split(), ImagingCore.getbbox(), ImagingCore.chop_invert(), ImagingCore.getextrema(), ImagingCore.getpalettemode(), ImagingCore.getprojection().

We also found a mismatch in NumPy, which has been fixed in numpy/numpy#18679.

Please let me know if I miss something or get something wrong here.

@radarhere radarhere changed the title Calling conventions of some Python foreign functions mismatch with their C implementations. Calling conventions of some Python foreign functions mismatch with their C implementations May 11, 2021
@radarhere
Copy link
Member

I've created #5488 to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants