Skip to content

Commit 87699c0

Browse files
committed
mnt: add label for thread unsafe uses that need manual fixes
1 parent c3f2ac8 commit 87699c0

File tree

9 files changed

+30
-27
lines changed

9 files changed

+30
-27
lines changed

numpy/_core/src/common/ufunc_override.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ PyUFuncOverride_GetOutObjects(PyObject *kwds, PyObject **out_kwd_obj, PyObject *
9393
*out_kwd_obj = NULL;
9494
return -1;
9595
}
96-
int result = PyDict_GetItemStringRef(kwds, "out", out_kwd_obj); // noqa: borrowed-ref OK
96+
int result = PyDict_GetItemStringRef(kwds, "out", out_kwd_obj);
9797
if (result == -1) {
9898
return -1;
9999
}

numpy/_core/src/multiarray/_multiarray_tests.c.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ incref_elide_l(PyObject *dummy, PyObject *args)
644644
}
645645
/* get item without increasing refcount, item may still be on the python
646646
* stack but above the inaccessible top */
647-
r = PyList_GetItem(arg, 4); // noqa: borrowed-ref OK
647+
r = PyList_GetItem(arg, 4); // noqa: borrowed-ref - manual fix needed
648648
res = PyNumber_Add(r, r);
649649

650650
return res;
@@ -883,7 +883,7 @@ get_all_cast_information(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args))
883883
PyObject *to_dtype, *cast_obj;
884884
Py_ssize_t pos = 0;
885885

886-
while (PyDict_Next(NPY_DT_SLOTS(from_dtype)->castingimpls, // noqa: borrowed-ref OK
886+
while (PyDict_Next(NPY_DT_SLOTS(from_dtype)->castingimpls, // noqa: borrowed-ref - manual fix needed
887887
&pos, &to_dtype, &cast_obj)) {
888888
if (cast_obj == Py_None) {
889889
continue;

numpy/_core/src/multiarray/arraytypes.c.src

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ VOID_getitem(void *input, void *vap)
881881
npy_intp offset;
882882
PyArray_Descr *new;
883883
key = PyTuple_GET_ITEM(names, i);
884-
tup = PyDict_GetItem(descr->fields, key); // noqa: borrowed-ref OK
884+
tup = PyDict_GetItem(descr->fields, key); // noqa: borrowed-ref - manual fix needed
885885
if (_unpack_field(tup, &new, &offset) < 0) {
886886
Py_DECREF(ret);
887887
return NULL;
@@ -973,7 +973,7 @@ _setup_field(int i, _PyArray_LegacyDescr *descr, PyArrayObject *arr,
973973
npy_intp offset;
974974

975975
key = PyTuple_GET_ITEM(descr->names, i);
976-
tup = PyDict_GetItem(descr->fields, key); // noqa: borrowed-ref OK
976+
tup = PyDict_GetItem(descr->fields, key); // noqa: borrowed-ref - manual fix needed
977977
if (_unpack_field(tup, &new, &offset) < 0) {
978978
return -1;
979979
}
@@ -2274,7 +2274,7 @@ VOID_copyswapn (char *dst, npy_intp dstride, char *src, npy_intp sstride,
22742274
PyArrayObject_fields dummy_fields = get_dummy_stack_array(arr);
22752275
PyArrayObject *dummy_arr = (PyArrayObject *)&dummy_fields;
22762276

2277-
while (PyDict_Next(descr->fields, &pos, &key, &value)) { // noqa: borrowed-ref OK
2277+
while (PyDict_Next(descr->fields, &pos, &key, &value)) { // noqa: borrowed-ref - manual fix needed
22782278
npy_intp offset;
22792279
PyArray_Descr *new;
22802280
if (NPY_TITLE_KEY(key, value)) {
@@ -2359,7 +2359,7 @@ VOID_copyswap (char *dst, char *src, int swap, PyArrayObject *arr)
23592359
PyArrayObject_fields dummy_fields = get_dummy_stack_array(arr);
23602360
PyArrayObject *dummy_arr = (PyArrayObject *)&dummy_fields;
23612361

2362-
while (PyDict_Next(descr->fields, &pos, &key, &value)) { // noqa: borrowed-ref OK
2362+
while (PyDict_Next(descr->fields, &pos, &key, &value)) { // noqa: borrowed-ref - manual fix needed
23632363
npy_intp offset;
23642364

23652365
PyArray_Descr * new;
@@ -2679,7 +2679,7 @@ VOID_nonzero (char *ip, PyArrayObject *ap)
26792679
PyArrayObject *dummy_arr = (PyArrayObject *)&dummy_fields;
26802680

26812681
_PyArray_LegacyDescr *descr = (_PyArray_LegacyDescr *)PyArray_DESCR(ap);
2682-
while (PyDict_Next(descr->fields, &pos, &key, &value)) { // noqa: borrowed-ref OK
2682+
while (PyDict_Next(descr->fields, &pos, &key, &value)) { // noqa: borrowed-ref - manual fix needed
26832683
PyArray_Descr * new;
26842684
npy_intp offset;
26852685
if (NPY_TITLE_KEY(key, value)) {
@@ -3041,7 +3041,7 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap)
30413041
PyArray_Descr *new;
30423042
npy_intp offset;
30433043
key = PyTuple_GET_ITEM(names, i);
3044-
tup = PyDict_GetItem(PyDataType_FIELDS(descr), key); // noqa: borrowed-ref OK
3044+
tup = PyDict_GetItem(PyDataType_FIELDS(descr), key); // noqa: borrowed-ref - manual fix needed
30453045
if (_unpack_field(tup, &new, &offset) < 0) {
30463046
goto finish;
30473047
}

numpy/_core/src/multiarray/buffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str,
268268
int ret;
269269

270270
name = PyTuple_GET_ITEM(ldescr->names, k);
271-
item = PyDict_GetItem(ldescr->fields, name); // noqa: borrowed-ref OK
271+
item = PyDict_GetItem(ldescr->fields, name); // noqa: borrowed-ref - manual fix needed
272272

273273
child = (PyArray_Descr*)PyTuple_GetItem(item, 0);
274274
offset_obj = PyTuple_GetItem(item, 1);

numpy/_core/src/multiarray/compiled_base.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *const *args, Py_ssize_t
15221522
PyTypeObject *new = (PyTypeObject *)obj;
15231523
_ADDDOC(new->tp_doc, new->tp_name);
15241524
if (new->tp_dict != NULL && PyDict_CheckExact(new->tp_dict) &&
1525-
PyDict_GetItemString(new->tp_dict, "__doc__") == Py_None) { // noqa: borrowed-ref OK
1525+
PyDict_GetItemString(new->tp_dict, "__doc__") == Py_None) { // noqa: borrowed-ref - manual fix needed
15261526
/* Warning: Modifying `tp_dict` is not generally safe! */
15271527
if (PyDict_SetItemString(new->tp_dict, "__doc__", str) < 0) {
15281528
return NULL;

numpy/_core/src/multiarray/convert_datatype.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ PyArray_GetCastFunc(PyArray_Descr *descr, int type_num)
344344
PyObject *cobj;
345345

346346
key = PyLong_FromLong(type_num);
347-
cobj = PyDict_GetItem(obj, key); // noqa: borrowed-ref OK
347+
cobj = PyDict_GetItem(obj, key); // noqa: borrowed-ref - manual fix needed
348348
Py_DECREF(key);
349349
if (cobj && PyCapsule_CheckExact(cobj)) {
350350
castfunc = PyCapsule_GetPointer(cobj, NULL);
@@ -2749,7 +2749,7 @@ nonstructured_to_structured_resolve_descriptors(
27492749

27502750
Py_ssize_t pos = 0;
27512751
PyObject *key, *tuple;
2752-
while (PyDict_Next(to_descr->fields, &pos, &key, &tuple)) { // noqa: borrowed-ref OK
2752+
while (PyDict_Next(to_descr->fields, &pos, &key, &tuple)) { // noqa: borrowed-ref - manual fix needed
27532753
PyArray_Descr *field_descr = (PyArray_Descr *)PyTuple_GET_ITEM(tuple, 0);
27542754
npy_intp field_view_off = NPY_MIN_INTP;
27552755
NPY_CASTING field_casting = PyArray_GetCastInfo(
@@ -2898,7 +2898,7 @@ structured_to_nonstructured_resolve_descriptors(
28982898
return -1;
28992899
}
29002900
PyObject *key = PyTuple_GetItem(PyDataType_NAMES(given_descrs[0]), 0);
2901-
PyObject *base_tup = PyDict_GetItem(PyDataType_FIELDS(given_descrs[0]), key); // noqa: borrowed-ref OK
2901+
PyObject *base_tup = PyDict_GetItem(PyDataType_FIELDS(given_descrs[0]), key); // noqa: borrowed-ref - manual fix needed
29022902
base_descr = (PyArray_Descr *)PyTuple_GET_ITEM(base_tup, 0);
29032903
struct_view_offset = PyLong_AsSsize_t(PyTuple_GET_ITEM(base_tup, 1));
29042904
if (error_converting(struct_view_offset)) {
@@ -3033,15 +3033,15 @@ can_cast_fields_safety(
30333033
for (Py_ssize_t i = 0; i < field_count; i++) {
30343034
npy_intp field_view_off = NPY_MIN_INTP;
30353035
PyObject *from_key = PyTuple_GET_ITEM(PyDataType_NAMES(from), i);
3036-
PyObject *from_tup = PyDict_GetItemWithError(PyDataType_FIELDS(from), from_key); // noqa: borrowed-ref OK
3036+
PyObject *from_tup = PyDict_GetItemWithError(PyDataType_FIELDS(from), from_key); // noqa: borrowed-ref - manual fix needed
30373037
if (from_tup == NULL) {
30383038
return give_bad_field_error(from_key);
30393039
}
30403040
PyArray_Descr *from_base = (PyArray_Descr *) PyTuple_GET_ITEM(from_tup, 0);
30413041

30423042
/* Check whether the field names match */
30433043
PyObject *to_key = PyTuple_GET_ITEM(PyDataType_NAMES(to), i);
3044-
PyObject *to_tup = PyDict_GetItem(PyDataType_FIELDS(to), to_key); // noqa: borrowed-ref OK
3044+
PyObject *to_tup = PyDict_GetItem(PyDataType_FIELDS(to), to_key); // noqa: borrowed-ref - manual fix needed
30453045
if (to_tup == NULL) {
30463046
return give_bad_field_error(from_key);
30473047
}

numpy/_core/src/multiarray/ctors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ _is_default_descr(PyObject *descr, PyObject *typestr) {
21142114
if (!PyList_Check(descr) || PyList_GET_SIZE(descr) != 1) {
21152115
return 0;
21162116
}
2117-
PyObject *tuple = PyList_GET_ITEM(descr, 0); // noqa: borrowed-ref OK
2117+
PyObject *tuple = PyList_GET_ITEM(descr, 0); // noqa: borrowed-ref - manual fix needed
21182118
if (!(PyTuple_Check(tuple) && PyTuple_GET_SIZE(tuple) == 2)) {
21192119
return 0;
21202120
}

numpy/_core/src/multiarray/descriptor.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ _convert_from_array_descr(PyObject *obj, int align)
424424
return NULL;
425425
}
426426
for (int i = 0; i < n; i++) {
427-
PyObject *item = PyList_GET_ITEM(obj, i); // noqa: borrowed-ref OK
427+
PyObject *item = PyList_GET_ITEM(obj, i); // noqa: borrowed-ref - manual fix needed
428428
if (!PyTuple_Check(item) || (PyTuple_GET_SIZE(item) < 2)) {
429429
PyErr_Format(PyExc_TypeError,
430430
"Field elements must be 2- or 3-tuples, got '%R'",
@@ -613,7 +613,7 @@ _convert_from_list(PyObject *obj, int align)
613613
* Ignore any empty string at end which _internal._commastring
614614
* can produce
615615
*/
616-
PyObject *last_item = PyList_GET_ITEM(obj, n-1); // noqa: borrowed-ref OK
616+
PyObject *last_item = PyList_GET_ITEM(obj, n-1); // noqa: borrowed-ref - manual fix needed
617617
if (PyUnicode_Check(last_item)) {
618618
Py_ssize_t s = PySequence_Size(last_item);
619619
if (s < 0) {
@@ -643,7 +643,7 @@ _convert_from_list(PyObject *obj, int align)
643643
int totalsize = 0;
644644
for (int i = 0; i < n; i++) {
645645
PyArray_Descr *conv = _convert_from_any(
646-
PyList_GET_ITEM(obj, i), align); // noqa: borrowed-ref OK
646+
PyList_GET_ITEM(obj, i), align); // noqa: borrowed-ref - manual fix needed
647647
if (conv == NULL) {
648648
goto fail;
649649
}
@@ -794,7 +794,7 @@ _validate_union_object_dtype(_PyArray_LegacyDescr *new, _PyArray_LegacyDescr *co
794794
if (name == NULL) {
795795
return -1;
796796
}
797-
tup = PyDict_GetItemWithError(conv->fields, name); // noqa: borrowed-ref OK
797+
tup = PyDict_GetItemWithError(conv->fields, name); // noqa: borrowed-ref - manual fix needed
798798
if (tup == NULL) {
799799
if (!PyErr_Occurred()) {
800800
/* fields was missing the name it claimed to contain */
@@ -940,7 +940,7 @@ _validate_object_field_overlap(_PyArray_LegacyDescr *dtype)
940940
if (key == NULL) {
941941
return -1;
942942
}
943-
tup = PyDict_GetItemWithError(fields, key); // noqa: borrowed-ref OK
943+
tup = PyDict_GetItemWithError(fields, key); // noqa: borrowed-ref - manual fix needed
944944
if (tup == NULL) {
945945
if (!PyErr_Occurred()) {
946946
/* fields was missing the name it claimed to contain */
@@ -960,7 +960,7 @@ _validate_object_field_overlap(_PyArray_LegacyDescr *dtype)
960960
if (key == NULL) {
961961
return -1;
962962
}
963-
tup = PyDict_GetItemWithError(fields, key); // noqa: borrowed-ref OK
963+
tup = PyDict_GetItemWithError(fields, key); // noqa: borrowed-ref - manual fix needed
964964
if (tup == NULL) {
965965
if (!PyErr_Occurred()) {
966966
/* fields was missing the name it claimed to contain */

tools/ci/check_c_api_usage.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ set -e
44
# List of suspicious function calls:
55
SUSPICIOUS_FUNCS=(
66
"PyList_GetItem"
7-
"PyList_GET_ITEM"
87
"PyDict_GetItem"
98
"PyDict_GetItemWithError"
10-
"PyDict_Next"
119
"PyDict_GetItemString"
10+
"PyDict_SetDefault"
11+
"PyDict_Next"
12+
"PyWeakref_GetObject"
13+
"PyWeakref_GET_OBJECT"
14+
"PyList_GET_ITEM"
1215
"_PyDict_GetItemStringWithError"
1316
"PySequence_Fast"
1417
)
@@ -44,7 +47,7 @@ for file in $ALL_FILES; do
4447
code_line=$(cut -d: -f2- <<< "$line")
4548

4649
# Skip if line contains noqa
47-
if [[ "$code_line" == *"noqa: borrowed-ref OK"* ]]; then
50+
if [[ "$code_line" == *"noqa: borrowed-ref OK"* || "$code_line" == *"noqa: borrowed-ref - manual fix needed"* ]]; then
4851
continue
4952
fi
5053

@@ -81,7 +84,7 @@ done
8184
if [[ $FAIL -eq 1 ]]; then
8285
echo "C API borrow-ref linter found issues."
8386
else
84-
echo "C API borrow-ref linter found no issues."
87+
echo "C API borrow-ref linter found no issues." > $OUTPUT
8588
fi
8689

8790
cat "$OUTPUT"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy