@@ -25,6 +25,7 @@ use crate::{
2525 lock:: { PyMutex , PyMutexGuard , PyRwLock } ,
2626 refcount:: RefCount ,
2727 } ,
28+ function:: PySetterValue ,
2829 vm:: VirtualMachine ,
2930} ;
3031use itertools:: Itertools ;
@@ -711,14 +712,37 @@ impl PyObject {
711712
712713 /// Set the dict field. Returns `Err(dict)` if this object does not have a dict field
713714 /// in the first place.
714- pub fn set_dict ( & self , dict : PyDictRef ) -> Result < ( ) , PyDictRef > {
715- match self . instance_dict ( ) {
716- Some ( d) => {
715+ pub fn set_dict ( & self , dict : PySetterValue < PyDictRef > ) -> Option < ( ) > {
716+ // NOTE(hanif) - So far, this is the only error condition that I know of so we can use Option
717+ // for now.
718+ if self . payload_is :: < crate :: builtins:: function:: PyFunction > ( ) {
719+ return None ;
720+ }
721+
722+ match ( self . instance_dict ( ) , dict) {
723+ ( Some ( d) , PySetterValue :: Assign ( dict) ) => {
717724 d. set ( dict) ;
718- Ok ( ( ) )
719725 }
720- None => Err ( dict) ,
721- }
726+ ( None , PySetterValue :: Assign ( dict) ) => {
727+ // self.0.dict = Some(InstanceDict::new(dict));
728+ unsafe {
729+ let ptr = self as * const _ as * mut PyObject ;
730+ ( * ptr) . 0 . dict = Some ( InstanceDict :: new ( dict) ) ;
731+ }
732+ }
733+ ( Some ( _) , PySetterValue :: Delete ) => {
734+ // self.0.dict = None;
735+ unsafe {
736+ let ptr = self as * const _ as * mut PyObject ;
737+ ( * ptr) . 0 . dict = None ;
738+ }
739+ }
740+ ( None , PySetterValue :: Delete ) => {
741+ // NOTE(hanif) - noop?
742+ }
743+ } ;
744+
745+ Some ( ( ) )
722746 }
723747
724748 #[ inline( always) ]
0 commit comments