@@ -4,7 +4,7 @@ use crate::object::{Traverse, TraverseFn};
44use crate :: {
55 AsObject , Context , Py , PyObjectRef , PyPayload , PyRef , PyResult , TryFromObject , VirtualMachine ,
66 builtins:: {
7- PyNone , PyStr , PyStrRef , PyTuple , PyTupleRef , PyType , PyTypeRef ,
7+ PyList , PyNone , PyStr , PyStrRef , PyTuple , PyTupleRef , PyType , PyTypeRef ,
88 traceback:: { PyTraceback , PyTracebackRef } ,
99 } ,
1010 class:: { PyClassImpl , StaticType } ,
@@ -652,6 +652,29 @@ impl PyRef<PyBaseException> {
652652 Ok ( self )
653653 }
654654
655+ #[ pymethod]
656+ fn add_note ( self , note : PyStrRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
657+ let dict = self
658+ . as_object ( )
659+ . dict ( )
660+ . ok_or_else ( || vm. new_attribute_error ( "Exception object has no __dict__" ) ) ?;
661+
662+ let notes = if let Ok ( notes) = dict. get_item ( "__notes__" , vm) {
663+ notes
664+ } else {
665+ let new_notes = vm. ctx . new_list ( vec ! [ ] ) ;
666+ dict. set_item ( "__notes__" , new_notes. clone ( ) . into ( ) , vm) ?;
667+ new_notes. into ( )
668+ } ;
669+
670+ let notes = notes
671+ . downcast :: < PyList > ( )
672+ . map_err ( |_| vm. new_type_error ( "__notes__ must be a list" ) ) ?;
673+
674+ notes. borrow_vec_mut ( ) . push ( note. into ( ) ) ;
675+ Ok ( ( ) )
676+ }
677+
655678 #[ pymethod]
656679 fn __reduce__ ( self , vm : & VirtualMachine ) -> PyTupleRef {
657680 if let Some ( dict) = self . as_object ( ) . dict ( ) . filter ( |x| !x. is_empty ( ) ) {
0 commit comments