@@ -339,17 +339,18 @@ impl Encoder {
339339 self . write_u8 ( b'\n' ) ;
340340 }
341341 PickleValue :: Instance ( inst) => {
342- let InstanceData { module, name, state, dict_items, list_items } = inst. as_ref ( ) ;
342+ let InstanceData { module, name, state, dict_items, list_items, newobj } = inst. as_ref ( ) ;
343343 if module. is_empty ( ) && name. is_empty ( ) {
344344 return self . encode_anonymous_instance ( state, depth) ;
345345 }
346- // Emit as: GLOBAL module\nname\n args NEWOBJ [items] state BUILD, where args is
347- // EMPTY_TUPLE unless the state carries constructor args (@args/@state, #12).
346+ // Emit as: GLOBAL module\nname\n args NEWOBJ|REDUCE [items] state BUILD, where args
347+ // is EMPTY_TUPLE unless the state carries constructor args (@args/@state, #12)
348+ // and the opcode follows the construction kind (#32).
348349 let ( args, state) = match newobj_args_state ( state) {
349350 Some ( ( args, inner) ) => ( Some ( args) , inner) ,
350351 None => ( None , state. as_ref ( ) ) ,
351352 } ;
352- self . buf . reserve ( 5 + module. len ( ) + name. len ( ) ) ; // GLOBAL+mod+\n+name+\n+EMPTY_TUPLE+NEWOBJ
353+ self . buf . reserve ( 5 + module. len ( ) + name. len ( ) ) ; // GLOBAL+mod+\n+name+\n+EMPTY_TUPLE+opcode
353354 self . write_u8 ( GLOBAL ) ;
354355 self . write_bytes ( module. as_bytes ( ) ) ;
355356 self . write_u8 ( b'\n' ) ;
@@ -359,7 +360,7 @@ impl Encoder {
359360 Some ( args) => self . encode_value ( args, depth + 1 ) ?,
360361 None => self . write_u8 ( EMPTY_TUPLE ) ,
361362 }
362- self . write_u8 ( NEWOBJ ) ;
363+ self . write_u8 ( if * newobj { NEWOBJ } else { REDUCE } ) ;
363364 // Dict/list subclass items go before the state, in the order CPython's
364365 // save_reduce writes them: APPENDS, then SETITEMS, then BUILD.
365366 if let Some ( items) = list_items {
@@ -586,6 +587,7 @@ mod tests {
586587 ) ] ) ) ,
587588 dict_items : Some ( Box :: new ( vec ! [ ( PickleValue :: String ( "k" . into( ) ) , PickleValue :: Int ( 2 ) ) ] ) ) ,
588589 list_items : None ,
590+ newobj : true ,
589591 } ) ) ;
590592 let bytes = encode_pickle ( & val) . unwrap ( ) ;
591593 // CPython's save_reduce: NEWOBJ, then the items (MARK ... SETITEMS), then state + BUILD.
@@ -609,6 +611,7 @@ mod tests {
609611 state : Box :: new ( PickleValue :: Dict ( vec ! [ ] ) ) ,
610612 dict_items : Some ( Box :: new ( vec ! [ ( PickleValue :: Int ( 1 ) , PickleValue :: Int ( 2 ) ) ] ) ) ,
611613 list_items : Some ( Box :: new ( vec ! [ PickleValue :: Int ( 3 ) ] ) ) ,
614+ newobj : true ,
612615 } ) ) ;
613616 let bytes = encode_pickle ( & val) . unwrap ( ) ;
614617 let newobj = bytes. iter ( ) . position ( |& b| b == NEWOBJ ) . unwrap ( ) ;
@@ -620,6 +623,36 @@ mod tests {
620623 assert_eq ! ( decode_pickle( & bytes) . unwrap( ) , val) ;
621624 }
622625
626+ #[ test]
627+ fn test_reduce_kind_instance_emits_reduce ( ) {
628+ let val = PickleValue :: Instance ( Box :: new ( InstanceData {
629+ module : "m" . into ( ) ,
630+ name : "C" . into ( ) ,
631+ state : Box :: new ( PickleValue :: Dict ( vec ! [ ] ) ) ,
632+ dict_items : None ,
633+ list_items : None ,
634+ newobj : false ,
635+ } ) ) ;
636+ let bytes = encode_pickle ( & val) . unwrap ( ) ;
637+ assert ! ( bytes. contains( & REDUCE ) && !bytes. contains( & NEWOBJ ) ) ;
638+ // with the @args/@state shape: GLOBAL args REDUCE state BUILD
639+ let val = PickleValue :: Instance ( Box :: new ( InstanceData {
640+ module : "m" . into ( ) ,
641+ name : "C" . into ( ) ,
642+ state : Box :: new ( PickleValue :: Dict ( vec ! [
643+ ( PickleValue :: String ( "@args" . into( ) ) , PickleValue :: Tuple ( vec![ PickleValue :: Int ( 1 ) ] ) ) ,
644+ ( PickleValue :: String ( "@state" . into( ) ) , PickleValue :: Dict ( vec![ ] ) ) ,
645+ ] ) ) ,
646+ dict_items : None ,
647+ list_items : None ,
648+ newobj : false ,
649+ } ) ) ;
650+ let bytes = encode_pickle ( & val) . unwrap ( ) ;
651+ let r = bytes. iter ( ) . position ( |& b| b == REDUCE ) . unwrap ( ) ;
652+ let b = bytes. iter ( ) . position ( |& b| b == BUILD ) . unwrap ( ) ;
653+ assert ! ( r < b && !bytes. contains( & NEWOBJ ) ) ;
654+ }
655+
623656 fn reduce ( newobj : bool , state : Option < PickleValue > ) -> PickleValue {
624657 PickleValue :: Reduce {
625658 callable : Box :: new ( PickleValue :: Global { module : "m" . into ( ) , name : "C" . into ( ) } ) ,
@@ -659,6 +692,7 @@ mod tests {
659692 ] ) ) ,
660693 dict_items : None ,
661694 list_items : None ,
695+ newobj : true ,
662696 } ) ) ;
663697 let bytes = encode_pickle ( & val) . unwrap ( ) ;
664698 let newobj = bytes. iter ( ) . position ( |& b| b == NEWOBJ ) . unwrap ( ) ;
0 commit comments