Commit 05037ee0 authored by Kjell Ahlstedt's avatar Kjell Ahlstedt
Browse files

Port tools/defs_gen to Python 3.

* .gitignore: Ignore /tools/defs_gen/__pycache__/.
* tools/defs_gen/*.py: Port to Python 3. These Python sources now run with
both Python 2 and Python 3. Tested with 2.7.3 and 3.2.3.
Bug #690969.
parent b54f8011
......@@ -160,6 +160,7 @@ giommconfig.h
# tools/
/tools/defs_gen/*.pyc
/tools/defs_gen/__pycache__/
/tools/extra_defs_gen/generate_defs_gio
/tools/extra_defs_gen/generate_defs_glib
/tools/generate_wrap_init.pl
......
......@@ -210,7 +210,7 @@ class EnumDef(Definition):
class FlagsDef(EnumDef):
def __init__(self, *args):
apply(EnumDef.__init__, (self,) + args)
EnumDef.__init__(*(self,) + args)
self.deftype = 'flags'
class BoxedDef(Definition):
......@@ -463,8 +463,8 @@ class FunctionDef(Definition):
self.params.append(Parameter(ptype, pname, pdflt, pnull))
elif arg[0] == 'properties':
if self.is_constructor_of is None:
print >> sys.stderr, "Warning: (properties ...) "\
"is only valid for constructors"
sys.stderr.write("Warning: (properties ...) "\
"is only valid for constructors")
for prop in arg[1:]:
pname = prop[0]
optional = False
......@@ -516,7 +516,7 @@ class FunctionDef(Definition):
raise RuntimeError("could not find %s in old_parameters %r" % (
param.pname, [p.pname for p in old.params]))
try:
self.params = map(merge_param, self.params)
self.params = list(map(merge_param, self.params))
except RuntimeError:
# parameter names changed and we can't find a match; it's
# safer to keep the old parameter list untouched.
......
......@@ -47,54 +47,54 @@ class DefsParser(IncludeParser):
self.defines = defines # -Dfoo=bar options, as dictionary
def define_object(self, *args):
odef = apply(ObjectDef, args)
odef = ObjectDef(*args)
self.objects.append(odef)
self.c_name[odef.c_name] = odef
def define_interface(self, *args):
idef = apply(InterfaceDef, args)
idef = InterfaceDef(*args)
self.interfaces.append(idef)
self.c_name[idef.c_name] = idef
def define_enum(self, *args):
edef = apply(EnumDef, args)
edef = EnumDef(*args)
self.enums.append(edef)
self.c_name[edef.c_name] = edef
def define_flags(self, *args):
fdef = apply(FlagsDef, args)
fdef = FlagsDef(*args)
self.enums.append(fdef)
self.c_name[fdef.c_name] = fdef
def define_boxed(self, *args):
bdef = apply(BoxedDef, args)
bdef = BoxedDef(*args)
self.boxes.append(bdef)
self.c_name[bdef.c_name] = bdef
def define_pointer(self, *args):
pdef = apply(PointerDef, args)
pdef = PointerDef(*args)
self.pointers.append(pdef)
self.c_name[pdef.c_name] = pdef
def define_function(self, *args):
fdef = apply(FunctionDef, args)
fdef = FunctionDef(*args)
self.functions.append(fdef)
self.c_name[fdef.c_name] = fdef
def define_method(self, *args):
mdef = apply(MethodDef, args)
mdef = MethodDef(*args)
self.functions.append(mdef)
self.c_name[mdef.c_name] = mdef
def define_virtual(self, *args):
vdef = apply(VirtualDef, args)
vdef = VirtualDef(*args)
self.virtuals.append(vdef)
def merge(self, old, parmerge):
for obj in self.objects:
if old.c_name.has_key(obj.c_name):
if obj.c_name in old.c_name:
obj.merge(old.c_name[obj.c_name])
for f in self.functions:
if old.c_name.has_key(f.c_name):
if f.c_name in old.c_name:
f.merge(old.c_name[f.c_name], parmerge)
def printMissing(self, old):
for obj in self.objects:
if not old.c_name.has_key(obj.c_name):
if obj.c_name not in old.c_name:
obj.write_defs()
for f in self.functions:
if not old.c_name.has_key(f.c_name):
if f.c_name not in old.c_name:
f.write_defs()
def write_defs(self, fp=sys.stdout):
......@@ -125,18 +125,18 @@ class DefsParser(IncludeParser):
def find_methods(self, obj):
objname = obj.c_name
return filter(lambda func, on=objname: isinstance(func, MethodDef) and
func.of_object == on, self.functions)
return list(filter(lambda func, on=objname: isinstance(func, MethodDef) and
func.of_object == on, self.functions))
def find_virtuals(self, obj):
objname = obj.c_name
retval = filter(lambda func, on=objname: isinstance(func, VirtualDef) and
func.of_object == on, self.virtuals)
retval = list(filter(lambda func, on=objname: isinstance(func, VirtualDef) and
func.of_object == on, self.virtuals))
return retval
def find_functions(self):
return filter(lambda func: isinstance(func, FunctionDef) and
not func.is_constructor_of, self.functions)
return [func for func in self.functions if isinstance(func, FunctionDef) and
not func.is_constructor_of]
def ifdef(self, *args):
if args[0] in self.defines:
......
......@@ -2,7 +2,7 @@
'''Simple module for extracting GNOME style doc comments from C
sources, so I can use them for other purposes.'''
import sys, os, string, re
import sys, os, re
# Used to tell if the "Since: ..." portion of the gtkdoc function description
# should be omitted. This is useful for some C++ modules such as gstreamermm
......@@ -483,10 +483,10 @@ def parse_tmpl(fp, doc_dict):
cur_doc = None # don't worry about unused params.
elif cur_doc:
if line[:10] == '@Returns: ':
if string.strip(line[10:]):
if line[10:].strip():
cur_doc.append_to_return(line[10:])
elif line[0] == '@':
pos = string.find(line, ':')
pos = line.find(':')
if pos >= 0:
cur_doc.append_to_named_param(line[1:pos], line[pos+1:])
else:
......
......@@ -8,7 +8,6 @@
import getopt
import re
import string
import sys
import docextract
......@@ -30,35 +29,35 @@ def escape_text(unescaped_text):
escaped_text = re.sub(r'&(?![A-Za-z]+;)', '&', unescaped_text)
# These weird entities turn up in the output...
escaped_text = string.replace(escaped_text, '—', '—')
escaped_text = string.replace(escaped_text, '*', '*')
escaped_text = string.replace(escaped_text, '%', '%')
escaped_text = string.replace(escaped_text, '@', '@')
escaped_text = string.replace(escaped_text, ':', ':')
escaped_text = string.replace(escaped_text, '#', '#')
escaped_text = string.replace(escaped_text, ' ', ' ')
escaped_text = string.replace(escaped_text, '&solidus;', '/')
escaped_text = string.replace(escaped_text, 'π', '∏')
escaped_text = string.replace(escaped_text, '⇒', '⇒')
escaped_text = escaped_text.replace('—', '—')
escaped_text = escaped_text.replace('*', '*')
escaped_text = escaped_text.replace('%', '%')
escaped_text = escaped_text.replace('@', '@')
escaped_text = escaped_text.replace(':', ':')
escaped_text = escaped_text.replace('#', '#')
escaped_text = escaped_text.replace(' ', ' ')
escaped_text = escaped_text.replace('&solidus;', '/')
escaped_text = escaped_text.replace('π', '∏')
escaped_text = escaped_text.replace('⇒', '⇒')
# This represents a '/' before or after an '*' so replace with slash but
# with spaces.
escaped_text = string.replace(escaped_text, '/', ' / ')
escaped_text = escaped_text.replace('/', ' / ')
# Escape for both tag contents and attribute values
escaped_text = string.replace(escaped_text, '<', '&lt;')
escaped_text = string.replace(escaped_text, '>', '&gt;')
escaped_text = string.replace(escaped_text, '"', '&quot;')
escaped_text = escaped_text.replace('<', '&lt;')
escaped_text = escaped_text.replace('>', '&gt;')
escaped_text = escaped_text.replace('"', '&quot;')
# Replace C++ comment begin and ends to ones that don't affect Doxygen.
escaped_text = string.replace(escaped_text, '/*', '/ *')
escaped_text = string.replace(escaped_text, '*/', '* /')
escaped_text = escaped_text.replace('/*', '/ *')
escaped_text = escaped_text.replace('*/', '* /')
return escaped_text
def print_annotations(annotations):
for annotation in annotations:
print "<annotation name=" + annotation[0] + ">" + \
escape_text(annotation[1]) + "</annotation>"
print("<annotation name=" + annotation[0] + ">" + \
escape_text(annotation[1]) + "</annotation>")
if __name__ == '__main__':
try:
......@@ -66,7 +65,7 @@ if __name__ == '__main__':
["source-dir=", "with-annotations",
"with-properties", "no-since",
"no-signals", "no-enums"])
except getopt.error, e:
except getopt.error as e:
sys.stderr.write('docextract_to_xml.py: %s\n' % e)
usage()
source_dirs = []
......@@ -97,7 +96,7 @@ if __name__ == '__main__':
if docs:
print "<root>"
print("<root>")
for name, value in sorted(docs.items()):
# Get the type of comment block ('function', 'signal' or
......@@ -114,41 +113,41 @@ if __name__ == '__main__':
elif block_type == 'enum' and not with_enums:
continue
print "<" + block_type + " name=\"" + escape_text(name) + "\">"
print("<" + block_type + " name=\"" + escape_text(name) + "\">")
print "<description>"
print escape_text(value.get_description())
print "</description>"
print("<description>")
print(escape_text(value.get_description()))
print("</description>")
# Loop through the parameters if not dealing with a property:
if block_type != 'property':
print "<parameters>"
print("<parameters>")
for name, description, annotations in value.params:
print "<parameter name=\"" + escape_text(name) + "\">"
print "<parameter_description>" + escape_text(description) + "</parameter_description>"
print("<parameter name=\"" + escape_text(name) + "\">")
print("<parameter_description>" + escape_text(description) + "</parameter_description>")
if with_annotations:
print_annotations(annotations)
print "</parameter>"
print("</parameter>")
print "</parameters>"
print("</parameters>")
if block_type != 'property' and block_type != 'enum':
# Show the return-type (also if not dealing with a property or
# enum):
if with_annotations:
print "<return>"
print "<return_description>" + escape_text(value.ret[0]) + \
"</return_description>"
print("<return>")
print("<return_description>" + escape_text(value.ret[0]) + \
"</return_description>")
print_annotations(value.ret[1])
print "</return>"
print("</return>")
else:
print "<return>" + escape_text(value.ret[0]) + "</return>"
print("<return>" + escape_text(value.ret[0]) + "</return>")
if with_annotations:
print_annotations(value.get_annotations())
print "</" + block_type + ">\n"
print("</" + block_type + ">\n")
print "</root>"
print("</root>")
......@@ -86,14 +86,14 @@ def to_upper_str(name):
name = _upperstr_pat1.sub(r'\1_\2', name)
name = _upperstr_pat2.sub(r'\1_\2', name)
name = _upperstr_pat3.sub(r'\1_\2', name, count=1)
return string.upper(name)
return name.upper()
def typecode(typename, namespace=None):
"""create a typecode (eg. GTK_TYPE_WIDGET) from a typename"""
if namespace:
return string.replace(string.upper(namespace) + "_" + to_upper_str(typename[len(namespace):]), '_', '_TYPE_', 1)
return (namespace.upper() + "_" + to_upper_str(typename[len(namespace):])).replace('_', '_TYPE_', 1)
return string.replace(to_upper_str(typename), '_', '_TYPE_', 1)
return to_upper_str(typename).replace('_', '_TYPE_', 1)
# ------------------ Find object definitions -----------------
......@@ -102,10 +102,10 @@ def strip_comments(buf):
parts = []
lastpos = 0
while 1:
pos = string.find(buf, '/*', lastpos)
pos = buf.find('/*', lastpos)
if pos >= 0:
parts.append(buf[lastpos:pos])
pos = string.find(buf, '*/', pos)
pos = buf.find('*/', pos)
if pos >= 0:
lastpos = pos + 2
else:
......@@ -113,7 +113,7 @@ def strip_comments(buf):
else:
parts.append(buf[lastpos:])
break
return string.join(parts, '')
return ''.join(parts)
# Strips the dll API from buffer, for example WEBKIT_API
def strip_dll_api(buf):
......@@ -259,11 +259,11 @@ def find_enum_defs(buf, enums=[]):
name = m.group(2)
vals = m.group(1)
isflags = string.find(vals, '<<') >= 0
isflags = '<<' in vals
entries = []
for val in splitter.split(vals):
if not string.strip(val): continue
entries.append(string.split(val)[0])
if not val.strip(): continue
entries.append(val.split()[0])
if name != 'GdkCursorType':
enums.append((name, isflags, entries))
......@@ -340,10 +340,10 @@ def clean_func(buf):
# make return types that are const work.
buf = re.sub(r'\s*\*\s*G_CONST_RETURN\s*\*\s*', '** ', buf)
buf = string.replace(buf, 'G_CONST_RETURN ', 'const-')
buf = string.replace(buf, 'const ', 'const-')
buf = buf.replace('G_CONST_RETURN ', 'const-')
buf = buf.replace('const ', 'const-')
# This is for types such as 'const gchar* const *'
buf = string.replace(buf, '* const', '*-const')
buf = buf.replace('* const', '*-const')
#strip GSEAL macros from the middle of function declarations:
pat = re.compile(r"""GSEAL""", re.VERBOSE)
......@@ -381,7 +381,7 @@ class DefsWriter:
if defsfilter:
filter = defsparser.DefsParser(defsfilter)
filter.startParsing()
for func in filter.functions + filter.methods.values():
for func in filter.functions + list(filter.methods.values()):
self._functions[func.c_name] = func
for obj in filter.objects + filter.boxes + filter.interfaces:
self._objects[obj.c_name] = obj
......@@ -400,8 +400,6 @@ class DefsWriter:
fp = self.fp
fp.write(';; Enumerations and flags ...\n\n')
trans = string.maketrans(string.uppercase + '_',
string.lowercase + '-')
filter = self._enums
for cname, isflags, entries in enums:
if filter:
......@@ -436,7 +434,7 @@ class DefsWriter:
fp.write(' (values\n')
for ent in entries:
fp.write(' \'("%s" "%s")\n' %
(string.translate(ent[prefix_len:], trans), ent))
(ent[prefix_len:].lower().replace('_', '-'), ent))
fp.write(' )\n')
fp.write(')\n\n')
......@@ -474,7 +472,7 @@ class DefsWriter:
def _define_func(self, buf):
buf = clean_func(buf)
buf = string.split(buf,'\n')
buf = buf.split('\n')
filter = self._functions
for p in buf:
if not p:
......@@ -494,9 +492,9 @@ class DefsWriter:
args = m.group('args')
args = arg_split_pat.split(args)
for i in range(len(args)):
spaces = string.count(args[i], ' ')
spaces = args[i].count(' ')
if spaces > 1:
args[i] = string.replace(args[i], ' ', '-', spaces - 1)
args[i] = args[i].replace(' ', '-', spaces - 1)
self._write_func(func, ret, args)
......@@ -538,7 +536,7 @@ class DefsWriter:
self._write_arguments(args)
def _write_method(self, obj, name, ret, args):
regex = string.join(map(lambda x: x+'_?', string.lower(obj)),'')
regex = ''.join([x+'_?' for x in obj.lower()])
mname = re.sub(regex, '', name, 1)
if self.prefix:
l = len(self.prefix) + 1
......@@ -568,7 +566,7 @@ class DefsWriter:
self.fp.write(' (parameters\n')
for arg in args:
if arg != '...':
tupleArg = tuple(string.split(arg))
tupleArg = tuple(arg.split())
if len(tupleArg) == 2:
self.fp.write(' \'("%s" "%s")\n' % tupleArg)
self.fp.write(' )\n')
......@@ -607,7 +605,7 @@ def main(args):
defsfilter = v
if not args[0:1]:
print 'Must specify at least one input file name'
print('Must specify at least one input file name')
return -1
# read all the object definitions in
......@@ -627,11 +625,11 @@ def main(args):
verbose=verbose, defsfilter=defsfilter)
dw.write_obj_defs(objdefs, types)
dw.write_enum_defs(enums, types)
print "Wrote %s-types.defs" % separate
print("Wrote %s-types.defs" % separate)
for filename in args:
dw.write_def(filename)
print "Wrote %s.defs" % separate
print("Wrote %s.defs" % separate)
else:
dw = DefsWriter(prefix=modulename, ns=namespace,
verbose=verbose, defsfilter=defsfilter)
......
#!/usr/bin/env python
# -*- Mode: Python; py-indent-offset: 4 -*-
from __future__ import generators
import string
from cStringIO import StringIO
from io import StringIO
class error(Exception):
def __init__(self, filename, lineno, msg):
......@@ -16,11 +16,11 @@ class error(Exception):
trans = [' '] * 256
for i in range(256):
if chr(i) in string.letters + string.digits + '_':
if chr(i) in string.ascii_letters + string.digits + '_':
trans[i] = chr(i)
else:
trans[i] = '_'
trans = string.join(trans, '')
trans = ''.join(trans)
def parse(filename):
if isinstance(filename, str):
......@@ -113,7 +113,7 @@ class Parser:
for statement in statements:
self.handle(statement)
def handle(self, tup):
cmd = string.translate(tup[0], trans)
cmd = tup[0].translate(trans)
if hasattr(self, cmd):
getattr(self, cmd)(*tup[1:])
else:
......@@ -140,4 +140,4 @@ if __name__ == '__main__':
fp = StringIO(_testString)
statements = parse(fp)
for s in statements:
print `s`
print(repr(s))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment