10000 shallow copy the notes in split(), if it's a sequence · python/cpython@6786dbd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6786dbd

Browse files
committed
shallow copy the notes in split(), if it's a sequence
1 parent f240e71 commit 6786dbd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Lib/test/test_exception_group.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,26 @@ def exc(ex):
665665
self.assertMatchesTemplate(
666666
rest, ExceptionGroup, [ValueError(1)])
667667

668+
def test_split_copies_notes(self):
669+
# make sure each exception group after a split has its own __notes__ list
670+
eg = ExceptionGroup("eg", [ValueError(1), TypeError(2)])
671+
eg.add_note("note1")
672+
eg.add_note("note2")
673+
orig_notes = list(eg.__notes__)
674+
match, rest = eg.split(TypeError)
675+
self.assertEqual(eg.__notes__, orig_notes)
676+
self.assertEqual(match.__notes__, orig_notes)
677+
self.assertEqual(rest.__notes__, orig_notes)
678+
self.assertIsNot(eg.__notes__, match.__notes__)
679+
self.assertIsNot(eg.__notes__, rest.__notes__)
680+
self.assertIsNot(match.__notes__, rest.__notes__)
681+
eg.add_note("eg")
682+
match.add_note("match")
683+
rest.add_note("rest")
684+
self.assertEqual(eg.__notes__, orig_notes + ["eg"])
685+
self.assertEqual(match.__notes__, orig_notes + ["match"])
686+
self.assertEqual(rest.__notes__, orig_notes + ["rest"])
687+
668688

669689
class NestedExceptionGroupSubclassSplitTest(ExceptionGroupSplitTestBase):
670690

Objects/exceptions.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,16 @@ exceptiongroup_subset(
923923
if (notes == NULL) {
924924
goto error;
925925
}
926+
if (PySequence_Check(notes)) {
927+
/* make a copy so the parts have independent notes.
928+
* If __notes__ is not a sequence, we don't know how to copy it */
929+
PyObject *notes_copy = PySequence_List(notes);
930+
Py_DECREF(notes);
931+
if (notes_copy == NULL) {
932+
goto error;
933+
}
934+
notes = notes_copy;
935+
}
926936
if (PyObject_SetAttr(eg, &_Py_ID(__notes__), notes) < 0) {
927937
Py_DECREF(notes);
928938
goto error;

0 commit comments

Comments
 (0)
0