From e5d5acf9df8d5b3cbabaa29d82ed025336fef900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Houz=C3=A9fa=20Abbasbhay?= Date: Mon, 1 Dec 2014 11:46:21 +0100 Subject: [PATCH] Python 3: - file(...) -> open(...) - print ... -> print(...) - print >> fd, ... -> print(..., file=fd) - basestring -> str - map(...) -> list(map(...)) --- bindings/bindings.py | 24 +-- bindings/python/lang.py | 462 ++++++++++++++++++++-------------------- bindings/utils.py | 14 +- 3 files changed, 250 insertions(+), 250 deletions(-) diff --git a/bindings/bindings.py b/bindings/bindings.py index d7687ff1..70fd5756 100644 --- a/bindings/bindings.py +++ b/bindings/bindings.py @@ -103,9 +103,9 @@ class BindingData: def display_funcs(self): for func in self.functions: - print func.return_type, func.name + print(func.return_type, func.name) for a in func.args: - print ' ', a + print(' ', a) def order_class_hierarchy(self): new_order = [] @@ -191,11 +191,11 @@ class Struct: return '' % (self.name, self.parent) def display(self): - print self.__repr__() + print(self.__repr__()) for m in self.members: - print ' ', m + print(' ', m) for m in self.methods: - print ' ', m + print(' ', m) def getMember(self, name): l = [m for m in self.members if arg_name(m) == name] @@ -240,8 +240,8 @@ class Function: try: arg = [x for x in self.args if x[1] == param.attrib.get('name')][0] except IndexError: - print >> sys.stderr, 'W: no such param (%s) in function (%s)' % ( - param.attrib.get('name'), self.name) + print('W: no such param (%s) in function (%s)' % ( + param.attrib.get('name'), self.name), file=sys.stderr) continue if param.attrib.get('optional') == 'true': arg[2]['optional'] = True @@ -405,7 +405,7 @@ def parse_header(header_file): in_struct_private = False in_ifdef_zero = False - lines = file(header_file).readlines() + lines = open(header_file).readlines() i = 0 while i < len(lines): line = lines[i] @@ -470,7 +470,7 @@ def parse_header(header_file): if not in_struct.name in binding.structs_toskip: binding.structs.append(in_struct) else: - print >>sys.stderr, 'W: skipping structure %s due to overrides.xml' % in_struct.name + print('W: skipping structure %s due to overrides.xml' % in_struct.name, file=sys.stderr) in_struct = None elif '/*< public >*/' in line: in_struct_private = False @@ -490,7 +490,7 @@ def parse_header(header_file): member_type, member_name = normalise_var(member_match.group(1), member_match.group(2)) field = (member_type, member_name, {}) if member_type == 'void*': - print >>sys.stderr, 'W: skipping field %s.%s' % (in_struct.name, member_name) + print('W: skipping field %s.%s' % (in_struct.name, member_name), file=sys.stderr) else: if is_glist(field) or is_hashtable(field): found = re.search(r' of ([^*]*)', line) @@ -539,13 +539,13 @@ def parse_header(header_file): type = clean_type(type) f.args.append(list((type, name, {}))) else: - print >>sys.stderr, 'failed to process:', arg, 'in line:', line + print('failed to process:', arg, 'in line:', line, file=sys.stderr) f.skip = True f.apply_overrides() if not f.skip: binding.functions.append(f) else: - print >>sys.stderr, 'W: skipping function', f + print('W: skipping function', f, file=sys.stderr) i += 1 diff --git a/bindings/python/lang.py b/bindings/python/lang.py index f56c731e..b8488617 100644 --- a/bindings/python/lang.py +++ b/bindings/python/lang.py @@ -33,9 +33,9 @@ def remove_bad_optional(args): if not '=' in x: non_opt = True elif non_opt: - print >>sys.stderr, 'W: changed', x, + print('W: changed', x, file=sys.stderr) x = re.sub(' *=.*', '', x) - print >>sys.stderr, 'to', x + print('to', x, file=sys.stderr) new_args.append(x) new_args.reverse() return new_args @@ -72,23 +72,23 @@ class Binding: if not name: raise Exception('Cannot free, missing a name') if is_cstring(type): - print >>fd, ' lasso_release_string(%s);' % name + print(' lasso_release_string(%s);' % name, file=fd) elif is_int(type, self.binding_data) or is_boolean(type): pass elif is_xml_node(type): - print >>fd, ' lasso_release_xml_node(%s);' % name + print(' lasso_release_xml_node(%s);' % name, file=fd) elif is_glist(type): etype = element_type(type) if is_cstring(etype): - print >> fd, ' lasso_release_list_of_strings(%s);' % name + print(' lasso_release_list_of_strings(%s);' % name, file=fd) elif is_object(etype): - print >> fd, ' lasso_release_list_of_gobjects(%s);' % name + print(' lasso_release_list_of_gobjects(%s);' % name, file=fd) else: raise Exception('Unsupported caller owned return type %s' % ((repr(type), name),)) elif is_hashtable(type): raise Exception('Unsupported caller owned return type %s' % ((repr(type), name),)) elif is_object(type): - print >> fd, ' if (return_value) g_object_unref(%s);' % name + print(' if (return_value) g_object_unref(%s);' % name, file=fd) else: raise Exception('Unsupported caller owned return type %s' % ((repr(type), name),)) @@ -109,7 +109,7 @@ class Binding: fd.close() def generate_header(self, fd): - print >> fd, '''\ + print('''\ # this file has been generated automatically; do not edit import _lasso @@ -155,11 +155,11 @@ class frozendict(dict): def __repr__(self): return 'frozendict(%s)' % dict.__repr__(self) -''' +''', file=fd) def generate_exceptions(self, fd): done_cats = [] - print >> fd, '''\ + print('''\ class Error(Exception): code = None @@ -185,15 +185,15 @@ class Error(Exception): return _lasso.strError(self.code) else: raise IndexError() -''' +''', file=fd) for exc_cat in self.binding_data.overrides.findall('exception/category'): cat = exc_cat.attrib.get('name') done_cats.append(cat) parent_cat = exc_cat.attrib.get('parent', '') - print >> fd, '''\ + print('''\ class %sError(%sError): pass -''' % (cat, parent_cat) +''' % (cat, parent_cat), file=fd) exceptions_dict = {} @@ -213,10 +213,10 @@ class %sError(%sError): else: parent_cat = '' - print >> fd, '''\ + print('''\ class %sError(%sError): pass -''' % (cat, parent_cat) +''' % (cat, parent_cat), file=fd) exceptions_dict[detail] = c[1][6:] @@ -231,19 +231,19 @@ class %sError(%sError): # ordering would change) continue - print >> fd, '''\ + print('''\ class %sError(%sError): pass -''' % (detail, cat) +''' % (detail, cat), file=fd) - print >> fd, 'exceptions_dict = {' + print('exceptions_dict = {', file=fd) for k, v in exceptions_dict.items(): - print >> fd, ' _lasso.%s: %sError,' % (v, k) - print >> fd, '}' - print >> fd, '' + print(' _lasso.%s: %sError,' % (v, k), file=fd) + print('}', file=fd) + print('', file=fd) def generate_footer(self, fd): - print >> fd, ''' + print(''' import lasso @@ -291,22 +291,22 @@ if WSF_SUPPORT: InteractionProfileService.buildResponseMsg = InteractionProfileService.buildSoapResponseMsg DataService.buildRequestMsg = DataService.buildSoapRequestMsg DiscoModifyResponse.newEntryIds = DiscoModifyResponse.newEntryIDs -''' +''', file=fd) def generate_constants(self, fd): - print >> fd, '### Constants (both enums and defines)' + print('### Constants (both enums and defines)', file=fd) for c in self.binding_data.constants: - print >> fd, '%s = _lasso.%s' % (c[1][6:], c[1][6:]) + print('%s = _lasso.%s' % (c[1][6:], c[1][6:]), file=fd) for c in self.binding_data.overrides.findall('constant'): name = c.attrib.get('name') if c.attrib.get('value'): name = name[6:] # dropping LASSO_ value = c.attrib.get('value') if value == 'True': - print >> fd, '%s = True' % name + print('%s = True' % name, file=fd) else: - print >> sys.stderr, 'E: unknown value for constant: %r' % value - print >> fd, '' + print('E: unknown value for constant: %r' % value, file=sys.stderr) + print('', file=fd) def generate_class(self, clss, fd): klassname = clss.name[5:] # remove Lasso from class name @@ -315,7 +315,7 @@ if WSF_SUPPORT: else: parentname = clss.parent[5:] - print >> fd, '''class %(klassname)s(%(parentname)s):''' % locals() + print('''class %(klassname)s(%(parentname)s):''' % locals(), file=fd) methods = clss.methods[:] # constructor(s) @@ -338,14 +338,14 @@ if WSF_SUPPORT: c_args = ', '.join(c_args) py_args = ', ' + ', '.join(py_args) - print >> fd, ' def __init__(self%s):' % py_args + print(' def __init__(self%s):' % py_args, file=fd) # XXX: could check self._cptr.typename to see if it got the # right class type - print >> fd, ' self._cptr = _lasso.%s(%s)' % ( - m.name[6:], c_args) - print >> fd, ' if self._cptr is None:' - print >> fd, ' raise Error(\'failed to create object\')' - print >> fd, '' + print(' self._cptr = _lasso.%s(%s)' % ( + m.name[6:], c_args), file=fd) + print(' if self._cptr is None:', file=fd) + print(' raise Error(\'failed to create object\')', file=fd) + print('', file=fd) for m in self.binding_data.functions: if m.name.startswith(method_prefix + 'new_'): @@ -369,13 +369,13 @@ if WSF_SUPPORT: if '=' in x: opt = True elif opt: - print >>sys.stderr, 'W: non-optional follows optional,', m + print('W: non-optional follows optional,', m, file=sys.stderr) c_args = ', '.join(c_args) py_args = ', ' + ', '.join(py_args) - print >> fd, ' @classmethod' - print >> fd, ' def %s(cls%s):' % (constructor_name, py_args) - print >> fd, ' return cptrToPy(_lasso.%s(%s))' % (m.name[6:], c_args) - print >> fd, '' + print(' @classmethod', file=fd) + print(' def %s(cls%s):' % (constructor_name, py_args), file=fd) + print(' return cptrToPy(_lasso.%s(%s))' % (m.name[6:], c_args), file=fd) + print('', file=fd) # create properties for members for m in clss.members: @@ -383,13 +383,13 @@ if WSF_SUPPORT: mname = format_as_camelcase(m[1]) options = m[2] # getter - print >> fd, ' def get_%s(self):' % mname - print >> fd, ' t = _lasso.%s_%s_get(self._cptr)' % ( - klassname, mname) + print(' def get_%s(self):' % mname, file=fd) + print(' t = _lasso.%s_%s_get(self._cptr)' % ( + klassname, mname), file=fd) if is_int(m, self.binding_data) or is_xml_node(m) or is_cstring(m) or is_boolean(m): pass elif is_object(m): - print >> fd, ' t = cptrToPy(t)' + print(' t = cptrToPy(t)', file=fd) elif is_glist(m): el_type = element_type(m) if is_cstring(el_type): @@ -397,54 +397,54 @@ if WSF_SUPPORT: elif is_xml_node(el_type): pass elif is_object(el_type): - print >> fd, ' if not t: return t' - print >> fd, ' t = tuple([cptrToPy(x) for x in t])' + print(' if not t: return t', file=fd) + print(' t = tuple([cptrToPy(x) for x in t])', file=fd) else: raise Exception('Unsupported python getter %s.%s' % (clss, m)) elif is_hashtable(m): el_type = element_type(m) - print >> fd, ' if not t: return t' + print(' if not t: return t', file=fd) if is_object(el_type): - print >> fd, ' d2 = {}' - print >> fd, ' for k, v in t.items():' - print >> fd, ' d2[k] = cptrToPy(v)' - print >> fd, ' t = frozendict(d2)' + print(' d2 = {}', file=fd) + print(' for k, v in t.items():', file=fd) + print(' d2[k] = cptrToPy(v)', file=fd) + print(' t = frozendict(d2)', file=fd) else: - print >> fd, ' t = frozendict(t)' + print(' t = frozendict(t)', file=fd) elif is_boolean(m) or is_int(m, self.binding_data) or is_xml_node(m) or is_cstring(m): pass else: raise Exception('Unsupported python getter %s.%s' % (clss, m)) - print >> fd, ' return t;' + print(' return t;', file=fd) # setter - print >> fd, ' def set_%s(self, value):' % mname + print(' def set_%s(self, value):' % mname, file=fd) if is_int(m, self.binding_data) or is_xml_node(m) or is_boolean(m): pass elif is_cstring(m): - print >> fd, ' value = str2lasso(value)' + print(' value = str2lasso(value)', file=fd) elif is_object(m): - print >> fd, ' if value is not None:' - print >> fd, ' value = value and value._cptr' + print(' if value is not None:', file=fd) + print(' value = value and value._cptr', file=fd) elif is_glist(m): el_type = element_type(m) if is_cstring(el_type) or is_xml_node(el_type): pass elif is_object(el_type): - print >> fd, ' if value is not None:' - print >> fd, ' value = tuple([x._cptr for x in value])' + print(' if value is not None:', file=fd) + print(' value = tuple([x._cptr for x in value])', file=fd) else: raise Exception('Unsupported python setter %s.%s' % (clss, m)) elif is_hashtable(m): - print >> sys.stderr, 'W: unsupported setter for hashtable %s' % (m,) + print('W: unsupported setter for hashtable %s' % (m,), file=sys.stderr) else: - print >> sys.stderr, 'W: unsupported setter for %s' % (m,) - print >> fd, ' _lasso.%s_%s_set(self._cptr, value)' % ( - klassname, mname) - print >> fd, ' %s = property(get_%s, set_%s)' % (mname, mname, mname) + print('W: unsupported setter for %s' % (m,), file=sys.stderr) + print(' _lasso.%s_%s_set(self._cptr, value)' % ( + klassname, mname), file=fd) + print(' %s = property(get_%s, set_%s)' % (mname, mname, mname), file=fd) old_mname = old_format_as_camelcase(m[1]) if mname != old_mname: - print >> fd, ' %s = %s' % (old_mname, mname) - print >> fd, '' + print(' %s = %s' % (old_mname, mname), file=fd) + print('', file=fd) # first pass on methods, getting accessors # second pass on methods, real methods @@ -454,7 +454,7 @@ if WSF_SUPPORT: m.name.endswith('_new_full'): continue if not m.name.startswith(method_prefix): - print >> sys.stderr, 'W:', m.name, 'vs', method_prefix + print('W:', m.name, 'vs', method_prefix, file=sys.stderr) continue if m.rename: @@ -491,7 +491,7 @@ if WSF_SUPPORT: if '=' in x: opt = True elif opt: - print >>sys.stderr, 'W: non-optional follow optional,', m + print('W: non-optional follow optional,', m, file=sys.stderr) if py_args: py_args = ', ' + ', '.join(py_args) @@ -502,51 +502,51 @@ if WSF_SUPPORT: else: c_args = '' - print >> fd, ' def %s(self%s):' % ( - format_underscore_as_camelcase(mname), py_args) + print(' def %s(self%s):' % ( + format_underscore_as_camelcase(mname), py_args), file=fd) if m.docstring: - print >> fd, " '''" - print >> fd, self.format_docstring(m, mname, 8) - print >> fd, " '''" + print(" '''", file=fd) + print(self.format_docstring(m, mname, 8), file=fd) + print(" '''", file=fd) if outarg: - print >> fd, " %s = list((None,))" % outvar + print(" %s = list((None,))" % outvar, file=fd) return_type = m.return_type return_type_qualifier = m.return_type_qualifier assert is_int(make_arg(return_type),self.binding_data) or not outarg if return_type in (None, 'void'): - print >> fd, ' _lasso.%s(self._cptr%s)' % ( - function_name, c_args) + print(' _lasso.%s(self._cptr%s)' % ( + function_name, c_args), file=fd) elif is_rc(m.return_arg): - print >> fd, ' rc = _lasso.%s(self._cptr%s)' % ( - function_name, c_args) - print >> fd, ' Error.raise_on_rc(rc)' + print(' rc = _lasso.%s(self._cptr%s)' % ( + function_name, c_args), file=fd) + print(' Error.raise_on_rc(rc)', file=fd) elif is_int(m.return_arg, self.binding_data) or is_xml_node(m.return_arg) or is_cstring(m.return_arg) or is_boolean(m.return_arg): - print >> fd, ' return _lasso.%s(self._cptr%s)' % ( - function_name, c_args) + print(' return _lasso.%s(self._cptr%s)' % ( + function_name, c_args), file=fd) elif is_glist(m.return_arg): el_type = element_type(m.return_arg) if is_object(el_type): - print >> fd, ' value = _lasso.%s(self._cptr%s)' % ( - function_name, c_args) - print >> fd, ' if value is not None:' - print >> fd, ' value = tuple([cptrToPy(x) for x in value])' - print >> fd, ' return value' + print(' value = _lasso.%s(self._cptr%s)' % ( + function_name, c_args), file=fd) + print(' if value is not None:', file=fd) + print(' value = tuple([cptrToPy(x) for x in value])', file=fd) + print(' return value', file=fd) elif is_cstring(el_type): - print >> fd, ' return _lasso.%s(self._cptr%s)' % ( - function_name, c_args) + print(' return _lasso.%s(self._cptr%s)' % ( + function_name, c_args), file=fd) else: raise Exception('Return Type GList<%s> is not supported' % el_type) elif is_hashtable(m.return_arg): raise Exception('Return type GHashTable unsupported') elif is_object(m.return_arg): - print >> fd, ' return cptrToPy(_lasso.%s(self._cptr%s))' % ( - function_name, c_args) + print(' return cptrToPy(_lasso.%s(self._cptr%s))' % ( + function_name, c_args), file=fd) else: raise Exception('Return type %s is unsupported' % (m.return_arg,)) if outarg: - print >> fd, ' return %s[0]' % outvar - print >> fd, '' + print(' return %s[0]' % outvar, file=fd) + print('', file=fd) # transform methods to properties for m in methods: if len(m.args) > 1: @@ -554,7 +554,7 @@ if WSF_SUPPORT: name = m.rename or m.name suffix = name[len(method_prefix)+len('get_'):] if clss.getMember(suffix): - print >>sys.stderr, 'W: method %s and member %s clashes' % (m.name, arg_name(clss.getMember(suffix))) + print('W: method %s and member %s clashes' % (m.name, arg_name(clss.getMember(suffix))), file=sys.stderr) continue if not name.startswith(method_prefix) or not name[len(method_prefix):].startswith('get_'): continue @@ -566,13 +566,13 @@ if WSF_SUPPORT: pname = format_as_camelcase(name[len(method_prefix)+len('get_'):]) fname = format_as_camelcase(name[len(method_prefix):]) if not setter: - print >> fd, ' %s = property(%s)' % (pname, fname) + print(' %s = property(%s)' % (pname, fname), file=fd) else: f2name = format_as_camelcase(setter.name[len(method_prefix):]) - print >> fd, ' %s = property(%s, %s)' % (pname, fname, f2name) + print(' %s = property(%s, %s)' % (pname, fname, f2name), file=fd) if empty: - print >> fd, ' pass' - print >> fd, '' + print(' pass', file=fd) + print('', file=fd) def format_docstring(self, func, method_name, indent): if func.args: @@ -591,7 +591,7 @@ if WSF_SUPPORT: return 'True' if var == 'FALSE': return 'False' - print >> sys.stderr, 'W: unknown docstring thingie: %s' % s.group(1) + print('W: unknown docstring thingie: %s' % s.group(1), file=sys.stderr) elif type == '@': if var == first_arg_name: var = 'self' @@ -676,14 +676,14 @@ if WSF_SUPPORT: else: name = m.name[6:] pname = format_as_camelcase(name) - print >> fd, '%s = _lasso.%s' % (pname, name) + print('%s = _lasso.%s' % (pname, name), file=fd) def generate_wrapper(self, fd): - print >> fd, open(os.path.join(self.src_dir,'wrapper_top.c')).read() + print(open(os.path.join(self.src_dir,'wrapper_top.c')).read(), file=fd) for h in self.binding_data.headers: - print >> fd, '#include <%s>' % h - print >> fd, '' + print('#include <%s>' % h, file=fd) + print('', file=fd) self.generate_constants_wrapper(fd) @@ -695,32 +695,32 @@ if WSF_SUPPORT: for m in c.methods: self.generate_function_wrapper(m, fd) self.generate_wrapper_list(fd) - print >> fd, open(os.path.join(self.src_dir,'wrapper_bottom.c')).read() + print(open(os.path.join(self.src_dir,'wrapper_bottom.c')).read(), file=fd) def generate_constants_wrapper(self, fd): - print >> fd, '''static void + print('''static void register_constants(PyObject *d) { PyObject *obj; -''' +''', file=fd) for c in self.binding_data.constants: if c[0] == 'i': - print >> fd, ' obj = PyInt_FromLong(%s);' % c[1] + print(' obj = PyInt_FromLong(%s);' % c[1], file=fd) elif c[0] == 's': - print >> fd, ' obj = PyString_FromString((char*)%s);' % c[1] + print(' obj = PyString_FromString((char*)%s);' % c[1], file=fd) elif c[0] == 'b': - print >> fd, '''\ + print('''\ #ifdef %s obj = Py_True; #else obj = Py_False; -#endif''' % c[1] +#endif''' % c[1], file=fd) else: - print >> sys.stderr, 'E: unknown constant type: %r' % c[0] - print >> fd, ' PyDict_SetItemString(d, "%s", obj);' % c[1][6:] - print >> fd, ' Py_DECREF(obj);' - print >> fd, '}' - print >> fd, '' + print('E: unknown constant type: %r' % c[0], file=sys.stderr) + print(' PyDict_SetItemString(d, "%s", obj);' % c[1][6:], file=fd) + print(' Py_DECREF(obj);', file=fd) + print('}', file=fd) + print('', file=fd) def generate_member_wrapper(self, c, fd): @@ -729,154 +729,154 @@ register_constants(PyObject *d) name = arg_name(m) mname = format_as_camelcase(arg_name(m)) # getter - print >> fd, '''static PyObject* + print('''static PyObject* %s_%s_get(G_GNUC_UNUSED PyObject *self, PyObject *args) -{''' % (klassname[5:], mname) +{''' % (klassname[5:], mname), file=fd) self.wrapper_list.append('%s_%s_get' % (klassname[5:], mname)) ftype = arg_type(m) if is_cstring(m): ftype = 'char*' - print >> fd, ' %s return_value;' % ftype - print >> fd, ' PyObject* return_pyvalue;' - print >> fd, ' PyGObjectPtr* cvt_this;' - print >> fd, ' %s* this;' % klassname - print >> fd, '' - print >> fd, ' if (! PyArg_ParseTuple(args, "O", &cvt_this)) return NULL;' - print >> fd, ' this = (%s*)cvt_this->obj;' % klassname - print >> fd, ' return_value = this->%s;' % arg_name(m) + print(' %s return_value;' % ftype, file=fd) + print(' PyObject* return_pyvalue;', file=fd) + print(' PyGObjectPtr* cvt_this;', file=fd) + print(' %s* this;' % klassname, file=fd) + print('', file=fd) + print(' if (! PyArg_ParseTuple(args, "O", &cvt_this)) return NULL;', file=fd) + print(' this = (%s*)cvt_this->obj;' % klassname, file=fd) + print(' return_value = this->%s;' % arg_name(m), file=fd) try: self.return_value(fd, m) except: - print >>sys.stderr, 'W: cannot make an assignment for', c, m + print('W: cannot make an assignment for', c, m, file=sys.stderr) raise - print >> fd, ' return return_pyvalue;' - print >> fd, '}' - print >> fd, '' + print(' return return_pyvalue;', file=fd) + print('}', file=fd) + print('', file=fd) # setter - print >> fd, '''static PyObject* + print('''static PyObject* %s_%s_set(G_GNUC_UNUSED PyObject *self, PyObject *args) -{''' % (klassname[5:], mname) +{''' % (klassname[5:], mname), file=fd) self.wrapper_list.append('%s_%s_set' % (klassname[5:], mname)) - print >> fd, ' PyGObjectPtr* cvt_this;' - print >> fd, ' %s* this;' % klassname + print(' PyGObjectPtr* cvt_this;', file=fd) + print(' %s* this;' % klassname, file=fd) type = m[0] # Determine type class if is_cstring(m): type = type.replace('const ', '') parse_format = 'z' parse_arg = '&value' - print >> fd, ' %s value;' % type + print(' %s value;' % type, file=fd) elif is_int(m, self.binding_data): parse_format = 'l' parse_arg = '&value' - print >> fd, ' long value;' + print(' long value;', file=fd) elif is_glist(m) or is_hashtable(m) or is_xml_node(m) or is_boolean(m): parse_format = 'O' - print >> fd, ' PyObject *cvt_value;' + print(' PyObject *cvt_value;', file=fd) parse_arg = '&cvt_value' elif is_object(m): parse_format = 'O' - print >> fd, ' PyGObjectPtr *cvt_value;' + print(' PyGObjectPtr *cvt_value;', file=fd) parse_arg = '&cvt_value' else: raise Exception('Unsupported field: %s' % (m,)) # Get GObject - print >> fd, ' if (! PyArg_ParseTuple(args, "O%s", &cvt_this, %s)) return NULL;' % ( - parse_format, parse_arg) - print >> fd, ' this = (%s*)cvt_this->obj;' % klassname + print(' if (! PyArg_ParseTuple(args, "O%s", &cvt_this, %s)) return NULL;' % ( + parse_format, parse_arg), file=fd) + print(' this = (%s*)cvt_this->obj;' % klassname, file=fd) # Change value if is_int(m, self.binding_data): - print >> fd, ' this->%s = value;' % name + print(' this->%s = value;' % name, file=fd) elif is_boolean(m): - print >> fd, ' this->%s = PyInt_AS_LONG(cvt_value) ? TRUE : FALSE;' % name + print(' this->%s = PyInt_AS_LONG(cvt_value) ? TRUE : FALSE;' % name, file=fd) elif is_cstring(m): - print >> fd, ' lasso_assign_string(this->%s, value);' % name + print(' lasso_assign_string(this->%s, value);' % name, file=fd) elif is_xml_node(m): - print >> fd, ' if (this->%s) xmlFreeNode(this->%s);' % (name, name) - print >> fd, ' this->%s = get_xml_node_from_pystring(cvt_value);' % name + print(' if (this->%s) xmlFreeNode(this->%s);' % (name, name), file=fd) + print(' this->%s = get_xml_node_from_pystring(cvt_value);' % name, file=fd) elif is_glist(m): el_type = element_type(m) if is_cstring(el_type): - print >> fd, ' set_list_of_strings(&this->%s, cvt_value);' % name + print(' set_list_of_strings(&this->%s, cvt_value);' % name, file=fd) elif is_xml_node(el_type): - print >> fd, ' set_list_of_xml_nodes(&this->%s, cvt_value);' % name + print(' set_list_of_xml_nodes(&this->%s, cvt_value);' % name, file=fd) elif is_object(el_type): - print >> fd, ' set_list_of_pygobject(&this->%s, cvt_value);' % name + print(' set_list_of_pygobject(&this->%s, cvt_value);' % name, file=fd) else: raise Exception('Unsupported setter for %s' % (m,)) elif is_hashtable(m): el_type = element_type(m) if is_object(el_type): - print >> fd, ' set_hashtable_of_pygobject(this->%s, cvt_value);' % name + print(' set_hashtable_of_pygobject(this->%s, cvt_value);' % name, file=fd) else: - print >> fd, ' set_hashtable_of_strings(this->%s, cvt_value);' % name + print(' set_hashtable_of_strings(this->%s, cvt_value);' % name, file=fd) elif is_object(m): - print >> fd, ' set_object_field((GObject**)&this->%s, cvt_value);' % name + print(' set_object_field((GObject**)&this->%s, cvt_value);' % name, file=fd) else: raise Exception('Unsupported member %s.%s' % (klassname, m)) - print >> fd, ' return noneRef();' - print >> fd, '}' - print >> fd, '' + print(' return noneRef();', file=fd) + print('}', file=fd) + print('', file=fd) def return_value(self, fd, arg, return_var_name = 'return_value', return_pyvar_name = 'return_pyvalue'): if is_boolean(arg): - print >> fd, ' if (%s) {' % return_var_name - print >> fd, ' Py_INCREF(Py_True);' - print >> fd, ' %s = Py_True;' % return_pyvar_name - print >> fd, ' } else {' - print >> fd, ' Py_INCREF(Py_False);' - print >> fd, ' %s = Py_False;' % return_pyvar_name - print >> fd, ' }' + print(' if (%s) {' % return_var_name, file=fd) + print(' Py_INCREF(Py_True);', file=fd) + print(' %s = Py_True;' % return_pyvar_name, file=fd) + print(' } else {', file=fd) + print(' Py_INCREF(Py_False);', file=fd) + print(' %s = Py_False;' % return_pyvar_name, file=fd) + print(' }', file=fd) elif is_int(arg, self.binding_data): - print >> fd, ' %s = PyInt_FromLong(%s);' % (return_pyvar_name, return_var_name) + print(' %s = PyInt_FromLong(%s);' % (return_pyvar_name, return_var_name), file=fd) elif is_cstring(arg) and is_transfer_full(arg): - print >> fd, ' if (%s) {' % return_var_name - print >> fd, ' %s = PyString_FromString(%s);' % (return_pyvar_name, return_var_name) - print >> fd, ' } else {' - print >> fd, ' %s = noneRef();' % return_pyvar_name - print >> fd, ' }' + print(' if (%s) {' % return_var_name, file=fd) + print(' %s = PyString_FromString(%s);' % (return_pyvar_name, return_var_name), file=fd) + print(' } else {', file=fd) + print(' %s = noneRef();' % return_pyvar_name, file=fd) + print(' }', file=fd) elif is_cstring(arg): - print >> fd, ' if (%s) {' % return_var_name - print >> fd, ' %s = PyString_FromString(%s);' % (return_pyvar_name, return_var_name) - print >> fd, ' } else {' - print >> fd, ' %s = noneRef();' % return_pyvar_name - print >> fd, ' }' + print(' if (%s) {' % return_var_name, file=fd) + print(' %s = PyString_FromString(%s);' % (return_pyvar_name, return_var_name), file=fd) + print(' } else {', file=fd) + print(' %s = noneRef();' % return_pyvar_name, file=fd) + print(' }', file=fd) elif is_glist(arg): el_type = element_type(arg) if is_object(el_type): - print >> fd, ' %s = get_list_of_pygobject(%s);' % (return_pyvar_name, return_var_name) + print(' %s = get_list_of_pygobject(%s);' % (return_pyvar_name, return_var_name), file=fd) elif is_cstring(el_type): - print >> fd, ' %s = get_list_of_strings(%s);' % (return_pyvar_name, return_var_name) + print(' %s = get_list_of_strings(%s);' % (return_pyvar_name, return_var_name), file=fd) elif is_xml_node(el_type): - print >> fd, ' %s = get_list_of_xml_nodes(%s);' % (return_pyvar_name, return_var_name) + print(' %s = get_list_of_xml_nodes(%s);' % (return_pyvar_name, return_var_name), file=fd) else: raise Exception('failed to make an assignment for %s' % (arg,)) elif is_hashtable(arg): el_type = element_type(arg) if is_object(el_type): - print >> fd, ' %s = get_dict_from_hashtable_of_objects(%s);' % (return_pyvar_name, return_var_name) + print(' %s = get_dict_from_hashtable_of_objects(%s);' % (return_pyvar_name, return_var_name), file=fd) else: - print >> fd, ' %s = get_dict_from_hashtable_of_strings(%s);' % (return_pyvar_name, return_var_name) + print(' %s = get_dict_from_hashtable_of_strings(%s);' % (return_pyvar_name, return_var_name), file=fd) elif is_xml_node(arg): # convert xmlNode* to strings - print >> fd, ' if (%s) {' % return_var_name - print >> fd, ' %s = get_pystring_from_xml_node(%s);' % (return_pyvar_name, return_var_name) - print >> fd, ' } else {' - print >> fd, ' %s = noneRef();' % return_pyvar_name - print >> fd, ' }' + print(' if (%s) {' % return_var_name, file=fd) + print(' %s = get_pystring_from_xml_node(%s);' % (return_pyvar_name, return_var_name), file=fd) + print(' } else {', file=fd) + print(' %s = noneRef();' % return_pyvar_name, file=fd) + print(' }', file=fd) elif is_object(arg): # return a PyGObjectPtr (wrapper around GObject) - print >> fd, '''\ + print('''\ if (%s) { %s = PyGObjectPtr_New(G_OBJECT(%s)); } else { %s = noneRef(); } -''' % (return_var_name, return_pyvar_name, return_var_name, return_pyvar_name) +''' % (return_var_name, return_pyvar_name, return_var_name, return_pyvar_name), file=fd) else: raise Exception('failed to make an assignment for %s' % (arg,)) @@ -888,9 +888,9 @@ register_constants(PyObject *d) else: name = m.name[6:] self.wrapper_list.append(name) - print >> fd, '''static PyObject* + print('''static PyObject* %s(G_GNUC_UNUSED PyObject *self, PyObject *args) -{''' % name +{''' % name, file=fd) parse_tuple_format = [] parse_tuple_args = [] for arg in m.args: @@ -939,22 +939,22 @@ register_constants(PyObject *d) parse_tuple_args.pop() parse_tuple_args.append('&cvt_%s_out' % aname) python_cvt_def = ' PyObject *cvt_%s_out = NULL;' % aname - print >> fd, ' PyObject *out_pyvalue = NULL;' - print >> fd, arg_def + print(' PyObject *out_pyvalue = NULL;', file=fd) + print(arg_def, file=fd) if python_cvt_def: - print >> fd, python_cvt_def + print(python_cvt_def, file=fd) if m.return_type: - print >> fd, ' %s return_value;' % m.return_type - print >> fd, ' PyObject* return_pyvalue = NULL;' - print >> fd, '' + print(' %s return_value;' % m.return_type, file=fd) + print(' PyObject* return_pyvalue = NULL;', file=fd) + print('', file=fd) parse_tuple_args = ', '.join(parse_tuple_args) if parse_tuple_args: parse_tuple_args = ', ' + parse_tuple_args - print >> fd, ' if (! PyArg_ParseTuple(args, "%s"%s)) return NULL;' % ( - ''.join(parse_tuple_format), parse_tuple_args) + print(' if (! PyArg_ParseTuple(args, "%s"%s)) return NULL;' % ( + ''.join(parse_tuple_format), parse_tuple_args), file=fd) for f, arg in zip([ x for x in parse_tuple_format if x != '|'], m.args): if is_out(arg): @@ -962,40 +962,40 @@ register_constants(PyObject *d) if is_list(arg): qualifier = element_type(arg) if is_cstring(qualifier): - print >> fd, ' set_list_of_strings(&%s, cvt_%s);' % (arg[1], arg[1]) + print(' set_list_of_strings(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd) elif qualifier == 'xmlNode*': - print >> fd, ' set_list_of_xml_nodes(&%s, cvt_%s);' % (arg[1], arg[1]) - elif isinstance(qualifier, basestring) and qualifier.startswith('Lasso'): - print >> fd, ' set_list_of_pygobject(&%s, cvt_%s);' % (arg[1], arg[1]) + print(' set_list_of_xml_nodes(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd) + elif isinstance(qualifier, str) and qualifier.startswith('Lasso'): + print(' set_list_of_pygobject(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd) else: - print >> sys.stderr, 'E: unqualified GList argument in', name, qualifier, arg + print('E: unqualified GList argument in', name, qualifier, arg, file=sys.stderr) elif is_xml_node(arg): - print >> fd, ' %s = get_xml_node_from_pystring(cvt_%s);' % (arg[1], arg[1]) + print(' %s = get_xml_node_from_pystring(cvt_%s);' % (arg[1], arg[1]), file=fd) elif is_time_t_pointer(arg): - print >> fd, ' %s = get_time_t(cvt_%s);' % (arg[1], arg[1]) + print(' %s = get_time_t(cvt_%s);' % (arg[1], arg[1]), file=fd) elif f == 'O': if is_optional(arg): - print >> fd, ' if (PyObject_TypeCheck((PyObject*)cvt_%s, &PyGObjectPtrType)) {' % arg[1] - print >> fd, ' %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]) - print >> fd, ' } else {' - print >> fd, ' %s = NULL;' % arg[1] - print >> fd, ' }' + print(' if (PyObject_TypeCheck((PyObject*)cvt_%s, &PyGObjectPtrType)) {' % arg[1], file=fd) + print(' %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]), file=fd) + print(' } else {', file=fd) + print(' %s = NULL;' % arg[1], file=fd) + print(' }', file=fd) else: - print >> fd, ' if (PyObject_TypeCheck((PyObject*)cvt_%s, &PyGObjectPtrType)) {' % arg[1] - print >> fd, ' %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]) - print >> fd, ' } else {' - print >> fd, ' PyErr_SetString(PyExc_TypeError, "value should be a PyGObject");' - print >> fd, ' return NULL;' - print >> fd, ' }' + print(' if (PyObject_TypeCheck((PyObject*)cvt_%s, &PyGObjectPtrType)) {' % arg[1], file=fd) + print(' %s = (%s)cvt_%s->obj;' % (arg[1], arg[0], arg[1]), file=fd) + print(' } else {', file=fd) + print(' PyErr_SetString(PyExc_TypeError, "value should be a PyGObject");', file=fd) + print(' return NULL;', file=fd) + print(' }', file=fd) if m.return_type: - print >> fd, ' return_value =', + print(' return_value =', file=fd) if 'new' in m.name: - print >> fd, '(%s)' % m.return_type, + print('(%s)' % m.return_type, file=fd) else: - print >> fd, ' ', - print >> fd, '%s(%s);' % (m.name, ', '.join([ref_name(x) for x in m.args])) + print(' ', file=fd) + print('%s(%s);' % (m.name, ', '.join([ref_name(x) for x in m.args])), file=fd) if m.return_type: # Constructor so decrease refcount (it was incremented by PyGObjectPtr_New called @@ -1003,7 +1003,7 @@ register_constants(PyObject *d) try: self.return_value(fd, m.return_arg) except: - print >>sys.stderr, 'W: cannot assign return value of', m + print('W: cannot assign return value of', m, file=sys.stderr) raise if is_transfer_full(m.return_arg, default=True): @@ -1011,33 +1011,33 @@ register_constants(PyObject *d) for f, arg in zip(parse_tuple_format, m.args): if is_out(arg): self.return_value(fd, arg, return_var_name = arg[1], return_pyvar_name = 'out_pyvalue') - print >> fd, ' PyList_SetItem(cvt_%s_out, 0, out_pyvalue);' % arg[1] + print(' PyList_SetItem(cvt_%s_out, 0, out_pyvalue);' % arg[1], file=fd) elif arg[0] == 'GList*': qualifier = arg[2].get('element-type') if qualifier == 'char*': - print >> fd, ' free_list(&%s, (GFunc)g_free);' % arg[1] + print(' free_list(&%s, (GFunc)g_free);' % arg[1], file=fd) elif qualifier == 'xmlNode*': - print >> fd, ' free_list(&%s, (GFunc)xmlFreeNode);' % arg[1] + print(' free_list(&%s, (GFunc)xmlFreeNode);' % arg[1], file=fd) elif qualifier == 'LassoNode': - print >> fd, ' free_list(&%s, (GFunc)g_object_unref);' % arg[1] + print(' free_list(&%s, (GFunc)g_object_unref);' % arg[1], file=fd) elif is_time_t_pointer(arg): - print >> fd, ' if (%s) free(%s);' % (arg[1], arg[1]) + print(' if (%s) free(%s);' % (arg[1], arg[1]), file=fd) elif not is_transfer_full(arg) and is_xml_node(arg): self.free_value(fd, arg) if not m.return_type: - print >> fd, ' return noneRef();' + print(' return noneRef();', file=fd) else: - print >> fd, ' return return_pyvalue;' - print >> fd, '}' - print >> fd, '' + print(' return return_pyvalue;', file=fd) + print('}', file=fd) + print('', file=fd) def generate_wrapper_list(self, fd): - print >> fd, ''' -static PyMethodDef lasso_methods[] = {''' + print(''' +static PyMethodDef lasso_methods[] = {''', file=fd) for m in self.wrapper_list: - print >> fd, ' {"%s", %s, METH_VARARGS, NULL},' % (m, m) - print >> fd, ' {NULL, NULL, 0, NULL}' - print >> fd, '};' - print >> fd, '' + print(' {"%s", %s, METH_VARARGS, NULL},' % (m, m), file=fd) + print(' {NULL, NULL, 0, NULL}', file=fd) + print('};', file=fd) + print('', file=fd) diff --git a/bindings/utils.py b/bindings/utils.py index 6c990572..c7a01773 100644 --- a/bindings/utils.py +++ b/bindings/utils.py @@ -60,13 +60,13 @@ def camelcase_to_list(varname): else: l[-1].append(x) last = x - return map(str.lower,map(''.join,l)) + return list(map(str.lower,map(''.join,l))) def old_format_as_camelcase(var): '''Format an identifier name into CamelCase''' if '_' in var: return format_underscore_as_camelcase(var) - if var[0] in string.uppercase: + if var[0] in string.ascii_uppercase: var = var[0].lower() + var[1:] var = re.sub(r'([a-z])(ID)([A-Z]|$)', r'\1Id\3', var) # replace standing ID by Id return var @@ -75,10 +75,10 @@ def format_as_camelcase(var): '''Format an identifier name into CamelCase''' if var[0].isupper(): l = camelcase_to_list(var) - return l[0] + ''.join(map(str.capitalize, l[1:])) + return l[0] + ''.join(list(map(str.capitalize, l[1:]))) if '_' in var: return format_underscore_as_camelcase(var) - if var[0] in string.uppercase: + if var[0] in string.ascii_uppercase: var = var[0].lower() + var[1:] var = re.sub(r'([a-z])(ID)([A-Z]|$)', r'\1Id\3', var) # replace standing ID by Id return var @@ -125,7 +125,7 @@ def pgroup(group,prev): if isinstance(x,tuple): pgroup(x,prefix) else: - print prefix * ' ' + x[prefix:] + print(prefix * ' ' + x[prefix:]) i = i + 1 def group(list): @@ -271,7 +271,7 @@ _not_objects = ( 'GHashTable', 'GList', 'GType' ) def is_object(arg): t = clean_type(unconstify(arg_type(arg))) - return t and t[0] in string.uppercase and not [ x for x in _not_objects if x in t ] + return t and t[0] in string.ascii_uppercase and not [ x for x in _not_objects if x in t ] if __name__ == '__main__': - print camelcase_to_list('Samlp2IDPList') + print(camelcase_to_list('Samlp2IDPList'))