Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Lib/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,14 +1236,15 @@ def get_sequences(self):
keys = set()
for spec in contents.split():
if spec.isdigit():
keys.add(int(spec))
key = int(spec)
if key in all_keys:
keys.add(key)
else:
start, stop = (int(x) for x in spec.split('-'))
keys.update(range(start, stop + 1))
results[name] = [key for key in sorted(keys) \
if key in all_keys]
if len(results[name]) == 0:
del results[name]
if start <= stop:
keys.update(k for k in all_keys if start <= k <= stop)
Comment on lines +1244 to +1245

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if start <= stop:
keys.update(k for k in all_keys if start <= k <= stop)
lo = bisect.bisect_left(sorted_keys, start)
hi = bisect.bisect_right(sorted_keys, stop)
keys.update(sorted_keys[lo:hi])

with sorted_keys set to sorted_keys = self.keys(). This avoids many start <= k <= stop checks.

if keys:
results[name] = sorted(keys)
except ValueError:
raise FormatError('Invalid sequence specification: %s' %
line.rstrip())
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,15 @@ def test_sequences(self):
self._box.set_sequences({'foo':[key0]})
self.assertEqual(self._box.get_sequences(), {'foo':[key0]})

def test_large_sequence_range(self):
# gh-156379: Verify large sequence ranges do not materialize large intervals
msg0 = mailbox.MHMessage(self._template % 0)
key0 = self._box.add(msg0)
seq_path = os.path.join(self._path, '.mh_sequences')
with open(seq_path, 'w', encoding='ASCII') as f:
f.write(f'unseen: 1-10000000\nempty: 50-100\n')
self.assertEqual(self._box.get_sequences(), {'unseen': [key0]})

def test_no_dot_mh_sequences_file(self):
path = os.path.join(self._path, 'foo.bar')
os.mkdir(path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid materializing large sequence ranges in :meth:`mailbox.MH.get_sequences`.
Loading