@@ -261,16 +261,10 @@ impl ArgumentType for PyTypeRef {
261261 {
262262 None
263263 } else {
264- // Use get_attr to traverse MRO (for subclasses like MyInt(c_int))
264+ // Converters belong to initialized storage, not mutable _type_.
265265 let typ = self
266- . as_object ( )
267- . get_attr ( vm. ctx . intern_str ( "_type_" ) , vm)
268- . ok ( )
266+ . type_code ( vm)
269267 . ok_or_else ( || vm. new_type_error ( "Unsupported argument type" ) ) ?;
270- let typ = typ
271- . downcast_ref :: < PyStr > ( )
272- . ok_or_else ( || vm. new_type_error ( "Unsupported argument type" ) ) ?
273- . to_string ( ) ;
274268 if ffi_type_from_code ( & typ) . is_none ( ) {
275269 return Err ( vm. new_type_error ( format ! ( "Unsupported argument type: {typ}" ) ) ) ;
276270 }
@@ -643,16 +637,12 @@ fn cast_check_pointertype(ctype: &PyObject, vm: &VirtualMachine) -> bool {
643637 return true ;
644638 }
645639
646- // simple pointer types via StgInfo.proto (c_void_p, c_char_p, etc.)
647- if let Ok ( type_attr) = ctype. get_attr ( "_type_" , vm)
648- && let Some ( s) = type_attr. downcast_ref :: < PyStr > ( )
640+ // Simple pointer types retain the representation selected at creation,
641+ // even if the user later changes the Python-visible _type_ attribute.
642+ if let Some ( ty) = ctype. downcast_ref :: < PyType > ( )
643+ && let Some ( code) = ty. type_code ( vm)
649644 {
650- let c = s
651- . to_str ( )
652- . expect ( "_type_ is validated as ASCII at type creation" ) ;
653- if c. len ( ) == 1 && "sPzUZXO" . contains ( c) {
654- return true ;
655- }
645+ return code. len ( ) == 1 && "sPzUZXO" . contains ( code. as_str ( ) ) ;
656646 }
657647
658648 false
@@ -1119,15 +1109,12 @@ fn compute_ret_spec(
11191109 return RetSpec :: Pointer ;
11201110 }
11211111
1122- // Simple type via its _type_ code (traversing MRO)
1123- if let Ok ( type_attr) = restype_type
1124- . as_object ( )
1125- . get_attr ( vm. ctx . intern_str ( "_type_" ) , vm)
1126- && let Some ( s) = type_attr. downcast_ref :: < PyStr > ( )
1127- && let Some ( code) = s. to_str ( )
1128- && ffi_type_from_code ( code) . is_some ( )
1112+ // Simple type via its initialized scalar converter.
1113+ if let Some ( code) = restype_type
1114+ . type_code ( vm)
1115+ . filter ( |code| ffi_type_from_code ( code) . is_some ( ) )
11291116 {
1130- return if simple_type_is_pointer ( code) {
1117+ return if simple_type_is_pointer ( & code) {
11311118 RetSpec :: Pointer
11321119 } else {
11331120 RetSpec :: Code ( code. chars ( ) . next ( ) . unwrap_or ( 'i' ) )
@@ -1605,15 +1592,11 @@ fn convert_raw_result(
16051592 // finalize it), which would self-deadlock against a held read guard.
16061593 let is_pointer_type = info. flags . contains ( StgInfoFlags :: TYPEFLAG_ISPOINTER ) ;
16071594 let has_proto = info. proto . is_some ( ) ;
1595+ let scalar_code = info. scalar_code ;
16081596 drop ( info) ;
16091597
16101598 // py_object: interpret return value as PyObject* and materialize it.
1611- if let Ok ( type_attr) = restype_type
1612- . as_object ( )
1613- . get_attr ( vm. ctx . intern_str ( "_type_" ) , vm)
1614- && let Some ( type_str) = type_attr. downcast_ref :: < PyStr > ( )
1615- && type_str. to_str ( ) == Some ( "O" )
1616- {
1599+ if scalar_code == Some ( 'O' ) {
16171600 let ptr = NonNull :: new ( result_word as * mut PyObject )
16181601 . ok_or_else ( || vm. new_value_error ( "PyObject is NULL" ) ) ?;
16191602 unsafe {
0 commit comments