Skip to content

Commit

Permalink
shallow copy the notes in split(), if it's a sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Mar 22, 2022
1 parent f240e71 commit 6786dbd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,26 @@ def exc(ex):
self.assertMatchesTemplate(
rest, ExceptionGroup, [ValueError(1)])

def test_split_copies_notes(self):
# make sure each exception group after a split has its own __notes__ list
eg = ExceptionGroup("eg", [ValueError(1), TypeError(2)])
eg.add_note("note1")
eg.add_note("note2")
orig_notes = list(eg.__notes__)
match, rest = eg.split(TypeError)
self.assertEqual(eg.__notes__, orig_notes)
self.assertEqual(match.__notes__, orig_notes)
self.assertEqual(rest.__notes__, orig_notes)
self.assertIsNot(eg.__notes__, match.__notes__)
self.assertIsNot(eg.__notes__, rest.__notes__)
self.assertIsNot(match.__notes__, rest.__notes__)
eg.add_note("eg")
match.add_note("match")
rest.add_note("rest")
self.assertEqual(eg.__notes__, orig_notes + ["eg"])
self.assertEqual(match.__notes__, orig_notes + ["match"])
self.assertEqual(rest.__notes__, orig_notes + ["rest"])


class NestedExceptionGroupSubclassSplitTest(ExceptionGroupSplitTestBase):

Expand Down
10 changes: 10 additions & 0 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,16 @@ exceptiongroup_subset(
if (notes == NULL) {
goto error;
}
if (PySequence_Check(notes)) {
/* make a copy so the parts have independent notes.
* If __notes__ is not a sequence, we don't know how to copy it */
PyObject *notes_copy = PySequence_List(notes);
Py_DECREF(notes);
if (notes_copy == NULL) {
goto error;
}
notes = notes_copy;
}
if (PyObject_SetAttr(eg, &_Py_ID(__notes__), notes) < 0) {
Py_DECREF(notes);
goto error;
Expand Down

0 comments on commit 6786dbd

Please sign in to comment.