mirror of
https://github.com/python/cpython.git
synced 2025-11-11 02:52:04 +00:00
gh-130914: Make graphlib.TopologicalSorter.prepare() idempotent (#131317)
Closes #130914: Make graphlib.TopologicalSorter.prepare() idempotent Relax the rules so that `.prepare()` can be called multiple times, provided that no work has been passed out by `.get_ready()` yet.
This commit is contained in:
parent
f819900245
commit
c1b42db9e4
6 changed files with 44 additions and 7 deletions
|
|
@ -140,9 +140,21 @@ def test_calls_before_prepare(self):
|
|||
def test_prepare_multiple_times(self):
|
||||
ts = graphlib.TopologicalSorter()
|
||||
ts.prepare()
|
||||
with self.assertRaisesRegex(ValueError, r"cannot prepare\(\) more than once"):
|
||||
ts.prepare()
|
||||
|
||||
def test_prepare_after_pass_out(self):
|
||||
ts = graphlib.TopologicalSorter({'a': 'bc'})
|
||||
ts.prepare()
|
||||
self.assertEqual(set(ts.get_ready()), {'b', 'c'})
|
||||
with self.assertRaisesRegex(ValueError, r"cannot prepare\(\) after starting sort"):
|
||||
ts.prepare()
|
||||
|
||||
def test_prepare_cycleerror_each_time(self):
|
||||
ts = graphlib.TopologicalSorter({'a': 'b', 'b': 'a'})
|
||||
for attempt in range(1, 4):
|
||||
with self.assertRaises(graphlib.CycleError, msg=f"{attempt=}"):
|
||||
ts.prepare()
|
||||
|
||||
def test_invalid_nodes_in_done(self):
|
||||
ts = graphlib.TopologicalSorter()
|
||||
ts.add(1, 2, 3, 4)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue