-
-
Notifications
You must be signed in to change notification settings - Fork 11k
MNT: add linter for thread-unsafe C API uses #28634
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
base: main
Are you sure you want to change the base?
Conversation
62be0b4
to
b77b43a
Compare
Does this need a whole new CI job? I know I suggested that in the issue, but it occurs to me that this could be integrated into e.g. the Also rather than just checking diffs we could do this codebase-wide and add e.g. If it ends up being really invasive to add the lint opt-outs we can reconsider. If the lint fails, it should suggest what to do to fix the issue. One of:
or, add an opt-out to the lint if:
The linter should also probably check all of the functions listed in the free-threaded HOWTO in the CPython docs: https://docs.python.org/3/howto/free-threading-extensions.html#borrowed-references Also see here for more details about why we need to do this and why we can't just globally replace all these functions: https://py-free-threading.github.io/porting-extensions/#cpython-c-api-usage We probably should also be linting uses of fast accessor macros that don't do any locking. Also, FWIW, there are probably still lurking issues. I'm aware of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this goes in the right direction, but may need a bit of care. Overall, perhaps good to keep it simple, and maybe just use git grep
for all, like,
git grep -e PyList_GetItem\( --or -e PyList_GET_ITEM\( ... origin/main...HEAD
Or perhaps better write the list to a file and use -f
.
Notes:
- important to add the
(
otherwise correct functions likePyList_GetItemRef
will be matched too. - You have to be sure to match all C files, so also
.c.src
and.cpp
, etc. My suggestion would be to just match all files and perhaps remove.rst
... since this only runs on the differences, false positives are not very likely.
I almost wonder if one could do a custom clang-tidy style check (not if that can that work with Plus that means we have an established pattern for allowing it, just add a |
e69e629
to
3cf055d
Compare
This is looking much closer to being mergeable. Let me know if you need a hand with getting the circleci build fixed. |
.. or maybe it's unrelated and would go away with a rebase? |
bd4afe5
to
5e57f30
Compare
|
7811388
to
0ea4d9c
Compare
af12173
to
dccef23
Compare
7bc287b
to
3e90169
Compare
d155200
to
56b3d77
Compare
Let me know if you'd like a hand with some git surgery :) |
@ngoldbaum looks like all the tests are passing now.
ALL_FILES=$(find numpy -type f \( -name "*.c" -o -name "*.h" -o -name "*.c.src" -o -name "*.cpp" \) ! -path "*/pythoncapi-compat/*") Let me know if you need any additional changes. thanks for the help! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, I'm glad this is shaping up.
I guess my main issue with this is that we should be relatively sure that all of the ones we're marking as OK really are OK, otherwise we'll be misleading future readers.
I need to sit down with this PR and try to stare at all the uses and see if I can come up with possible problems.
We might need a new NOQA category for uses that are known to be problematic but need manual fixes. That would allow us to catch and triage new uses in CI while not needing to go through and fix absolutely everything that's still in the library.
I will try to do this soon but may not have a ton of time before PyCon.
@@ -108,7 +108,7 @@ PyUFuncOverride_GetOutObjects(PyObject *kwds, PyObject **out_kwd_obj, PyObject * | |||
* PySequence_Fast* functions. This is required for PyPy | |||
*/ | |||
PyObject *seq; | |||
seq = PySequence_Fast(*out_kwd_obj, | |||
seq = PySequence_Fast(*out_kwd_obj, // noqa: borrowed-ref OK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually have some changes I need to upstream related to this from back in February, see #28046 (comment). Maybe I should finally get around to sending in what I have...
Hi all, I'm really sorry for the delay here. Totally my fault for letting it sit for so long. I'll try to set aside some time to get this up to shape. |
Adding a linter to audit PRs.
This would lint out PRs that use problematic functions such as
PyList_GetItem
PyList_GET_ITEM
PyDict_GetItem
PyDict_getItemWithError
PyDict_Next
PyDict_GetItemString
_PyDict_GetItemStringWithError
Attempts to resolve #26159