@@ -364,17 +364,40 @@ def convert_attribute(self, context, instance, attribute, nocheck=False):
364364 else :
365365 outputType = mergeTypeWrappers (possibleTypes )
366366
367- output = context .allocateUninitializedSlot (outputType )
367+ native = context .converter .defineNativeFunction (
368+ 'getattr(' + self .typeRepresentation .__name__ + ", " + attribute + ")" ,
369+ ('getattr' , self , attribute ),
370+ [self ],
371+ outputType ,
372+ lambda context , out , instance : self .generateNativeGetattr (
373+ context , outputType , validIndices , out , instance , attribute
374+ )
375+ )
376+
377+ if outputType .is_pass_by_ref :
378+ return context .push (
379+ outputType ,
380+ lambda out : native .call (out , instance )
381+ )
382+ else :
383+ return context .pushPod (
384+ outputType ,
385+ native .call (instance )
386+ )
387+
388+ def generateNativeGetattr (self , context , outputType , validIndices , out , instance , attr ):
389+ which = instance .nonref_expr .ElementPtrIntegers (0 , 1 ).load ()
390+ for ix in validIndices :
391+ with context .ifelse (which .eq (ix )) as (ifTrue , ifFalse ):
392+ with ifTrue :
393+ res = self .refAs (context , instance , ix ).convert_attribute (attr )
394+ if res is not None :
395+ res = res .convert_to_type (outputType , ConversionLevel .Signature )
368396
369- with context .switch (instance .nonref_expr .ElementPtrIntegers (0 , 1 ).load (), validIndices , False ) as indicesAndContexts :
370- for ix , subcontext in indicesAndContexts :
371- with subcontext :
372- attr = self .refAs (context , instance , ix ).convert_attribute (attribute )
373- attr = attr .convert_to_type (outputType , ConversionLevel .Signature )
374- output .convert_copy_initialize (attr )
375- context .markUninitializedSlotInitialized (output )
397+ context .pushReturnValue (res )
376398
377- return output
399+ if len (validIndices ) != len (self .alternatives ):
400+ context .pushException (AttributeError , attr )
378401
379402 def convert_check_matches (self , context , instance , typename ):
380403 index = - 1
0 commit comments