def dump_def_private(self): dumped = '' args = [] itypes = [types.full_name(x) for x in self.itypes] otype = types.full_name(self.otypes) for i, arg in enumerate(self.args): args.append('%s %s' % (itypes[i], arg)) args = ', '.join(args) definition = '%s %s(%s)\n{\n' % (otype, self.name, args) dumped += definition return dumped
def dump(self): # Init C transcribed line. self.cline = self.line # Replace operators. self.cline = self.cline.replace(' and ', ' && ') self.cline = self.cline.replace(' or ', ' || ') self.cline = self.cline.replace(' not ', ' !') # Replace booleans. self.cline = self.cline.replace('True', '1') self.cline = self.cline.replace('False', '0') # Manage casts. chars = types.full_name() for char in chars.keys(): searchto = '(%s)' % char replacewith = '(%s)' % types.full_name(char) self.cline = self.cline.replace(searchto, replacewith) # Remove indentation. self.cline = re.sub(' ', '\t', self.cline) self.cline = self.cline.replace('\t', '') if not self.cline: return '' # Fix non-value returns, compose public (to python) returns. self.cline = re.sub('return$', 'return 0', self.cline) self.perform_returns() # Detect line motif and process it. if self.cline.startswith('type '): self.process_as_init() elif self.cline.startswith('if '): self.process_as_conditional() elif self.cline.startswith('else:'): self.process_as_conditional_out() elif self.cline.startswith('for '): self.process_as_loop() else: self.process_as_idle() # Return dump string for this line. dent = '\t' * self.get_indent() return '%s%s\n' % (dent, self.cline)
def dump_def_public(self): # Init dump string. dumped = '' # Write function definition to receive Python objects. dumped += 'PyObject *%s' % self.name dumped += '(PyObject *self, PyObject *args)\n{\n' # Init variables. for i, itype in enumerate(self.itypes): typename = types.full_name(itype) arg = self.args[i] if itype == 'P': arg += '_' dumped += '\t%s %s;\n' % (typename, arg) # Convert input matrix to Python spec. pytypes = [types.python_char(x) for x in self.itypes] pytypes = ''.join(pytypes) # Create a comma-separated list of variable addresses. addresses = [] for i, itype in enumerate(self.itypes): arg = self.args[i] if itype == 'P': arg += '_' addresses.append('&%s' % arg) addresses = ', '.join(addresses) # Write the ParseTuple function. formatter = pytypes, addresses dumped += '\tPyArg_ParseTuple(args, "%s", %s);\n' % formatter # Redirect pixel buffer address with given pointer. for i, itype in enumerate(self.itypes): if itype == 'P': arg = self.args[i] bits, linkage = platform.architecture() if bits == '32bit': cast = '(unsigned char*)(long int)' if bits == '64bit': cast = '(unsigned char*)' dumped += '\tunsigned char *%s = %s%s_;\n' % (arg, cast, arg) # Return dump string for this function. return dumped
def process_as_init(self): typechar = re.search('type (.*):', self.cline).group(1) typename = types.full_name(typechar) variables = re.search(':(.*)', self.cline).group(1) self.cline = '%s%s;' % (typename, variables)