Python 3: Fix Python 2 support (use six.print_)

This commit is contained in:
Houzéfa Abbasbhay 2014-12-01 16:53:07 +01:00 committed by Benjamin Dauvergne
parent 32a4049cdc
commit 4c79280ed8
3 changed files with 216 additions and 210 deletions

View File

@ -26,6 +26,10 @@ In all cases you need to install development packages for dependencies. On Debia
aptitude install libxml2-dev libxslt1-dev libxmlsec1-dev libxmlsec1-openssl \
libglib2.0-dev python-all-dev fastjar php5-dev php5-cli python-lxml
Python bindings require the "six" library:
pip install six
Basic Installation
==================

View File

@ -23,6 +23,7 @@
import os
import re
from six import print_
import sys
from utils import *
@ -240,7 +241,7 @@ class Function:
try:
arg = [x for x in self.args if x[1] == param.attrib.get('name')][0]
except IndexError:
print('W: no such param (%s) in function (%s)' % (
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':
@ -470,7 +471,7 @@ def parse_header(header_file):
if not in_struct.name in binding.structs_toskip:
binding.structs.append(in_struct)
else:
print('W: skipping structure %s due to overrides.xml' % in_struct.name, file=sys.stderr)
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 +491,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('W: skipping field %s.%s' % (in_struct.name, member_name), file=sys.stderr)
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 +540,13 @@ def parse_header(header_file):
type = clean_type(type)
f.args.append(list((type, name, {})))
else:
print('failed to process:', arg, 'in line:', line, file=sys.stderr)
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('W: skipping function', f, file=sys.stderr)
print_('W: skipping function', f, file=sys.stderr)
i += 1

View File

@ -19,6 +19,7 @@
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import os
from six import print_
import sys
import re
import textwrap
@ -33,9 +34,9 @@ def remove_bad_optional(args):
if not '=' in x:
non_opt = True
elif non_opt:
print('W: changed', x, file=sys.stderr)
print_('W: changed', x, file=sys.stderr)
x = re.sub(' *=.*', '', x)
print('to', x, file=sys.stderr)
print_('to', x, file=sys.stderr)
new_args.append(x)
new_args.reverse()
return new_args
@ -72,23 +73,23 @@ class Binding:
if not name:
raise Exception('Cannot free, missing a name')
if is_cstring(type):
print(' lasso_release_string(%s);' % name, file=fd)
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(' lasso_release_xml_node(%s);' % name, file=fd)
print_(' lasso_release_xml_node(%s);' % name, file=fd)
elif is_glist(type):
etype = element_type(type)
if is_cstring(etype):
print(' lasso_release_list_of_strings(%s);' % name, file=fd)
print_(' lasso_release_list_of_strings(%s);' % name, file=fd)
elif is_object(etype):
print(' lasso_release_list_of_gobjects(%s);' % name, file=fd)
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(' if (return_value) g_object_unref(%s);' % name, file=fd)
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 +110,7 @@ class Binding:
fd.close()
def generate_header(self, fd):
print('''\
print_('''\
# this file has been generated automatically; do not edit
import _lasso
@ -159,7 +160,7 @@ class frozendict(dict):
def generate_exceptions(self, fd):
done_cats = []
print('''\
print_('''\
class Error(Exception):
code = None
@ -190,7 +191,7 @@ class Error(Exception):
cat = exc_cat.attrib.get('name')
done_cats.append(cat)
parent_cat = exc_cat.attrib.get('parent', '')
print('''\
print_('''\
class %sError(%sError):
pass
''' % (cat, parent_cat), file=fd)
@ -213,7 +214,7 @@ class %sError(%sError):
else:
parent_cat = ''
print('''\
print_('''\
class %sError(%sError):
pass
''' % (cat, parent_cat), file=fd)
@ -231,19 +232,19 @@ class %sError(%sError):
# ordering would change)
continue
print('''\
print_('''\
class %sError(%sError):
pass
''' % (detail, cat), file=fd)
print('exceptions_dict = {', file=fd)
print_('exceptions_dict = {', file=fd)
for k, v in exceptions_dict.items():
print(' _lasso.%s: %sError,' % (v, k), file=fd)
print('}', file=fd)
print('', file=fd)
print_(' _lasso.%s: %sError,' % (v, k), file=fd)
print_('}', file=fd)
print_('', file=fd)
def generate_footer(self, fd):
print('''
print_('''
import lasso
@ -294,19 +295,19 @@ if WSF_SUPPORT:
''', file=fd)
def generate_constants(self, fd):
print('### Constants (both enums and defines)', file=fd)
print_('### Constants (both enums and defines)', file=fd)
for c in self.binding_data.constants:
print('%s = _lasso.%s' % (c[1][6:], c[1][6:]), file=fd)
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('%s = True' % name, file=fd)
print_('%s = True' % name, file=fd)
else:
print('E: unknown value for constant: %r' % value, file=sys.stderr)
print('', file=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 +316,7 @@ if WSF_SUPPORT:
else:
parentname = clss.parent[5:]
print('''class %(klassname)s(%(parentname)s):''' % locals(), file=fd)
print_('''class %(klassname)s(%(parentname)s):''' % locals(), file=fd)
methods = clss.methods[:]
# constructor(s)
@ -338,14 +339,14 @@ if WSF_SUPPORT:
c_args = ', '.join(c_args)
py_args = ', ' + ', '.join(py_args)
print(' def __init__(self%s):' % py_args, file=fd)
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(' self._cptr = _lasso.%s(%s)' % (
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)
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 +370,13 @@ if WSF_SUPPORT:
if '=' in x:
opt = True
elif opt:
print('W: non-optional follows optional,', m, file=sys.stderr)
print_('W: non-optional follows optional,', m, file=sys.stderr)
c_args = ', '.join(c_args)
py_args = ', ' + ', '.join(py_args)
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)
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 +384,13 @@ if WSF_SUPPORT:
mname = format_as_camelcase(m[1])
options = m[2]
# getter
print(' def get_%s(self):' % mname, file=fd)
print(' t = _lasso.%s_%s_get(self._cptr)' % (
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(' t = cptrToPy(t)', file=fd)
print_(' t = cptrToPy(t)', file=fd)
elif is_glist(m):
el_type = element_type(m)
if is_cstring(el_type):
@ -397,54 +398,54 @@ if WSF_SUPPORT:
elif is_xml_node(el_type):
pass
elif is_object(el_type):
print(' if not t: return t', file=fd)
print(' t = tuple([cptrToPy(x) for x in t])', file=fd)
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(' if not t: return t', file=fd)
print_(' if not t: return t', file=fd)
if is_object(el_type):
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)
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(' t = frozendict(t)', file=fd)
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(' return t;', file=fd)
print_(' return t;', file=fd)
# setter
print(' def set_%s(self, value):' % mname, file=fd)
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(' value = str2lasso(value)', file=fd)
print_(' value = str2lasso(value)', file=fd)
elif is_object(m):
print(' if value is not None:', file=fd)
print(' value = value and value._cptr', file=fd)
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(' if value is not None:', file=fd)
print(' value = tuple([x._cptr for x in value])', file=fd)
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('W: unsupported setter for hashtable %s' % (m,), file=sys.stderr)
print_('W: unsupported setter for hashtable %s' % (m,), file=sys.stderr)
else:
print('W: unsupported setter for %s' % (m,), file=sys.stderr)
print(' _lasso.%s_%s_set(self._cptr, value)' % (
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)
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(' %s = %s' % (old_mname, mname), file=fd)
print('', file=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 +455,7 @@ if WSF_SUPPORT:
m.name.endswith('_new_full'):
continue
if not m.name.startswith(method_prefix):
print('W:', m.name, 'vs', method_prefix, file=sys.stderr)
print_('W:', m.name, 'vs', method_prefix, file=sys.stderr)
continue
if m.rename:
@ -491,7 +492,7 @@ if WSF_SUPPORT:
if '=' in x:
opt = True
elif opt:
print('W: non-optional follow optional,', m, file=sys.stderr)
print_('W: non-optional follow optional,', m, file=sys.stderr)
if py_args:
py_args = ', ' + ', '.join(py_args)
@ -502,51 +503,51 @@ if WSF_SUPPORT:
else:
c_args = ''
print(' def %s(self%s):' % (
print_(' def %s(self%s):' % (
format_underscore_as_camelcase(mname), py_args), file=fd)
if m.docstring:
print(" '''", file=fd)
print(self.format_docstring(m, mname, 8), file=fd)
print(" '''", file=fd)
print_(" '''", file=fd)
print_(self.format_docstring(m, mname, 8), file=fd)
print_(" '''", file=fd)
if outarg:
print(" %s = list((None,))" % outvar, file=fd)
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(' _lasso.%s(self._cptr%s)' % (
print_(' _lasso.%s(self._cptr%s)' % (
function_name, c_args), file=fd)
elif is_rc(m.return_arg):
print(' rc = _lasso.%s(self._cptr%s)' % (
print_(' rc = _lasso.%s(self._cptr%s)' % (
function_name, c_args), file=fd)
print(' Error.raise_on_rc(rc)', 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(' return _lasso.%s(self._cptr%s)' % (
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(' value = _lasso.%s(self._cptr%s)' % (
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)
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(' return _lasso.%s(self._cptr%s)' % (
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(' return cptrToPy(_lasso.%s(self._cptr%s))' % (
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(' return %s[0]' % outvar, file=fd)
print('', file=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 +555,7 @@ if WSF_SUPPORT:
name = m.rename or m.name
suffix = name[len(method_prefix)+len('get_'):]
if clss.getMember(suffix):
print('W: method %s and member %s clashes' % (m.name, arg_name(clss.getMember(suffix))), file=sys.stderr)
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 +567,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(' %s = property(%s)' % (pname, fname), file=fd)
print_(' %s = property(%s)' % (pname, fname), file=fd)
else:
f2name = format_as_camelcase(setter.name[len(method_prefix):])
print(' %s = property(%s, %s)' % (pname, fname, f2name), file=fd)
print_(' %s = property(%s, %s)' % (pname, fname, f2name), file=fd)
if empty:
print(' pass', file=fd)
print('', file=fd)
print_(' pass', file=fd)
print_('', file=fd)
def format_docstring(self, func, method_name, indent):
if func.args:
@ -591,7 +592,7 @@ if WSF_SUPPORT:
return 'True'
if var == 'FALSE':
return 'False'
print('W: unknown docstring thingie: %s' % s.group(1), file=sys.stderr)
print_('W: unknown docstring thingie: %s' % s.group(1), file=sys.stderr)
elif type == '@':
if var == first_arg_name:
var = 'self'
@ -676,14 +677,14 @@ if WSF_SUPPORT:
else:
name = m.name[6:]
pname = format_as_camelcase(name)
print('%s = _lasso.%s' % (pname, name), file=fd)
print_('%s = _lasso.%s' % (pname, name), file=fd)
def generate_wrapper(self, fd):
print(open(os.path.join(self.src_dir,'wrapper_top.c')).read(), file=fd)
print_(open(os.path.join(self.src_dir,'wrapper_top.c')).read(), file=fd)
for h in self.binding_data.headers:
print('#include <%s>' % h, file=fd)
print('', file=fd)
print_('#include <%s>' % h, file=fd)
print_('', file=fd)
self.generate_constants_wrapper(fd)
@ -695,32 +696,32 @@ if WSF_SUPPORT:
for m in c.methods:
self.generate_function_wrapper(m, fd)
self.generate_wrapper_list(fd)
print(open(os.path.join(self.src_dir,'wrapper_bottom.c')).read(), file=fd)
print_(open(os.path.join(self.src_dir,'wrapper_bottom.c')).read(), file=fd)
def generate_constants_wrapper(self, fd):
print('''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(' obj = PyInt_FromLong(%s);' % c[1], file=fd)
print_(' obj = PyInt_FromLong(%s);' % c[1], file=fd)
elif c[0] == 's':
print(' obj = PyString_FromString((char*)%s);' % c[1], file=fd)
print_(' obj = PyString_FromString((char*)%s);' % c[1], file=fd)
elif c[0] == 'b':
print('''\
print_('''\
#ifdef %s
obj = Py_True;
#else
obj = Py_False;
#endif''' % c[1], file=fd)
else:
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)
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,7 +730,7 @@ register_constants(PyObject *d)
name = arg_name(m)
mname = format_as_camelcase(arg_name(m))
# getter
print('''static PyObject*
print_('''static PyObject*
%s_%s_get(G_GNUC_UNUSED PyObject *self, PyObject *args)
{''' % (klassname[5:], mname), file=fd)
self.wrapper_list.append('%s_%s_get' % (klassname[5:], mname))
@ -737,140 +738,140 @@ register_constants(PyObject *d)
ftype = arg_type(m)
if is_cstring(m):
ftype = 'char*'
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)
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('W: cannot make an assignment for', c, m, file=sys.stderr)
print_('W: cannot make an assignment for', c, m, file=sys.stderr)
raise
print(' return return_pyvalue;', file=fd)
print('}', file=fd)
print('', file=fd)
print_(' return return_pyvalue;', file=fd)
print_('}', file=fd)
print_('', file=fd)
# setter
print('''static PyObject*
print_('''static PyObject*
%s_%s_set(G_GNUC_UNUSED PyObject *self, PyObject *args)
{''' % (klassname[5:], mname), file=fd)
self.wrapper_list.append('%s_%s_set' % (klassname[5:], mname))
print(' PyGObjectPtr* cvt_this;', file=fd)
print(' %s* this;' % klassname, file=fd)
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(' %s value;' % type, file=fd)
print_(' %s value;' % type, file=fd)
elif is_int(m, self.binding_data):
parse_format = 'l'
parse_arg = '&value'
print(' long value;', file=fd)
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(' PyObject *cvt_value;', file=fd)
print_(' PyObject *cvt_value;', file=fd)
parse_arg = '&cvt_value'
elif is_object(m):
parse_format = 'O'
print(' PyGObjectPtr *cvt_value;', file=fd)
print_(' PyGObjectPtr *cvt_value;', file=fd)
parse_arg = '&cvt_value'
else:
raise Exception('Unsupported field: %s' % (m,))
# Get GObject
print(' if (! PyArg_ParseTuple(args, "O%s", &cvt_this, %s)) return NULL;' % (
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)
print_(' this = (%s*)cvt_this->obj;' % klassname, file=fd)
# Change value
if is_int(m, self.binding_data):
print(' this->%s = value;' % name, file=fd)
print_(' this->%s = value;' % name, file=fd)
elif is_boolean(m):
print(' this->%s = PyInt_AS_LONG(cvt_value) ? TRUE : FALSE;' % name, file=fd)
print_(' this->%s = PyInt_AS_LONG(cvt_value) ? TRUE : FALSE;' % name, file=fd)
elif is_cstring(m):
print(' lasso_assign_string(this->%s, value);' % name, file=fd)
print_(' lasso_assign_string(this->%s, value);' % name, file=fd)
elif is_xml_node(m):
print(' if (this->%s) xmlFreeNode(this->%s);' % (name, name), file=fd)
print(' this->%s = get_xml_node_from_pystring(cvt_value);' % name, file=fd)
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(' set_list_of_strings(&this->%s, cvt_value);' % name, file=fd)
print_(' set_list_of_strings(&this->%s, cvt_value);' % name, file=fd)
elif is_xml_node(el_type):
print(' set_list_of_xml_nodes(&this->%s, cvt_value);' % name, file=fd)
print_(' set_list_of_xml_nodes(&this->%s, cvt_value);' % name, file=fd)
elif is_object(el_type):
print(' set_list_of_pygobject(&this->%s, cvt_value);' % name, file=fd)
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(' set_hashtable_of_pygobject(this->%s, cvt_value);' % name, file=fd)
print_(' set_hashtable_of_pygobject(this->%s, cvt_value);' % name, file=fd)
else:
print(' set_hashtable_of_strings(this->%s, cvt_value);' % name, file=fd)
print_(' set_hashtable_of_strings(this->%s, cvt_value);' % name, file=fd)
elif is_object(m):
print(' set_object_field((GObject**)&this->%s, cvt_value);' % name, file=fd)
print_(' set_object_field((GObject**)&this->%s, cvt_value);' % name, file=fd)
else:
raise Exception('Unsupported member %s.%s' % (klassname, m))
print(' return noneRef();', file=fd)
print('}', file=fd)
print('', file=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(' 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)
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(' %s = PyInt_FromLong(%s);' % (return_pyvar_name, return_var_name), file=fd)
print_(' %s = PyInt_FromLong(%s);' % (return_pyvar_name, return_var_name), file=fd)
elif is_cstring(arg) and is_transfer_full(arg):
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)
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(' 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)
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(' %s = get_list_of_pygobject(%s);' % (return_pyvar_name, return_var_name), file=fd)
print_(' %s = get_list_of_pygobject(%s);' % (return_pyvar_name, return_var_name), file=fd)
elif is_cstring(el_type):
print(' %s = get_list_of_strings(%s);' % (return_pyvar_name, return_var_name), file=fd)
print_(' %s = get_list_of_strings(%s);' % (return_pyvar_name, return_var_name), file=fd)
elif is_xml_node(el_type):
print(' %s = get_list_of_xml_nodes(%s);' % (return_pyvar_name, return_var_name), file=fd)
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(' %s = get_dict_from_hashtable_of_objects(%s);' % (return_pyvar_name, return_var_name), file=fd)
print_(' %s = get_dict_from_hashtable_of_objects(%s);' % (return_pyvar_name, return_var_name), file=fd)
else:
print(' %s = get_dict_from_hashtable_of_strings(%s);' % (return_pyvar_name, return_var_name), file=fd)
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(' 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)
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('''\
print_('''\
if (%s) {
%s = PyGObjectPtr_New(G_OBJECT(%s));
} else {
@ -888,7 +889,7 @@ register_constants(PyObject *d)
else:
name = m.name[6:]
self.wrapper_list.append(name)
print('''static PyObject*
print_('''static PyObject*
%s(G_GNUC_UNUSED PyObject *self, PyObject *args)
{''' % name, file=fd)
parse_tuple_format = []
@ -939,21 +940,21 @@ 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(' PyObject *out_pyvalue = NULL;', file=fd)
print(arg_def, file=fd)
print_(' PyObject *out_pyvalue = NULL;', file=fd)
print_(arg_def, file=fd)
if python_cvt_def:
print(python_cvt_def, file=fd)
print_(python_cvt_def, file=fd)
if m.return_type:
print(' %s return_value;' % m.return_type, file=fd)
print(' PyObject* return_pyvalue = NULL;', file=fd)
print('', file=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(' if (! PyArg_ParseTuple(args, "%s"%s)) return NULL;' % (
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):
@ -962,40 +963,40 @@ register_constants(PyObject *d)
if is_list(arg):
qualifier = element_type(arg)
if is_cstring(qualifier):
print(' set_list_of_strings(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
print_(' set_list_of_strings(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
elif qualifier == 'xmlNode*':
print(' set_list_of_xml_nodes(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
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)
print_(' set_list_of_pygobject(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
else:
print('E: unqualified GList argument in', name, qualifier, arg, file=sys.stderr)
print_('E: unqualified GList argument in', name, qualifier, arg, file=sys.stderr)
elif is_xml_node(arg):
print(' %s = get_xml_node_from_pystring(cvt_%s);' % (arg[1], arg[1]), file=fd)
print_(' %s = get_xml_node_from_pystring(cvt_%s);' % (arg[1], arg[1]), file=fd)
elif is_time_t_pointer(arg):
print(' %s = get_time_t(cvt_%s);' % (arg[1], arg[1]), file=fd)
print_(' %s = get_time_t(cvt_%s);' % (arg[1], arg[1]), file=fd)
elif f == 'O':
if is_optional(arg):
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)
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(' 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)
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(' return_value =', file=fd)
print_(' return_value =', file=fd)
if 'new' in m.name:
print('(%s)' % m.return_type, file=fd)
print_('(%s)' % m.return_type, file=fd)
else:
print(' ', file=fd)
print('%s(%s);' % (m.name, ', '.join([ref_name(x) for x in m.args])), file=fd)
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 +1004,7 @@ register_constants(PyObject *d)
try:
self.return_value(fd, m.return_arg)
except:
print('W: cannot assign return value of', m, file=sys.stderr)
print_('W: cannot assign return value of', m, file=sys.stderr)
raise
if is_transfer_full(m.return_arg, default=True):
@ -1011,33 +1012,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(' PyList_SetItem(cvt_%s_out, 0, out_pyvalue);' % arg[1], file=fd)
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(' free_list(&%s, (GFunc)g_free);' % arg[1], file=fd)
print_(' free_list(&%s, (GFunc)g_free);' % arg[1], file=fd)
elif qualifier == 'xmlNode*':
print(' free_list(&%s, (GFunc)xmlFreeNode);' % arg[1], file=fd)
print_(' free_list(&%s, (GFunc)xmlFreeNode);' % arg[1], file=fd)
elif qualifier == 'LassoNode':
print(' free_list(&%s, (GFunc)g_object_unref);' % arg[1], file=fd)
print_(' free_list(&%s, (GFunc)g_object_unref);' % arg[1], file=fd)
elif is_time_t_pointer(arg):
print(' if (%s) free(%s);' % (arg[1], arg[1]), file=fd)
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(' return noneRef();', file=fd)
print_(' return noneRef();', file=fd)
else:
print(' return return_pyvalue;', file=fd)
print('}', file=fd)
print('', file=fd)
print_(' return return_pyvalue;', file=fd)
print_('}', file=fd)
print_('', file=fd)
def generate_wrapper_list(self, fd):
print('''
print_('''
static PyMethodDef lasso_methods[] = {''', file=fd)
for m in self.wrapper_list:
print(' {"%s", %s, METH_VARARGS, NULL},' % (m, m), file=fd)
print(' {NULL, NULL, 0, NULL}', file=fd)
print('};', file=fd)
print('', file=fd)
print_(' {"%s", %s, METH_VARARGS, NULL},' % (m, m), file=fd)
print_(' {NULL, NULL, 0, NULL}', file=fd)
print_('};', file=fd)
print_('', file=fd)