@@ -389,39 +389,35 @@ pub(crate) mod _ctypes {
389389 /// Get the size of a ctypes type or instance
390390 #[ pyfunction( name = "sizeof" ) ]
391391 pub fn size_of ( obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult < usize > {
392- use super :: array:: { PyCArray , PyCArrayType } ;
393392 use super :: pointer:: PyCPointer ;
394393 use super :: structure:: { PyCStructType , PyCStructure } ;
395- use super :: union:: { PyCUnion , PyCUnionType } ;
394+ use super :: union:: PyCUnionType ;
395+ use super :: util:: StgInfo ;
396+ use crate :: builtins:: PyType ;
396397
397- // 1. Instances with stg_info
398- if obj. fast_isinstance ( PyCArray :: static_type ( ) ) {
399- // Get stg_info from the type
400- if let Some ( type_obj) = obj. class ( ) . as_object ( ) . downcast_ref :: < PyCArrayType > ( ) {
401- return Ok ( type_obj. stg_info . size ) ;
402- }
398+ // 1. Check TypeDataSlot on class (for instances)
399+ if let Some ( stg_info) = obj. class ( ) . get_type_data :: < StgInfo > ( ) {
400+ return Ok ( stg_info. size ) ;
403401 }
402+
403+ // 2. Check TypeDataSlot on type itself (for type objects)
404+ if let Some ( type_obj) = obj. downcast_ref :: < PyType > ( )
405+ && let Some ( stg_info) = type_obj. get_type_data :: < StgInfo > ( )
406+ {
407+ return Ok ( stg_info. size ) ;
408+ }
409+
410+ // 3. Instances with cdata buffer
404411 if let Some ( structure) = obj. downcast_ref :: < PyCStructure > ( ) {
405412 return Ok ( structure. cdata . read ( ) . size ( ) ) ;
406413 }
407- if obj. fast_isinstance ( PyCUnion :: static_type ( ) ) {
408- // Get stg_info from the type
409- if let Some ( type_obj) = obj. class ( ) . as_object ( ) . downcast_ref :: < PyCUnionType > ( ) {
410- return Ok ( type_obj. stg_info . size ) ;
411- }
412- }
413414 if let Some ( simple) = obj. downcast_ref :: < PyCSimple > ( ) {
414415 return Ok ( simple. cdata . read ( ) . size ( ) ) ;
415416 }
416417 if obj. fast_isinstance ( PyCPointer :: static_type ( ) ) {
417418 return Ok ( std:: mem:: size_of :: < usize > ( ) ) ;
418419 }
419420
420- // 2. Types (metatypes with stg_info)
421- if let Some ( array_type) = obj. downcast_ref :: < PyCArrayType > ( ) {
422- return Ok ( array_type. stg_info . size ) ;
423- }
424-
425421 // 3. Type objects
426422 if let Ok ( type_ref) = obj. clone ( ) . downcast :: < crate :: builtins:: PyType > ( ) {
427423 // Structure types - check if metaclass is or inherits from PyCStructType
@@ -659,33 +655,37 @@ pub(crate) mod _ctypes {
659655
660656 #[ pyfunction]
661657 fn alignment ( tp : Either < PyTypeRef , PyObjectRef > , vm : & VirtualMachine ) -> PyResult < usize > {
662- use super :: array:: { PyCArray , PyCArrayType } ;
663658 use super :: base:: PyCSimpleType ;
664659 use super :: pointer:: PyCPointer ;
665660 use super :: structure:: PyCStructure ;
666661 use super :: union:: PyCUnion ;
662+ use super :: util:: StgInfo ;
663+ use crate :: builtins:: PyType ;
667664
668665 let obj = match & tp {
669666 Either :: A ( t) => t. as_object ( ) ,
670667 Either :: B ( o) => o. as_ref ( ) ,
671668 } ;
672669
673- // Try to get alignment from stg_info directly (for instances)
674- if let Some ( array_type ) = obj. downcast_ref :: < PyCArrayType > ( ) {
675- return Ok ( array_type . stg_info . align ) ;
670+ // 1. Check TypeDataSlot on class (for instances)
671+ if let Some ( stg_info ) = obj. class ( ) . get_type_data :: < StgInfo > ( ) {
672+ return Ok ( stg_info. align ) ;
676673 }
674+
675+ // 2. Check TypeDataSlot on type itself (for type objects)
676+ if let Some ( type_obj) = obj. downcast_ref :: < PyType > ( )
677+ && let Some ( stg_info) = type_obj. get_type_data :: < StgInfo > ( )
678+ {
679+ return Ok ( stg_info. align ) ;
680+ }
681+
682+ // 3. Fallback for simple types without TypeDataSlot
677683 if obj. fast_isinstance ( PyCSimple :: static_type ( ) ) {
678684 // Get stg_info from the type by reading _type_ attribute
679685 let cls = obj. class ( ) . to_owned ( ) ;
680686 let stg_info = PyCSimpleType :: get_stg_info ( & cls, vm) ;
681687 return Ok ( stg_info. align ) ;
682688 }
683- if obj. fast_isinstance ( PyCArray :: static_type ( ) ) {
684- // Get stg_info from the type
685- if let Some ( type_obj) = obj. class ( ) . as_object ( ) . downcast_ref :: < PyCArrayType > ( ) {
686- return Ok ( type_obj. stg_info . align ) ;
687- }
688- }
689689 if obj. fast_isinstance ( PyCStructure :: static_type ( ) ) {
690690 // Calculate alignment from _fields_
691691 let cls = obj. class ( ) ;
0 commit comments