@@ -85,6 +85,8 @@ pub struct StgInfo {
8585 pub length : usize , // number of fields (for arrays/structures)
8686 pub proto : Option < PyTypeRef > , // Only for Pointer/ArrayObject
8787 pub flags : StgInfoFlags , // type flags (TYPEFLAG_*, DICTFLAG_*)
88+ // Scalar assignment uses the converter selected at type initialization.
89+ pub ( super ) scalar_code : Option < char > ,
8890
8991 // Array-specific fields
9092 pub element_type : Option < PyTypeRef > , // _type_ for arrays
@@ -123,6 +125,7 @@ impl core::fmt::Debug for StgInfo {
123125 . field ( "length" , & self . length )
124126 . field ( "proto" , & self . proto )
125127 . field ( "flags" , & self . flags )
128+ . field ( "scalar_code" , & self . scalar_code )
126129 . field ( "element_type" , & self . element_type )
127130 . field ( "element_size" , & self . element_size )
128131 . field ( "format" , & self . format )
@@ -143,6 +146,7 @@ impl Default for StgInfo {
143146 length : 0 ,
144147 proto : None ,
145148 flags : StgInfoFlags :: empty ( ) ,
149+ scalar_code : None ,
146150 element_type : None ,
147151 element_size : 0 ,
148152 format : None ,
@@ -164,6 +168,7 @@ impl StgInfo {
164168 length : 0 ,
165169 proto : None ,
166170 flags : StgInfoFlags :: empty ( ) ,
171+ scalar_code : None ,
167172 element_type : None ,
168173 element_size : 0 ,
169174 format : None ,
@@ -216,6 +221,7 @@ impl StgInfo {
216221 length,
217222 proto : None ,
218223 flags,
224+ scalar_code : None ,
219225 element_type : Some ( element_type) ,
220226 element_size,
221227 format,
@@ -595,25 +601,16 @@ impl CDataValue {
595601 let metaclass = proto. class ( ) ;
596602 let simple_metaclass = super :: simple:: PyCSimpleType :: static_type ( ) ;
597603 if metaclass. is ( simple_metaclass) || metaclass. fast_issubclass ( simple_metaclass) {
598- let type_code = proto
599- . as_object ( )
600- . get_attr ( "_type_" , vm)
601- . ok ( )
602- . and_then ( |code| code. downcast :: < PyStr > ( ) . ok ( ) )
603- . ok_or_else ( || vm. new_type_error ( "no _type_ attribute" ) ) ?;
604- let borrowed;
605- let owned;
606- let type_code = if type_code. class ( ) . is ( PyStr :: static_type ( ) ) {
607- borrowed = type_code;
608- borrowed. to_string_lossy ( )
609- } else {
610- // Preserve subclass finalization before conversion hooks run.
611- owned = type_code. to_string ( ) ;
612- drop ( type_code) ;
613- Cow :: Borrowed ( owned. as_str ( ) )
614- } ;
604+ // Reading the mutable _type_ here could select a wider converter
605+ // than the allocated slot, or change conversion during callbacks.
606+ let code = proto
607+ . stg_info ( vm) ?
608+ . scalar_code
609+ . ok_or_else ( || vm. new_type_error ( "abstract class" ) ) ?;
610+ let mut encoded = [ 0 ; 4 ] ;
611+ let type_code = code. encode_utf8 ( & mut encoded) ;
615612 let ( bytes, keep, hidden) =
616- super :: simple:: assignment_value ( & type_code, value, swapped, vm) ?;
613+ super :: simple:: assignment_value ( type_code, value, swapped, vm) ?;
617614 return Ok ( Self {
618615 bytes,
619616 keep,
0 commit comments