Skip to content

bpo-30459: Cast the result of PyList_SET_ITEM() to void #19975

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

Merged
merged 1 commit into from
Dec 5, 2020

Conversation

ZackerySpytz
Copy link
Contributor

@ZackerySpytz ZackerySpytz commented May 7, 2020

Do the same for PyTuple_SET_ITEM().

https://bugs.python.org/issue30459

Do the same for PyTuple_SET_ITEM().
@@ -34,7 +34,7 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
#define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op))

#define PyList_GET_ITEM(op, i) (_PyList_CAST(op)->ob_item[i])
#define PyList_SET_ITEM(op, i, v) (_PyList_CAST(op)->ob_item[i] = (v))
#define PyList_SET_ITEM(op, i, v) ((void)(_PyList_CAST(op)->ob_item[i] = (v)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a change is made, I would prefer to convert this macro to a static inline function. You need to keep a macro for _PyList_CAST(). For example, declare the static inline function with name _PyList_SET_ITEM(). See Py_INCREF() for another example. Also, while we are here, I would prefer to use better name than "i" and "v". For example: PyList_SET_ITEM(list, index, value).

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial issue seems to show that PyList_SET_ITEM() result can be used, but also that it is used in practice:
https://bugs.python.org/issue30459#msg294362

So I would suggest to document in the C API changes, Porting to Python 3.9:
https://docs.python.org/dev/whatsnew/3.9.html#id2

@vstinner
Copy link
Member

vstinner commented May 7, 2020

By the way, I added Py_SET_REFCNT(), Py_SET_TYPE() and Py_SET_SIZE() in Python 3.9, but I left the related Py_REFCNT(), Py_TYPE() and Py_SIZE() unchanged. It remains possible to use them as l-value for example: Py_REFCNT(ob) = 1;.

I expect that few C extensions use Py_REFCNT(ob) = 1;, but maybe a few use Py_TYPE(op) = type;. For Py_SIZE(ob) = size;, Python 3.8 didn't provide any "abstract" setter function/macro, direct access to the member is also possible: ((PyVarObject*)ob)->ob_size = size;,

I chose to add new macros in Python 3.9, and maybe change the macros in Python 3.10. The change is part of a larger project:
https://github.com/vstinner/misc/blob/master/cpython/pep-opaque-c-api.rst

See bpo-39573.

@vstinner
Copy link
Member

vstinner commented Dec 4, 2020

No activity since May, I close the PR. I created PR #23645 which converts PyTuple_SET_ITEM() and PyList_SET_ITEM() to static inline functions.

@vstinner vstinner closed this Dec 4, 2020
@vstinner
Copy link
Member

vstinner commented Dec 5, 2020

Thanks @ZackerySpytz, I merged your PR. I wrote PR #23654 to modify also PyCell_SET().

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 this pull request may close these issues.

5 participants