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

PyVectorcall_NARGS(): change return type to Py_ssize_t #83792

Closed
vstinner opened this issue Feb 11, 2020 · 4 comments
Closed

PyVectorcall_NARGS(): change return type to Py_ssize_t #83792

vstinner opened this issue Feb 11, 2020 · 4 comments
Labels
3.9 only security fixes topic-C-API

Comments

@vstinner
Copy link
Member

BPO 39611
Nosy @vstinner, @encukou, @jdemeyer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-02-18.12:20:27.391>
created_at = <Date 2020-02-11.16:50:02.061>
labels = ['expert-C-API', 'invalid', '3.9']
title = 'PyVectorcall_NARGS(): change return type to Py_ssize_t'
updated_at = <Date 2020-02-18.13:58:06.526>
user = 'https://github.com/vstinner'

bugs.python.org fields:

activity = <Date 2020-02-18.13:58:06.526>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2020-02-18.12:20:27.391>
closer = 'petr.viktorin'
components = ['C API']
creation = <Date 2020-02-11.16:50:02.061>
creator = 'vstinner'
dependencies = []
files = []
hgrepos = []
issue_num = 39611
keywords = []
message_count = 4.0
messages = ['361824', '362198', '362199', '362215']
nosy_count = 3.0
nosy_names = ['vstinner', 'petr.viktorin', 'jdemeyer']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue39611'
versions = ['Python 3.9']

@vstinner
Copy link
Member Author

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)
{
    return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
}

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.

@vstinner vstinner added 3.9 only security fixes topic-C-API labels Feb 11, 2020
@encukou
Copy link
Member

encukou commented Feb 18, 2020

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.

@encukou
Copy link
Member

encukou commented Feb 18, 2020

Closing; please reopen if we're somehow misunderstanding each other :)

@vstinner
Copy link
Member Author

Alright, I was confused by how PyVectorcall_NARGS() is supposed to be used.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes topic-C-API
Projects
None yet
Development

No branches or pull requests

2 participants