Ignore:
Timestamp:
12/23/08 10:24:23 (4 years ago)
Author:
marek
Message:

add primitive callback support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/host/pye17/codegen/codegen.py

    r4694 r4879  
    205205        ) 
    206206 
     207    cb_function_tmpl = ( 
     208        'static PyObject *%(cb_name)s_obj = NULL;\n\n' 
     209        'void %(c_name)s_cb(void)\n' 
     210        '{\n' 
     211        '    PyObject *arglist = NULL;\n\n' 
     212        '    if (%(cb_name)s_obj != NULL)\n' 
     213        '        PyEval_CallObject(%(cb_name)s_obj, arglist);\n' 
     214        '}\n\n' 
     215        ) 
     216 
    207217    virtual_accessor_tmpl = ( 
    208218        'static PyObject *\n' 
     
    278288        # Maybe this could be done in a nicer way, but I'll leave it as it is 
    279289        # for now: -- Johan 
    280         if not self.overrides.slot_is_overriden('%s.tp_init' % 
    281                                                 self.objinfo.c_name): 
    282             substdict['tp_init'] = self.write_constructor() 
     290        if not self.overrides.slot_is_overriden('%s.tp_init' % self.objinfo.c_name): 
     291           #import sys 
     292           #sys.stderr.write("tp_init: " + str(self.objinfo.c_name) + "\n") 
     293           substdict['tp_init'] = self.write_constructor() 
    283294        substdict['tp_methods'] = self.write_methods() 
    284295        substdict['tp_getset'] = self.write_getsets() 
     
    303314        self.write_virtuals() 
    304315 
    305     def write_function_wrapper(self, function_obj, template, 
     316    def write_function_wrapper(self, function_obj, template, cb_template, 
    306317                               handle_return=0, is_method=0, kwargs_needed=0, 
    307318                               substdict=None): 
     
    311322 
    312323        info = argtypes.WrapperInfo() 
     324        info.function_c_name = function_obj.c_name 
     325        info.piscb = 0 
    313326 
    314327        substdict.setdefault('errorreturn', 'NULL') 
     
    329342                info.add_parselist('|', [], []) 
    330343            handler = argtypes.matcher.get(param.ptype) 
     344            if param.piscb: 
     345              info.piscb += 1 
    331346            #fd.write("pname: " + param.pname + "; ptype: " + str(param.ptype) + "; pdflt: " + str(param.pdflt) + "; pnull: " + str(param.pnull) + "; handler: " + str(handler) + "\n") 
    332347            handler.write_param(param.ptype, param.pname, param.pdflt, 
    333                                 param.pnull, info) 
     348                                param.pnull, info, param.piscb) 
    334349 
    335350        #fd.write("arglist 2: " + str(info.get_arglist()) + "\n") 
     
    368383        substdict['typecodes'] = info.parsestr 
    369384        substdict['parselist'] = info.get_parselist() 
     385 
    370386        substdict['arglist'] = info.get_arglist() 
     387 
     388        substdict['cb_code'] = "" 
     389 
     390        # TODO: deal with more arguments 
     391        if info.piscb > 0: 
     392          substdict['arglist'] = ", &" + function_obj.c_name + "_cb" 
     393          substdict['cb_code'] = cb_template % {'c_name': function_obj.c_name, 
     394                                                'cb_name': function_obj.c_name} 
     395          template = substdict['cb_code'] + template 
     396          sys.stderr.write("cb_code: %s\n" % substdict['cb_code']) 
     397 
    371398        substdict['codebefore'] = deprecated + ( 
    372399            string.replace(info.get_codebefore(), 
     
    390417            flags = 'METH_NOARGS' 
    391418 
    392         #fd.write("arglist: " + str(info.get_arglist()) + "\n") 
    393419        #for key,item in substdict.iteritems(): 
    394         #       fd.write("key: " + str(key) + ", item: " + str(item) + "\n") 
    395         #fd.close() 
    396  
     420                #sys.stderr.write("key: " + str(key) + ", item: " + str(item) + "\n") 
     421 
     422        if info.piscb > 0: 
     423           sys.stderr.write("template: %s\n" % (template % substdict)) 
     424           #sys.stderr.write("cb_code (again): %s\n" % substdict['cb_code']) 
     425           #substdict['callback_code'] = 'balbla' 
     426           #for key,item in substdict.iteritems(): 
     427                #sys.stderr.write("key: " + str(key) + ", item: " + str(item) + "\n") 
    397428        return template % substdict, flags 
    398429 
    399430    def write_constructor(self): 
    400431        initfunc = '0' 
     432        #import sys 
     433        #sys.stderr.write("searching constructor ...\n") 
    401434        constructor = self.parser.find_constructor(self.objinfo,self.overrides) 
    402435        if not constructor: 
    403436            return self.write_default_constructor() 
    404437 
     438        #sys.stderr.write("constructor: " + str(constructor.c_name) + "\n") 
    405439        funcname = constructor.c_name 
    406440        try: 
     
    431465 
    432466                # write constructor from template ... 
     467                #sys.stderr.write("constructor: " + str(funcname) + ", tmpl:" + str(self.constructor_tmpl) + "\n") 
    433468                code = self.write_function_wrapper(constructor, 
    434469                    self.constructor_tmpl, 
     
    506541                    # write constructor from template ... 
    507542                    code, methflags = self.write_function_wrapper(meth, 
    508                         self.method_tmpl, handle_return=1, is_method=1, 
    509                         substdict=self.get_initial_method_substdict(meth)) 
     543                        self.method_tmpl, self.cb_function_tmpl, handle_return=1, 
     544                        is_method=1, substdict=self.get_initial_method_substdict(meth)) 
    510545                    self.fp.write(code) 
    511546                methods.append(self.methdef_tmpl % 
     
    857892                    # write constructor from template ... 
    858893                    code, methflags = self.write_function_wrapper(func, 
    859                         self.function_tmpl, handle_return=1, is_method=0) 
     894                        self.function_tmpl, self.cb_function_tmpl, handle_return=1, is_method=0) 
    860895                    self.fp.write(code) 
    861896                functions.append((func.name, '_wrap_' + funcname, 
Note: See TracChangeset for help on using the changeset viewer.