diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 99426220154360..6662b21d0775bd 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -922,9 +922,11 @@ def _generate_toc(self): starts, stops = [], [] last_was_empty = False self._file.seek(0) + next_pos = 0 while True: - line_pos = self._file.tell() + line_pos = next_pos line = self._file.readline() + next_pos += len(line) if line.startswith(b'From '): if len(stops) < len(starts): if last_was_empty: @@ -972,17 +974,18 @@ def _generate_toc(self): starts, stops = [], [] self._file.seek(0) next_pos = 0 + sep = b'\001\001\001\001' + linesep while True: line_pos = next_pos line = self._file.readline() - next_pos = self._file.tell() - if line.startswith(b'\001\001\001\001' + linesep): + next_pos += len(line) + if line.startswith(sep): starts.append(next_pos) while True: line_pos = next_pos line = self._file.readline() - next_pos = self._file.tell() - if line == b'\001\001\001\001' + linesep: + next_pos += len(line) + if line == sep: stops.append(line_pos - len(linesep)) break elif not line: @@ -1418,19 +1421,23 @@ def _generate_toc(self): self._file.seek(0) next_pos = 0 label_lists = [] + msg_sep = b'\037\014' + linesep + end_sep = b'\037' + linesep while True: line_pos = next_pos line = self._file.readline() - next_pos = self._file.tell() - if line == b'\037\014' + linesep: + next_pos += len(line) + if line == msg_sep: if len(stops) < len(starts): stops.append(line_pos - len(linesep)) starts.append(next_pos) + label_line = self._file.readline() + next_pos += len(label_line) labels = [label.strip() for label - in self._file.readline()[1:].split(b',') + in label_line[1:].split(b',') if label.strip()] label_lists.append(labels) - elif line == b'\037' or line == b'\037' + linesep: + elif line == b'\037' or line == end_sep: if len(stops) < len(starts): stops.append(line_pos - len(linesep)) elif not line: diff --git a/Misc/NEWS.d/next/Library/2026-09-15-12-00-00.gh-issue-157509.mbXtoc.rst b/Misc/NEWS.d/next/Library/2026-09-15-12-00-00.gh-issue-157509.mbXtoc.rst new file mode 100644 index 00000000000000..0c89691bebf44a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-15-12-00-00.gh-issue-157509.mbXtoc.rst @@ -0,0 +1,2 @@ +Speed up reading :class:`mailbox.mbox`, :class:`mailbox.MMDF` and +:class:`mailbox.Babyl` mailboxes.