<s> '''<STR_LIT:U+0020>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> from cparser import * <EOL> ctypes_type_map = { <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT:None>' , <EOL> ( '<STR_LIT:int>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT:int>' , False , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT:int>' , True , <NUM_LIT:1> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT:int>' , False , <NUM_LIT:1> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT:int>' , True , <NUM_LIT:2> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT:int>' , False , <NUM_LIT:2> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , False , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , False , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT:float>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , True , <NUM_LIT:0> ) : '<STR_LIT>' , <EOL> } <EOL> reserved_names = [ '<STR_LIT:None>' , '<STR_LIT:True>' , '<STR_LIT:False>' ] <EOL> def get_ctypes_type ( typ , declarator ) : <EOL> signed = True <EOL> typename = '<STR_LIT:int>' <EOL> longs = <NUM_LIT:0> <EOL> t = None <EOL> for specifier in typ . specifiers : <EOL> if isinstance ( specifier , StructTypeSpecifier ) : <EOL> t = CtypesStruct ( specifier ) <EOL> elif isinstance ( specifier , EnumSpecifier ) : <EOL> t = CtypesEnum ( specifier ) <EOL> elif specifier == '<STR_LIT>' : <EOL> signed = True <EOL> elif specifier == '<STR_LIT>' : <EOL> signed = False <EOL> elif specifier == '<STR_LIT>' : <EOL> longs += <NUM_LIT:1> <EOL> else : <EOL> typename = str ( specifier ) <EOL> if not t : <EOL> ctypes_name = ctypes_type_map . get ( ( typename , signed , longs ) , typename ) <EOL> t = CtypesType ( ctypes_name ) <EOL> while declarator and declarator . pointer : <EOL> if declarator . parameters is not None : <EOL> t = CtypesFunction ( t , declarator . parameters ) <EOL> a = declarator . array <EOL> while a : <EOL> t = CtypesArray ( t , a . size ) <EOL> a = a . array <EOL> if type ( t ) == CtypesType and t . name == '<STR_LIT>' : <EOL> t = CtypesType ( '<STR_LIT>' ) <EOL> elif type ( t ) == CtypesType and t . name == '<STR_LIT>' : <EOL> t = CtypesType ( '<STR_LIT>' ) <EOL> else : <EOL> t = CtypesPointer ( t , declarator . qualifiers ) <EOL> declarator = declarator . pointer <EOL> if declarator and declarator . parameters is not None : <EOL> t = CtypesFunction ( t , declarator . parameters ) <EOL> if declarator : <EOL> a = declarator . array <EOL> while a : <EOL> t = CtypesArray ( t , a . size ) <EOL> a = a . array <EOL> return t <EOL> def remove_function_pointer ( t ) : <EOL> if type ( t ) == CtypesPointer and type ( t . destination ) == CtypesFunction : <EOL> return t . destination <EOL> elif type ( t ) == CtypesPointer : <EOL> t . destination = remove_function_pointer ( t . destination ) <EOL> return t <EOL> else : <EOL> return t <EOL> class CtypesTypeVisitor ( object ) : <EOL> def visit_struct ( self , struct ) : <EOL> pass <EOL> def visit_enum ( self , enum ) : <EOL> pass <EOL> class CtypesType ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def get_required_type_names ( self ) : <EOL> '''<STR_LIT>''' <EOL> return ( self . name , ) <EOL> def visit ( self , visitor ) : <EOL> pass <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> class CtypesPointer ( CtypesType ) : <EOL> def __init__ ( self , destination , qualifiers ) : <EOL> self . destination = destination <EOL> def get_required_type_names ( self ) : <EOL> if self . destination : <EOL> return self . destination . get_required_type_names ( ) <EOL> else : <EOL> return ( ) <EOL> def visit ( self , visitor ) : <EOL> if self . destination : <EOL> self . destination . visit ( visitor ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % str ( self . destination ) <EOL> class CtypesArray ( CtypesType ) : <EOL> def __init__ ( self , base , count ) : <EOL> self . base = base <EOL> self . count = count <EOL> def get_required_type_names ( self ) : <EOL> return self . base . get_required_type_names ( ) <EOL> def visit ( self , visitor ) : <EOL> self . base . visit ( visitor ) <EOL> def __str__ ( self ) : <EOL> if self . count is None : <EOL> return '<STR_LIT>' % str ( self . base ) <EOL> if type ( self . base ) == CtypesArray : <EOL> return '<STR_LIT>' % ( str ( self . base ) , str ( self . count ) ) <EOL> else : <EOL> return '<STR_LIT>' % ( str ( self . base ) , str ( self . count ) ) <EOL> class CtypesFunction ( CtypesType ) : <EOL> def __init__ ( self , restype , parameters ) : <EOL> if parameters and parameters [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> parameters = parameters [ : - <NUM_LIT:1> ] <EOL> self . restype = restype <EOL> if type ( self . restype ) == CtypesPointer and type ( self . restype . destination ) == CtypesType and self . restype . destination . name == '<STR_LIT:None>' : <EOL> self . restype = CtypesPointer ( CtypesType ( '<STR_LIT>' ) , ( ) ) <EOL> self . argtypes = [ remove_function_pointer ( <EOL> get_ctypes_type ( p . type , p . declarator ) ) for p in parameters ] <EOL> def get_required_type_names ( self ) : <EOL> lst = list ( self . restype . get_required_type_names ( ) ) <EOL> for a in self . argtypes : <EOL> lst += list ( a . get_required_type_names ( ) ) <EOL> return lst <EOL> def visit ( self , visitor ) : <EOL> self . restype . visit ( visitor ) <EOL> for a in self . argtypes : <EOL> a . visit ( visitor ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ str ( self . restype ) ] + [ str ( a ) for a in self . argtypes ] ) <EOL> last_tagnum = <NUM_LIT:0> <EOL> def anonymous_struct_tag ( ) : <EOL> global last_tagnum <EOL> last_tagnum += <NUM_LIT:1> <EOL> return '<STR_LIT>' % last_tagnum <EOL> class CtypesStruct ( CtypesType ) : <EOL> def __init__ ( self , specifier ) : <EOL> self . is_union = specifier . is_union <EOL> self . tag = specifier . tag <EOL> if not self . tag : <EOL> self . tag = anonymous_struct_tag ( ) <EOL> if specifier . declarations : <EOL> self . opaque = False <EOL> self . members = [ ] <EOL> for declaration in specifier . declarations : <EOL> t = get_ctypes_type ( declaration . type , declaration . declarator ) <EOL> declarator = declaration . declarator <EOL> if declarator is None : <EOL> return <EOL> while declarator . pointer : <EOL> declarator = declarator . pointer <EOL> name = declarator . identifier <EOL> self . members . append ( ( name , t ) ) <EOL> else : <EOL> self . opaque = True <EOL> self . members = [ ] <EOL> def get_required_type_names ( self ) : <EOL> lst = [ '<STR_LIT>' % self . tag ] <EOL> for m in self . members : <EOL> lst += m [ <NUM_LIT:1> ] . get_required_type_names ( ) <EOL> return lst <EOL> def visit ( self , visitor ) : <EOL> visitor . visit_struct ( self ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . tag <EOL> last_tagnum = <NUM_LIT:0> <EOL> def anonymous_enum_tag ( ) : <EOL> global last_tagnum <EOL> last_tagnum += <NUM_LIT:1> <EOL> return '<STR_LIT>' % last_tagnum <EOL> class CtypesEnum ( CtypesType ) : <EOL> def __init__ ( self , specifier ) : <EOL> self . tag = specifier . tag <EOL> if not self . tag : <EOL> self . tag = anonymous_enum_tag ( ) <EOL> value = <NUM_LIT:0> <EOL> context = EvaluationContext ( ) <EOL> self . enumerators = [ ] <EOL> for e in specifier . enumerators : <EOL> if e . expression : <EOL> try : <EOL> value = int ( e . expression . evaluate ( context ) ) <EOL> except : <EOL> pass <EOL> self . enumerators . append ( ( e . name , value ) ) <EOL> value += <NUM_LIT:1> <EOL> def get_required_type_names ( self ) : <EOL> return [ ] <EOL> def visit ( self , visitor ) : <EOL> visitor . visit_enum ( self ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . tag <EOL> class CtypesParser ( CParser ) : <EOL> '''<STR_LIT>''' <EOL> def handle_define ( self , name , value , filename , lineno ) : <EOL> if value == '<STR_LIT:int>' : <EOL> t = CtypesType ( '<STR_LIT>' ) <EOL> self . handle_ctypes_type_definition ( <EOL> name , t , filename , lineno ) <EOL> def handle_define_constant ( self , name , value , filename , lineno ) : <EOL> if name in reserved_names : <EOL> name += '<STR_LIT:_>' <EOL> self . handle_ctypes_constant ( name , value , filename , lineno ) <EOL> def handle_declaration ( self , declaration , filename , lineno ) : <EOL> t = get_ctypes_type ( declaration . type , declaration . declarator ) <EOL> declarator = declaration . declarator <EOL> if declarator is None : <EOL> return <EOL> while declarator . pointer : <EOL> declarator = declarator . pointer <EOL> name = declarator . identifier <EOL> if declaration . storage == '<STR_LIT>' : <EOL> self . handle_ctypes_type_definition ( <EOL> name , remove_function_pointer ( t ) , filename , lineno ) <EOL> elif type ( t ) == CtypesFunction : <EOL> self . handle_ctypes_function ( <EOL> name , t . restype , t . argtypes , filename , lineno ) <EOL> elif declaration . storage != '<STR_LIT>' : <EOL> self . handle_ctypes_variable ( name , t , filename , lineno ) <EOL> def handle_ctypes_constant ( self , name , value , filename , lineno ) : <EOL> pass <EOL> def handle_ctypes_type_definition ( self , name , ctype , filename , lineno ) : <EOL> pass <EOL> def handle_ctypes_function ( self , name , restype , argtypes , filename , lineno ) : <EOL> pass <EOL> def handle_ctypes_variable ( self , name , ctype , filename , lineno ) : <EOL> pass </s>
