2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 Johan Dahlin
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
26 from xml.etree.cElementTree import parse
31 from .transformer import TransformerException
32 from .utils import to_underscores
35 G_PARAM_READABLE = 1 << 0
36 G_PARAM_WRITABLE = 1 << 1
37 G_PARAM_CONSTRUCT = 1 << 2
38 G_PARAM_CONSTRUCT_ONLY = 1 << 3
39 G_PARAM_LAX_VALIDATION = 1 << 4
40 G_PARAM_STATIC_NAME = 1 << 5
41 G_PARAM_STATIC_NICK = 1 << 6
42 G_PARAM_STATIC_BLURB = 1 << 7
45 class IntrospectionBinary(object):
47 def __init__(self, args, tmpdir=None):
50 self.tmpdir = tempfile.mkdtemp('', 'tmp-introspect')
55 class Unresolved(object):
57 def __init__(self, target):
61 class UnknownTypeError(Exception):
65 class GDumpParser(object):
67 def __init__(self, transformer):
68 self._transformer = transformer
69 self._namespace = transformer.namespace
71 self._get_type_functions = []
72 self._error_quark_functions = []
73 self._error_domains = {}
74 self._boxed_types = {}
75 self._private_internal_types = {}
80 """Do parsing steps that don't involve the introspection binary
82 This does enough work that get_type_functions() can be called.
87 for node in self._namespace.itervalues():
88 if isinstance(node, ast.Function):
89 self._initparse_function(node)
91 if self._namespace.name == 'GObject' or self._namespace.name == 'GLib':
92 for node in self._namespace.itervalues():
93 if isinstance(node, ast.Record):
94 self._initparse_gobject_record(node)
96 def get_get_type_functions(self):
97 return self._get_type_functions
99 def get_error_quark_functions(self):
100 return self._error_quark_functions
102 def set_introspection_binary(self, binary):
103 self._binary = binary
106 """Do remaining parsing steps requiring introspection binary"""
108 # Get all the GObject data by passing our list of get_type
109 # functions to the compiled binary, returning an XML blob.
110 tree = self._execute_binary_get_tree()
111 root = tree.getroot()
113 if child.tag == 'error-quark':
114 self._introspect_error_quark(child)
116 self._introspect_type(child)
118 # Pair up boxed types and class records
119 for name, boxed in self._boxed_types.iteritems():
120 self._pair_boxed_type(boxed)
121 for node in self._namespace.itervalues():
122 if isinstance(node, (ast.Class, ast.Interface)):
123 self._find_class_record(node)
125 # Clear the _get_type functions out of the namespace;
126 # Anyone who wants them can get them from the ast.Class/Interface/Boxed
128 for name, node in self._namespace.iteritems():
129 if isinstance(node, ast.Registered) and node.get_type is not None:
130 get_type_name = node.get_type
131 if get_type_name == 'intern':
133 assert get_type_name, node
134 (ns, name) = self._transformer.split_csymbol(get_type_name)
135 assert ns is self._namespace
136 get_type_func = self._namespace.get(name)
137 assert get_type_func, name
138 to_remove.append(get_type_func)
139 for node in to_remove:
140 self._namespace.remove(node)
144 def _execute_binary_get_tree(self):
145 """Load the library (or executable), returning an XML
146 blob containing data gleaned from GObject's primitive introspection."""
147 in_path = os.path.join(self._binary.tmpdir, 'functions.txt')
148 f = open(in_path, 'w')
149 for func in self._get_type_functions:
153 for func in self._error_quark_functions:
154 f.write('error-quark:')
158 out_path = os.path.join(self._binary.tmpdir, 'dump.xml')
161 args.extend(self._binary.args)
162 args.append('--introspect-dump=%s,%s' % (in_path, out_path))
164 # Invoke the binary, having written our get_type functions to types.txt
167 subprocess.check_call(args, stdout=sys.stdout, stderr=sys.stderr)
168 except subprocess.CalledProcessError as e:
169 # Clean up temporaries
171 return parse(out_path)
173 if not utils.have_debug_flag('save-temps'):
174 shutil.rmtree(self._binary.tmpdir)
178 def _initparse_function(self, func):
180 if symbol.startswith('_'):
182 elif (symbol.endswith('_get_type') or symbol.endswith('_get_gtype')):
183 self._initparse_get_type_function(func)
184 elif symbol.endswith('_error_quark'):
185 self._initparse_error_quark_function(func)
187 def _initparse_get_type_function(self, func):
188 if func.symbol == 'g_variant_get_gtype':
189 # We handle variants internally, see _initparse_gobject_record
192 if func.is_type_meta_function():
193 self._get_type_functions.append(func.symbol)
198 def _initparse_error_quark_function(self, func):
199 if (func.retval.type.ctype != 'GQuark'):
201 self._error_quark_functions.append(func.symbol)
204 def _initparse_gobject_record(self, record):
205 if (record.name.startswith('ParamSpec')
206 and not record.name in ('ParamSpecPool', 'ParamSpecClass', 'ParamSpecTypeInfo')):
208 if record.name != 'ParamSpec':
209 parent = ast.Type(target_giname='GObject.ParamSpec')
210 prefix = to_underscores(record.name).lower()
211 node = ast.Class(record.name, parent,
213 # GParamSpecXxx has g_type_name 'GParamXxx'
214 gtype_name=record.ctype.replace('Spec', ''),
216 c_symbol_prefix=prefix)
217 node.fundamental = True
218 if record.name == 'ParamSpec':
219 node.is_abstract = True
220 self._add_record_fields(node)
221 self._namespace.append(node, replace=True)
222 elif record.name == 'Variant':
223 self._boxed_types['GVariant'] = ast.Boxed('Variant',
224 gtype_name='GVariant',
226 c_symbol_prefix='variant')
227 elif record.name == 'InitiallyUnownedClass':
228 record.fields = self._namespace.get('ObjectClass').fields
229 record.disguised = False
231 # Introspection over the data we get from the dynamic
232 # GObject/GType system out of the binary
234 def _introspect_type(self, xmlnode):
235 if xmlnode.tag in ('enum', 'flags'):
236 self._introspect_enum(xmlnode)
237 elif xmlnode.tag == 'class':
238 self._introspect_object(xmlnode)
239 elif xmlnode.tag == 'interface':
240 self._introspect_interface(xmlnode)
241 elif xmlnode.tag == 'boxed':
242 self._introspect_boxed(xmlnode)
243 elif xmlnode.tag == 'fundamental':
244 self._introspect_fundamental(xmlnode)
246 raise ValueError("Unhandled introspection XML tag %s", xmlnode.tag)
248 def _introspect_enum(self, xmlnode):
249 type_name = xmlnode.attrib['name']
250 (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
252 enum_name = self._transformer.strip_identifier(type_name)
253 except TransformerException as e:
256 # The scanned member values are more accurate than the values that the
257 # we dumped from GEnumValue.value because GEnumValue.value has the
258 # values as a 32-bit signed integer, even if they were unsigned
259 # in the source code.
261 previous = self._namespace.get(enum_name)
262 if isinstance(previous, (ast.Enum, ast.Bitfield)):
263 for member in previous.members:
264 previous_values[member.name] = member.value
267 for member in xmlnode.findall('member'):
268 # Keep the name closer to what we'd take from C by default;
269 # see http://bugzilla.gnome.org/show_bug.cgi?id=575613
270 name = member.attrib['nick'].replace('-', '_')
272 if name in previous_values:
273 value = previous_values[name]
275 value = member.attrib['value']
277 members.append(ast.Member(name,
279 member.attrib['name'],
280 member.attrib['nick']))
282 if xmlnode.tag == 'flags':
287 node = klass(enum_name, type_name,
288 gtype_name=type_name,
289 c_symbol_prefix=c_symbol_prefix,
291 get_type=xmlnode.attrib['get-type'])
292 self._namespace.append(node, replace=True)
294 def _split_type_and_symbol_prefix(self, xmlnode):
295 """Infer the C symbol prefix from the _get_type function."""
296 get_type = xmlnode.attrib['get-type']
297 (ns, name) = self._transformer.split_csymbol(get_type)
298 assert ns is self._namespace
299 if name in ('get_type', '_get_gtype'):
300 message.fatal("""The GObject name %r isn't compatible
301 with the configured identifier prefixes:
303 The class would have no name. Most likely you want to specify a
304 different --identifier-prefix.""" % (xmlnode.attrib['name'], self._namespace.identifier_prefixes))
305 if name.endswith('_get_type'):
306 type_suffix = '_get_type'
308 type_suffix = '_get_gtype'
309 return (get_type, name[:-len(type_suffix)])
311 def _introspect_object(self, xmlnode):
312 type_name = xmlnode.attrib['name']
313 is_abstract = bool(xmlnode.attrib.get('abstract', False))
314 (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
316 object_name = self._transformer.strip_identifier(type_name)
317 except TransformerException as e:
319 node = ast.Class(object_name, None,
320 gtype_name=type_name,
322 c_symbol_prefix=c_symbol_prefix,
323 is_abstract=is_abstract)
324 self._parse_parents(xmlnode, node)
325 self._introspect_properties(node, xmlnode)
326 self._introspect_signals(node, xmlnode)
327 self._introspect_implemented_interfaces(node, xmlnode)
328 self._add_record_fields(node)
329 self._namespace.append(node, replace=True)
331 def _introspect_interface(self, xmlnode):
332 type_name = xmlnode.attrib['name']
333 (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
335 interface_name = self._transformer.strip_identifier(type_name)
336 except TransformerException as e:
338 node = ast.Interface(interface_name, None,
339 gtype_name=type_name,
341 c_symbol_prefix=c_symbol_prefix)
342 self._introspect_properties(node, xmlnode)
343 self._introspect_signals(node, xmlnode)
344 for child in xmlnode.findall('prerequisite'):
345 name = child.attrib['name']
346 prereq = ast.Type.create_from_gtype_name(name)
347 node.prerequisites.append(prereq)
349 record = self._namespace.get(node.name)
350 if isinstance(record, ast.Record):
351 node.ctype = record.ctype
353 message.warn_node(node, "Couldn't find associated structure for '%r'" % (node.name, ))
355 # GtkFileChooserEmbed is an example of a private interface, we
356 # just filter them out
357 if xmlnode.attrib['get-type'].startswith('_'):
358 self._private_internal_types[type_name] = node
360 self._namespace.append(node, replace=True)
363 # https://bugzilla.gnome.org/show_bug.cgi?id=550616
364 def _introspect_boxed_gstreamer_workaround(self, xmlnode):
365 node = ast.Boxed('ParamSpecMiniObject', gtype_name='GParamSpecMiniObject',
366 get_type='gst_param_spec_mini_object_get_type',
367 c_symbol_prefix='param_spec_mini_object')
368 self._boxed_types[node.gtype_name] = node
370 def _introspect_boxed(self, xmlnode):
371 type_name = xmlnode.attrib['name']
373 # Work around GStreamer legacy naming issue
374 # https://bugzilla.gnome.org/show_bug.cgi?id=550616
375 if type_name == 'GParamSpecMiniObject':
376 self._introspect_boxed_gstreamer_workaround(xmlnode)
380 name = self._transformer.strip_identifier(type_name)
381 except TransformerException as e:
383 # This one doesn't go in the main namespace; we associate it with
384 # the struct or union
385 (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
386 node = ast.Boxed(name, gtype_name=type_name,
388 c_symbol_prefix=c_symbol_prefix)
389 self._boxed_types[node.gtype_name] = node
391 def _introspect_implemented_interfaces(self, node, xmlnode):
393 for interface in xmlnode.findall('implements'):
394 gitype = ast.Type.create_from_gtype_name(interface.attrib['name'])
395 gt_interfaces.append(gitype)
396 node.interfaces = gt_interfaces
398 def _introspect_properties(self, node, xmlnode):
399 for pspec in xmlnode.findall('property'):
400 ctype = pspec.attrib['type']
401 flags = int(pspec.attrib['flags'])
402 readable = (flags & G_PARAM_READABLE) != 0
403 writable = (flags & G_PARAM_WRITABLE) != 0
404 construct = (flags & G_PARAM_CONSTRUCT) != 0
405 construct_only = (flags & G_PARAM_CONSTRUCT_ONLY) != 0
406 node.properties.append(ast.Property(
407 pspec.attrib['name'],
408 ast.Type.create_from_gtype_name(ctype),
409 readable, writable, construct, construct_only))
410 node.properties = node.properties
412 def _introspect_signals(self, node, xmlnode):
413 for signal_info in xmlnode.findall('signal'):
414 rctype = signal_info.attrib['return']
415 rtype = ast.Type.create_from_gtype_name(rctype)
416 return_ = ast.Return(rtype)
418 when = signal_info.attrib.get('when')
419 no_recurse = signal_info.attrib.get('no-recurse', '0') == '1'
420 detailed = signal_info.attrib.get('detailed', '0') == '1'
421 action = signal_info.attrib.get('action', '0') == '1'
422 no_hooks = signal_info.attrib.get('no-hooks', '0') == '1'
423 for i, parameter in enumerate(signal_info.findall('param')):
427 argname = 'p%s' % (i - 1, )
428 pctype = parameter.attrib['type']
429 ptype = ast.Type.create_from_gtype_name(pctype)
430 param = ast.Parameter(argname, ptype)
431 param.transfer = ast.PARAM_TRANSFER_NONE
432 parameters.append(param)
433 signal = ast.Signal(signal_info.attrib['name'], return_, parameters,
434 when=when, no_recurse=no_recurse, detailed=detailed,
435 action=action, no_hooks=no_hooks)
436 node.signals.append(signal)
437 node.signals = node.signals
439 def _parse_parents(self, xmlnode, node):
440 parents_str = xmlnode.attrib.get('parents', '')
441 if parents_str != '':
442 parent_types = map(lambda s: ast.Type.create_from_gtype_name(s),
443 parents_str.split(','))
446 node.parent_chain = parent_types
448 def _introspect_fundamental(self, xmlnode):
449 type_name = xmlnode.attrib['name']
451 is_abstract = bool(xmlnode.attrib.get('abstract', False))
452 (get_type, c_symbol_prefix) = self._split_type_and_symbol_prefix(xmlnode)
454 fundamental_name = self._transformer.strip_identifier(type_name)
455 except TransformerException as e:
459 node = ast.Class(fundamental_name, None,
460 gtype_name=type_name,
462 c_symbol_prefix=c_symbol_prefix,
463 is_abstract=is_abstract)
464 self._parse_parents(xmlnode, node)
465 node.fundamental = True
466 self._introspect_implemented_interfaces(node, xmlnode)
468 self._add_record_fields(node)
469 self._namespace.append(node, replace=True)
471 def _add_record_fields(self, node):
473 record = self._namespace.get(node.name)
474 if not isinstance(record, ast.Record):
476 node.ctype = record.ctype
477 node.fields = record.fields
478 for field in node.fields:
479 if isinstance(field, ast.Field):
480 # Object instance fields are assumed to be read-only
481 # (see also _find_class_record and transformer.py)
482 field.writable = False
484 def _introspect_error_quark(self, xmlnode):
485 symbol = xmlnode.attrib['function']
486 error_domain = xmlnode.attrib['domain']
487 function = self._namespace.get_by_symbol(symbol)
491 node = ast.ErrorQuarkFunction(function.name, function.retval,
492 function.parameters, function.throws,
493 function.symbol, error_domain)
494 self._namespace.append(node, replace=True)
496 def _pair_boxed_type(self, boxed):
498 name = self._transformer.strip_identifier(boxed.gtype_name)
499 except TransformerException as e:
501 pair_node = self._namespace.get(name)
503 # Keep the "bare" boxed instance
504 self._namespace.append(boxed)
505 elif isinstance(pair_node, (ast.Record, ast.Union)):
506 pair_node.add_gtype(boxed.gtype_name, boxed.get_type)
507 assert boxed.c_symbol_prefix is not None
508 pair_node.c_symbol_prefix = boxed.c_symbol_prefix
509 # Quick hack - reset the disguised flag; we're setting it
510 # incorrectly in the scanner
511 pair_node.disguised = False
515 def _find_class_record(self, cls):
517 if isinstance(cls, ast.Class):
518 pair_record = self._namespace.get(cls.name + 'Class')
520 for suffix in ('Iface', 'Interface'):
521 pair_record = self._namespace.get(cls.name + suffix)
524 if not (pair_record and isinstance(pair_record, ast.Record)):
527 cls.glib_type_struct = pair_record.create_type()
528 cls.inherit_file_positions(pair_record)
529 pair_record.is_gtype_struct_for = cls.create_type()