Changeset 633400d for python/lib/gen_code.py
- Timestamp:
- Dec 5, 2018, 10:34:39 PM (6 years ago)
- Branches:
- feature/cnn, feature/crepe, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
- Children:
- 283a619a
- Parents:
- 5b46bc3 (diff), f19db54 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/lib/gen_code.py
r5b46bc3 r633400d 3 3 'buf_size': 'Py_default_vector_length', 4 4 'win_s': 'Py_default_vector_length', 5 'size': 'Py_default_vector_length', 5 6 # and here too 6 7 'hop_size': 'Py_default_vector_length / 2', … … 85 86 'tss': 'self->buf_size', 86 87 'pitchshift': 'self->hop_size', 88 'dct': 'self->size', 87 89 } 88 90 … … 180 182 self.do_outputs = get_params_types_names(self.do_proto)[2:] 181 183 struct_output_str = ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in self.do_outputs] 184 if len(self.prototypes['rdo']): 185 rdo_outputs = get_params_types_names(prototypes['rdo'][0])[2:] 186 struct_output_str += ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in rdo_outputs] 187 self.outputs += rdo_outputs 182 188 self.struct_outputs = ";\n ".join(struct_output_str) 183 189 … … 187 193 def gen_code(self): 188 194 out = "" 189 out += self.gen_struct() 190 out += self.gen_doc() 191 out += self.gen_new() 192 out += self.gen_init() 193 out += self.gen_del() 194 out += self.gen_do() 195 out += self.gen_memberdef() 196 out += self.gen_set() 197 out += self.gen_get() 198 out += self.gen_methodef() 199 out += self.gen_typeobject() 195 try: 196 out += self.gen_struct() 197 out += self.gen_doc() 198 out += self.gen_new() 199 out += self.gen_init() 200 out += self.gen_del() 201 out += self.gen_do() 202 if len(self.prototypes['rdo']): 203 self.do_proto = self.prototypes['rdo'][0] 204 self.do_inputs = [get_params_types_names(self.do_proto)[1]] 205 self.do_outputs = get_params_types_names(self.do_proto)[2:] 206 out += self.gen_do(method='rdo') 207 out += self.gen_memberdef() 208 out += self.gen_set() 209 out += self.gen_get() 210 out += self.gen_methodef() 211 out += self.gen_typeobject() 212 except Exception as e: 213 print ("Failed generating code for", self.shortname) 214 raise 200 215 return out 201 216 … … 381 396 return out 382 397 383 def gen_do(self ):398 def gen_do(self, method = 'do'): 384 399 out = """ 385 400 // do {shortname} 386 401 static PyObject* 387 Py _{shortname}_do(Py_{shortname} * self, PyObject * args)388 {{""".format( **self.__dict__)402 Pyaubio_{shortname}_{method} (Py_{shortname} * self, PyObject * args) 403 {{""".format(method = method, **self.__dict__) 389 404 input_params = self.do_inputs 390 405 output_params = self.do_outputs … … 462 477 """.format(**self.__dict__) 463 478 for set_param in self.prototypes['set']: 464 params = get_params_types_names(set_param)[1] 465 paramtype = params['type'] 479 params = get_params_types_names(set_param)[1:] 480 param = self.shortname.split('_set_')[-1] 481 paramdecls = "".join([""" 482 {0} {1};""".format(p['type'], p['name']) for p in params]) 466 483 method_name = get_name(set_param) 467 484 param = method_name.split('aubio_'+self.shortname+'_set_')[-1] 468 pyparamtype = pyargparse_chars[paramtype] 485 refs = ", ".join(["&%s" % p['name'] for p in params]) 486 paramlist = ", ".join(["%s" % p['name'] for p in params]) 487 if len(params): 488 paramlist = "," + paramlist 489 pyparamtypes = ''.join([pyargparse_chars[p['type']] for p in params]) 469 490 out += """ 470 491 static PyObject * … … 472 493 {{ 473 494 uint_t err = 0; 474 {paramtype} {param}; 475 476 if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{ 495 {paramdecls} 496 """.format(param = param, paramdecls = paramdecls, **self.__dict__) 497 498 if len(refs) and len(pyparamtypes): 499 out += """ 500 501 if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{ 477 502 return NULL; 478 503 }} 479 err = aubio_{shortname}_set_{param} (self->o, {param}); 504 """.format(pyparamtypes = pyparamtypes, refs = refs) 505 506 out += """ 507 err = aubio_{shortname}_set_{param} (self->o {paramlist}); 480 508 481 509 if (err > 0) {{ 482 PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}"); 510 if (PyErr_Occurred() == NULL) {{ 511 PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}"); 512 }} else {{ 513 // change the RuntimeError into ValueError 514 PyObject *type, *value, *traceback; 515 PyErr_Fetch(&type, &value, &traceback); 516 PyErr_Restore(PyExc_ValueError, value, traceback); 517 }} 483 518 return NULL; 484 519 }} 485 520 Py_RETURN_NONE; 486 521 }} 487 """.format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__) 522 """.format(param = param, refs = refs, paramdecls = paramdecls, 523 pyparamtypes = pyparamtypes, paramlist = paramlist, **self.__dict__) 488 524 return out 489 525 … … 526 562 {{"{shortname}", (PyCFunction) Py{name}, 527 563 METH_NOARGS, ""}},""".format(name = name, shortname = shortname) 564 for m in self.prototypes['rdo']: 565 name = get_name(m) 566 shortname = name.replace('aubio_%s_' % self.shortname, '') 567 out += """ 568 {{"{shortname}", (PyCFunction) Py{name}, 569 METH_VARARGS, ""}},""".format(name = name, shortname = shortname) 528 570 out += """ 529 571 {NULL} /* sentinel */ … … 551 593 0, 552 594 0, 553 (ternaryfunc)Py _{shortname}_do,595 (ternaryfunc)Pyaubio_{shortname}_do, 554 596 0, 555 597 0,
Note: See TracChangeset
for help on using the changeset viewer.