Title: PyVectorcall_NARGS(): change return type to Py_ssize_t
Type: Stage: resolved
Components: C API Versions: Python 3.9
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jdemeyer, petr.viktorin, vstinner
Priority: normal Keywords:

Created on 2020-02-11 16:50 by vstinner, last changed 2020-02-18 13:58 by vstinner. This issue is now closed.

Messages (4)
msg361824 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-11 16:50
I propose to change PyVectorcall_NARGS() return type from unsigned size_t to signed Py_ssize_t.

Currently, the function is defined as:

static inline Py_ssize_t
PyVectorcall_NARGS(size_t n)

But in CPython code base, the result is always stored in a *signed* Py_ssize_t:

    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);

Sometimes, this nargs is passed to _PyObject_MakeTpCall() which expects nargs to be Py_ssize_t, so it's consistent.

In general in CPython, a size uses type Py_ssize_t, not size_t. Example: PyVarObject.ob_size type is Py_ssize_t.
msg362198 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-02-18 12:10
The current return type already is Py_ssize_t, exactly for the reason you mention – compatibility with all other "argument count" values in Python. (It would be more correct to use unsigned, but that ship has sailed.)

The *argument* type is unsigned size_t, though: unsigned is the correct type for for bit fields. Also, the "nargsf" value should never be directly used as argument count; making it a different type tends to trigger nice compiler warnings.
msg362199 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2020-02-18 12:20
Closing; please reopen if we're somehow misunderstanding each other :)
msg362215 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-18 13:58
Alright, I was confused by how PyVectorcall_NARGS() is supposed to be used.
Date User Action Args
2020-02-18 13:58:06vstinnersetmessages: + msg362215
2020-02-18 12:20:27petr.viktorinsetstatus: open -> closed
resolution: not a bug
messages: + msg362199

stage: resolved
2020-02-18 12:10:14petr.viktorinsetmessages: + msg362198
2020-02-11 16:50:02vstinnercreate