Skip to content

Commit 727696d

Browse files
committed
Simplify borrowed-ref comments to focus on the why
1 parent 7e3c835 commit 727696d

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

Modules/_json.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,10 +1745,8 @@ _encoder_iterate_mapping_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
17451745
PyObject *key, *value;
17461746
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(items); i++) {
17471747
PyObject *item = PyList_GET_ITEM(items, i);
1748-
// gh-142831: item is a borrowed ref from the items list.
1749-
// encoder_encode_key_value() may invoke user Python code
1750-
// (the key encoder) that can mutate or clear the list,
1751-
// so we must hold a strong reference.
1748+
// gh-142831: encoder_encode_key_value() can invoke user code
1749+
// that mutates the items list, invalidating this borrowed ref.
17521750
Py_INCREF(item);
17531751
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
17541752
PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
@@ -1779,10 +1777,8 @@ _encoder_iterate_dict_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
17791777
PyObject *key, *value;
17801778
Py_ssize_t pos = 0;
17811779
while (PyDict_Next(dct, &pos, &key, &value)) {
1782-
// gh-119438, gh-145244: key and value are borrowed refs from
1783-
// PyDict_Next(). encoder_encode_key_value() may invoke user
1784-
// Python code (the 'default' callback) that can mutate or
1785-
// clear the dict, so we must hold strong references.
1780+
// gh-145244: encoder_encode_key_value() can invoke user code
1781+
// that mutates the dict, invalidating these borrowed refs.
17861782
Py_INCREF(key);
17871783
Py_INCREF(value);
17881784
if (encoder_encode_key_value(s, writer, first, dct, key, value,
@@ -1897,10 +1893,8 @@ _encoder_iterate_fast_seq_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
18971893
{
18981894
for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
18991895
PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
1900-
// gh-142831: obj is a borrowed ref from the sequence.
1901-
// encoder_listencode_obj() may invoke user Python code
1902-
// (the 'default' callback) that can mutate or clear the
1903-
// sequence, so we must hold a strong reference.
1896+
// gh-142831: encoder_listencode_obj() can invoke user code
1897+
// that mutates the sequence, invalidating this borrowed ref.
19041898
Py_INCREF(obj);
19051899
if (i) {
19061900
if (PyUnicodeWriter_WriteStr(writer, separator) < 0) {

0 commit comments

Comments
 (0)