Skip to content

Commit e70a1e8

Browse files
committed
Shorten comments in join.h and the regression test
1 parent c424703 commit e70a1e8

2 files changed

Lines changed: 6 additions & 13 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,7 @@ def test_join_reentrant_buffer_mutation(self):
650650
# to that item by mutating the joined sequence.
651651
# See: https://github.com/python/cpython/issues/151295
652652
def make_seq(mutate):
653-
# The mutating item is only referenced from the list slot, so
654-
# mutate() drops its last reference mid-join.
653+
# Item is only referenced from the list slot, so mutate() frees it.
655654
class Item:
656655
def __buffer__(self, flags):
657656
mutate(seq)
@@ -665,15 +664,12 @@ def __buffer__(self, flags):
665664

666665
for sep in (self.type2test(b''), self.type2test(b'::')):
667666
with self.subTest(sep=sep):
668-
# Clearing the list changes its length, which is reported as a
669-
# RuntimeError.
667+
# Changing the list length is reported as a RuntimeError.
670668
seq = make_seq(lambda seq: seq.clear())
671669
self.assertRaises(RuntimeError, sep.join, seq)
672670

673-
# Replacing the item in place keeps the list length unchanged,
674-
# so the size-change recheck cannot fire; only keeping the item
675-
# alive across __buffer__() prevents the use-after-free, and
676-
# the join uses the buffer returned by __buffer__().
671+
# The list length is unchanged, so the size-change recheck
672+
# cannot fire: only keeping the item alive avoids the crash.
677673
def replace(seq):
678674
seq[1] = b'z'
679675
seq = make_seq(replace)

Objects/stringlib/join.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
6868
buffers[i].len = PyBytes_GET_SIZE(item);
6969
}
7070
else {
71-
/* Keep item alive across PyObject_GetBuffer(): item is only
72-
borrowed from the sequence, and its __buffer__() may run
73-
Python that drops that last reference (e.g. by mutating the
74-
sequence being joined), freeing item while the buffer
75-
machinery is still using it. */
71+
/* item is only borrowed; its __buffer__() may run Python that
72+
drops the sequence's last reference to it. */
7673
Py_INCREF(item);
7774
if (PyObject_GetBuffer(item, &buffers[i], PyBUF_SIMPLE) != 0) {
7875
Py_DECREF(item);

0 commit comments

Comments
 (0)