@@ -570,7 +570,9 @@ impl Initializer for PyCSimpleType {
570570 super :: base:: set_or_init_stginfo ( & type_ref, stg_info) ;
571571
572572 // Create __ctype_le__ and __ctype_be__ swapped types
573- create_swapped_types ( & type_ref, & type_str, vm) ?;
573+ if type_ref. class ( ) . is ( Self :: static_type ( ) ) {
574+ create_swapped_types ( & type_ref, & type_str, args, vm) ?;
575+ }
574576
575577 Ok ( ( ) )
576578 }
@@ -586,15 +588,9 @@ impl Initializer for PyCSimpleType {
586588fn create_swapped_types (
587589 type_ref : & Py < PyType > ,
588590 type_str : & str ,
591+ mut args : FuncArgs ,
589592 vm : & VirtualMachine ,
590593) -> PyResult < ( ) > {
591- use crate :: builtins:: PyDict ;
592-
593- // Avoid infinite recursion - if __ctype_le__ already exists, skip
594- if type_ref. as_object ( ) . get_attr ( "__ctype_le__" , vm) . is_ok ( ) {
595- return Ok ( ( ) ) ;
596- }
597-
598594 // Types that don't support byte order swapping - no __ctype_le__/__ctype_be__
599595 // c_void_p (P), c_char_p (z), c_wchar_p (Z), c_wchar (u), py_object (O)
600596 let unsupported_types = [ "P" , "z" , "Z" , "u" , "O" ] ;
@@ -618,31 +614,15 @@ fn create_swapped_types(
618614 // Check system byte order at compile time
619615 let is_little_endian = cfg ! ( target_endian = "little" ) ;
620616
621- // Create dict for the swapped (non-native) type
622- let swapped_dict: crate :: PyRef < crate :: builtins:: PyDict > = PyDict :: default ( ) . into_ref ( & vm. ctx ) ;
623- swapped_dict. set_item ( "_type_" , vm. ctx . new_str ( type_str) . into ( ) , vm) ?;
624-
625- // Create the swapped type using the same metaclass
626- let metaclass = type_ref. class ( ) ;
627- let bases = vm. ctx . new_tuple ( vec ! [ type_ref. as_object( ) . to_owned( ) ] ) ;
628-
629- // Set placeholder first to prevent recursion
630- set_attrs ! (
631- type_ref. as_object( ) , vm,
632- "__ctype_le__" => vm. ctx. none( ) ,
633- "__ctype_be__" => vm. ctx. none( ) ,
634- ) ;
635-
636617 // Create only the non-native endian type
637618 let suffix = if is_little_endian { "_be" } else { "_le" } ;
638- let swapped_type = metaclass. as_object ( ) . call (
639- (
640- vm. ctx . new_str ( format ! ( "{}{}" , type_ref. name( ) , suffix) ) ,
641- bases,
642- swapped_dict. as_object ( ) . to_owned ( ) ,
643- ) ,
644- vm,
645- ) ?;
619+ args. args [ 0 ] = vm
620+ . ctx
621+ . new_str ( format ! ( "{}{}" , type_ref. name( ) , suffix) )
622+ . into ( ) ;
623+ // Endian variants share bases and class attributes; neither subclasses
624+ // the other. Bypass the initializer to avoid generating another pair.
625+ let swapped_type = PyType :: slot_new ( type_ref. class ( ) . to_owned ( ) , args, vm) ?;
646626
647627 // Set _swappedbytes_ on the swapped type to indicate byte swapping is needed
648628 set_attrs ! (
@@ -651,15 +631,15 @@ fn create_swapped_types(
651631 ) ;
652632
653633 // Update swapped type's StgInfo format to use opposite endian prefix
654- if let Ok ( swapped_type_ref) = swapped_type. clone ( ) . downcast :: < PyType > ( )
655- && let Some ( mut sw_stg) = swapped_type_ref. get_type_data_mut :: < StgInfo > ( )
656- {
634+ if let Ok ( swapped_type_ref) = swapped_type. clone ( ) . downcast :: < PyType > ( ) {
635+ let mut sw_stg = type_ref. stg_info ( vm) ?. clone ( ) ;
657636 // Swapped: little-endian system uses big-endian prefix and vice versa
658637 sw_stg. big_endian = is_little_endian;
659638 sw_stg. format = Some ( alloc_format_string_for_type (
660639 type_str. chars ( ) . next ( ) . unwrap_or ( '?' ) ,
661640 is_little_endian,
662641 ) ) ;
642+ super :: base:: set_or_init_stginfo ( & swapped_type_ref, sw_stg) ;
663643 }
664644
665645 // Set attributes based on system byte order
@@ -716,7 +696,6 @@ pub(super) fn returns_scalar(cls: &Py<PyType>) -> bool {
716696 cls. base
717697 . deref ( )
718698 . is_some_and ( |base| base. is ( PyCSimple :: static_type ( ) ) )
719- || is_swapped ( cls)
720699}
721700
722701pub ( super ) fn swapped_value_bytes (
0 commit comments