History

python/lib/gen_code.py: use new proxy functions

Paul Brossier committed 10 years ago

Commit a7f398d1 parent b5bef112 files · +83 −50Browse files

Showing the changes to python (2 of 2 files). Show all

Unified · Split

2 files
  1. python/lib/gen_code.py +81 −48
  2. python/lib/gen_external.py +2 −2
python/lib/gen_code.py+81−48
@@ -38,7 +38,7 @@ pyfromtype_fn = { · 37 unmodified lines
3838 pytoaubio_fn = {
3939 'fvec_t*': 'PyAubio_ArrayToCFvec',
4040 'cvec_t*': 'PyAubio_ArrayToCCvec',
41− 'fmat_t*': 'PyAubio_ArrayToCFmat',
41+ #'fmat_t*': 'PyAubio_ArrayToCFmat',
4242 }
4343
4444 pyfromaubio_fn = {
@@ -74,6 +74,7 @@ pyargparse_chars = { · 29 unmodified lines
7474 'char_t*': 's',
7575 'fmat_t*': 'O',
7676 'fvec_t*': 'O',
77+ 'cvec_t*': 'O',
7778 }
7879
7980 objoutsize = {
@@ -83,8 +84,9 @@ objoutsize = { · 3 unmodified lines
8384 'sampler': 'self->hop_size',
8485 'mfcc': 'self->n_coeffs',
8586 'specdesc': '1',
86− 'tempo': '1',
87+ 'tempo': '2',
8788 'filterbank': 'self->n_filters',
89+ 'tss': 'self->hop_size',
8890 }
8991
9092 def get_name(proto):
@@ -104,20 +106,20 @@ def split_type(arg): · 13 unmodified lines
104106 """ arg = 'foo *name'
105107 return ['foo*', 'name'] """
106108 l = arg.split()
107− type_arg = {'type': l[0], 'name': l[1]}
108− # ['foo', '*name'] -> ['foo*', 'name']
109− if l[-1].startswith('*'):
110− #return [l[0]+'*', l[1][1:]]
111− type_arg['type'] = l[0] + '*'
112− type_arg['name'] = l[1][1:]
113− # ['foo', '*', 'name'] -> ['foo*', 'name']
114− if len(l) == 3:
115− #return [l[0]+l[1], l[2]]
116− type_arg['type'] = l[0]+l[1]
117− type_arg['name'] = l[2]
118− else:
119− #return l
120− pass
109+ type_arg = {} #'type': l[0], 'name': l[1]}
110+ type_arg['type'] = " ".join(l[:-1])
111+ type_arg['name'] = l[-1]
112+ # fix up type / name
113+ if type_arg['name'].startswith('*'):
114+ # ['foo', '*name'] -> ['foo*', 'name']
115+ type_arg['type'] += '*'
116+ type_arg['name'] = type_arg['name'][1:]
117+ if type_arg['type'].endswith(' *'):
118+ # ['foo *', 'name'] -> ['foo*', 'name']
119+ type_arg['type'] = type_arg['type'].replace(' *','*')
120+ if type_arg['type'].startswith('const '):
121+ # ['foo *', 'name'] -> ['foo*', 'name']
122+ type_arg['type'] = type_arg['type'].replace('const ','')
121123 return type_arg
122124
123125 def get_params(proto):
@@ -126,16 +128,28 @@ def get_params(proto): · 2 unmodified lines
126128 returns: ['int argc', 'char ** argv']
127129 """
128130 import re
129− paramregex = re.compile('[\(, ](\w+ \*?\*? ?\w+)[, \)]')
130− return paramregex.findall(proto)
131+ paramregex = re.compile('.*\((.*)\);')
132+ a = paramregex.findall(proto)[0].split(', ')
133+ #a = [i.replace('const ', '') for i in a]
134+ return a
135+
136+def get_input_params(proto):
137+ a = get_params(proto)
138+ return [i.replace('const ', '') for i in a if (i.startswith('const ') or i.startswith('uint_t ') or i.startswith('smpl_t '))]
139+
140+def get_output_params(proto):
141+ a = get_params(proto)
142+ return [i for i in a if not i.startswith('const ')][1:]
131143
132144 def get_params_types_names(proto):
133145 """ get the list of parameters from a function prototype
134146 example: proto = "int main (int argc, char ** argv)"
135147 returns: [['int', 'argc'], ['char **','argv']]
136148 """
137− return list(map(split_type, get_params(proto)))
138−
149+ a = list(map(split_type, get_params(proto)))
150+ #print proto, a
151+ #import sys; sys.exit(1)
152+ return a
139153
140154 class MappedObject(object):
141155
@@ -148,10 +162,15 @@ class MappedObject(object): · 6 unmodified lines
148162 self.del_proto = prototypes['del'][0]
149163 self.do_proto = prototypes['do'][0]
150164 self.input_params = get_params_types_names(self.new_proto)
151− self.input_params_list = "; ".join(get_params(self.new_proto))
165+ self.input_params_list = "; ".join(get_input_params(self.new_proto))
152166 self.outputs = get_params_types_names(self.do_proto)[2:]
153− self.outputs_flat = get_params(self.do_proto)[2:]
154− self.output_results = ", ".join(self.outputs_flat)
167+ self.do_inputs = [get_params_types_names(self.do_proto)[1]]
168+ self.do_outputs = get_params_types_names(self.do_proto)[2:]
169+ self.outputs_flat = get_output_params(self.do_proto)
170+ self.output_results = "; ".join(self.outputs_flat)
171+
172+ print "input_params", map(split_type, get_input_params(self.do_proto))
173+ print "output_params", map(split_type, get_output_params(self.do_proto))
155174
156175 def gen_code(self):
157176 out = ""
@@ -177,17 +196,19 @@ typedef struct{{ · 19 unmodified lines
177196 {longname} *o;
178197 // input parameters
179198 {input_params_list};
199+ // do input vectors
200+ {do_inputs_list};
180201 // output results
181202 {output_results};
182203 }} Py_{shortname};
183204 """
184− return out.format(**self.__dict__)
205+ return out.format(do_inputs_list = "; ".join(get_input_params(self.do_proto)), **self.__dict__)
185206
186207 def gen_doc(self):
187208 out = """
188209 // TODO: add documentation
189210 static char Py_{shortname}_doc[] = \"undefined\";
190− """
211+"""
191212 return out.format(**self.__dict__)
192213
193214 def gen_new(self):
@@ -221,7 +242,7 @@ Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) · 27 unmodified lines
221242 """.format(**self.__dict__)
222243 params = self.input_params
223244 for p in params:
224− out += self.check_valid(p)
245+ out += self.check_valid(p)
225246 out += """
226247 return (PyObject *)self;
227248 }
@@ -289,6 +310,9 @@ Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds) · 61 unmodified lines
289310 out += """
290311 // TODO get internal params after actual object creation?
291312 """
313+ for input_param in self.do_inputs:
314+ out += """
315+ self->{0} = ({1})malloc(sizeof({2}));""".format(input_param['name'], input_param['type'], input_param['type'][:-1])
292316 out += """
293317 // create outputs{output_create}
294318 """.format(output_create = output_create)
@@ -317,6 +341,9 @@ static PyMemberDef Py_{shortname}_members[] = {{ · 22 unmodified lines
317341 static void
318342 Py_{shortname}_del (Py_{shortname} * self, PyObject * unused)
319343 {{""".format(**self.__dict__)
344+ for input_param in self.do_inputs:
345+ out += """
346+ free(self->{0[name]});""".format(input_param)
320347 for o in self.outputs:
321348 name = o['name']
322349 del_out = delfromtype_fn[o['type']]
@@ -331,39 +358,45 @@ Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) · 8 unmodified lines
331358 return out
332359
333360 def gen_do(self):
334− do_fn = get_name(self.do_proto)
335− input_param = get_params_types_names(self.do_proto)[1];
336− pytoaubio = pytoaubio_fn[input_param['type']]
337361 output = self.outputs[0]
338362 out = """
339363 // do {shortname}
340364 static PyObject*
341365 Py_{shortname}_do (Py_{shortname} * self, PyObject * args)
342−{{
343− PyObject * in_obj;
344− {input_type} {input_name};
345−
346− if (!PyArg_ParseTuple (args, "O", &in_obj)) {{
366+{{""".format(**self.__dict__)
367+ input_params = self.do_inputs
368+ output_params = self.do_outputs
369+ #print input_params
370+ #print output_params
371+ for input_param in input_params:
372+ out += """
373+ PyObject *py_{0};""".format(input_param['name'], input_param['type'])
374+ refs = ", ".join(["&py_%s" % p['name'] for p in input_params])
375+ pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params])
376+ out += """
377+ if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
347378 return NULL;
348− }}
349− {input_name} = {pytoaubio} (in_obj);
350− if ({input_name} == NULL) {{
379+ }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)
380+ for p in input_params:
381+ out += """
382+ if (!{pytoaubio}(py_{0[name]}, self->{0[name]})) {{
351383 return NULL;
352− }}
384+ }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
385+ do_fn = get_name(self.do_proto)
386+ inputs = ", ".join(['self->'+p['name'] for p in input_params])
387+ outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
388+ out += """
353389
354− {do_fn}(self->o, {input_name}, {outputs});
390+ {do_fn}(self->o, {inputs}, {outputs});
355391
356392 return (PyObject *) {aubiotonumpy} ({outputs});
357393 }}
358−"""
359− return out.format(do_fn = do_fn,
360− shortname = self.prototypes['shortname'],
361− input_name = input_param['name'],
362− input_type= input_param['type'],
363− pytoaubio = pytoaubio,
364− outputs = ", ".join(["self->%s" % p['name'] for p in self.outputs]),
365− aubiotonumpy = pyfromaubio_fn[output['type']],
366− )
394+""".format(
395+ do_fn = do_fn,
396+ aubiotonumpy = pyfromaubio_fn[output['type']],
397+ inputs = inputs, outputs = outputs,
398+ )
399+ return out
367400
368401 def gen_set(self):
369402 out = """
python/lib/gen_external.py+2−2
@@ -116,7 +116,7 @@ def generate_external(output_path): · 115 unmodified lines
116116 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
117117 elif len(lib[o][family]) == 1:
118118 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
119− else:
119+ else:
120120 print ( "{:15s} {:10s} {:d}".format(o, family, len(lib[o][family]) ) )
121121 """
122122
@@ -158,7 +158,7 @@ void add_generated_objects ( PyObject *m ) · 35 unmodified lines
158158 f.write(out)
159159 print ("wrote %s" % output_file )
160160 sources_list.append(output_file)
161−
161+
162162 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
163163 out = """
164164 // generated list of objects created with gen_external.py

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t