Fix glib:error-quark scanning for unregistered enum types
[gnome.gobject-introspection] / giscanner / transformer.py
index 88cbc15..bbcabe4 100644 (file)
 #
 
 import os
+import sys
 
 from .ast import (Bitfield, Callback, Enum, Function, Namespace, Member,
                   Parameter, Return, Struct, Field,
                   Type, Array, Alias, Interface, Class, Node, Union,
                   Varargs, Constant, type_name_from_ctype,
                   type_names, TYPE_STRING, BASIC_GIR_TYPES)
-from .config import DATADIR
+from .config import DATADIR, GIR_DIR, GIR_SUFFIX
 from .glibast import GLibBoxed
 from .girparser import GIRParser
 from .odict import odict
@@ -117,15 +118,17 @@ class Transformer(object):
     def _find_include(self, include):
         searchdirs = self._includepaths[:]
         for path in _xdg_data_dirs:
-            searchdirs.append(os.path.join(path, 'gir-1.0'))
+            searchdirs.append(os.path.join(path, GIR_SUFFIX))
+        searchdirs.append(GIR_DIR)
 
         girname = '%s-%s.gir' % (include.name, include.version)
         for d in searchdirs:
             path = os.path.join(d, girname)
             if os.path.exists(path):
                 return path
-        raise ValueError("Couldn't find include %r (search path: %r)"\
+        sys.stderr.write("Couldn't find include %r (search path: %r)\n"\
                          % (girname, searchdirs))
+        sys.exit(1)
 
     def _parse_include(self, filename):
         parser = self._cachestore.load(filename)
@@ -213,15 +216,13 @@ class Transformer(object):
 
     def _enum_common_prefix(self, symbol):
         def common_prefix(a, b):
-            alen = len(a)
-            blen = len(b)
-            l = min(alen, blen)
-            for i in xrange(l):
-                if a[i] != b[i]:
-                    return a[:i]
-            if alen > blen:
-                return b
-            return a
+            commonparts = []
+            for aword, bword in zip(a.split('_'), b.split('_')):
+                if aword != bword:
+                    return '_'.join(commonparts) + '_'
+                commonparts.append(aword)
+            return min(a, b)
+
         # Nothing less than 2 has a common prefix
         if len(list(symbol.base_type.child_list)) < 2:
             return None
@@ -268,8 +269,10 @@ class Transformer(object):
                       symbol.ident)
 
     def _type_is_callback(self, type):
-        if (isinstance(type, Callback) or
-            isinstance(self._typedefs_ns.get(type.name), Callback)):
+        if isinstance(type, Callback):
+            return True
+        node = self._names.names.get(type.name)
+        if node and isinstance(node[1], Callback):
             return True
         return False
 
@@ -282,9 +285,8 @@ class Transformer(object):
         return False
 
     def _handle_destroy(self, param, destroy_idx, destroy_param):
-        if ((self._namespace.name == 'GLib' and
-             destroy_param.type.name == 'DestroyNotify') or
-            destroy_param.type.name == 'GLib.DestroyNotify'):
+        if (destroy_param.type.name == 'GLib.DestroyNotify' or
+            destroy_param.type.ctype == 'GDestroyNotify'):
             param.destroy_name = destroy_param.name
             param.destroy_index = destroy_idx
             return True
@@ -295,6 +297,10 @@ class Transformer(object):
             if not self._type_is_callback(param.type):
                 continue
 
+            # set a default scope
+            if param.scope is None:
+                param.scope = 'call'
+
             # j is the index where we look for closure/destroy to
             # group with the callback param
             j = i + 1
@@ -500,7 +506,8 @@ class Transformer(object):
     def _create_const(self, symbol):
         # Don't create constants for non-public things
         # http://bugzilla.gnome.org/show_bug.cgi?id=572790
-        if not symbol.source_filename.endswith('.h'):
+        if (symbol.source_filename is None or
+            not symbol.source_filename.endswith('.h')):
             return None
         name = self.remove_prefix(symbol.ident)
         if symbol.const_string is not None:
@@ -665,7 +672,7 @@ class Transformer(object):
                                                      self.ctype_of(ptype),
                                                      names, **kwargs)
         elif isinstance(ptype, basestring):
-            return self.resolve_type_name_full(ptype, None, names, **kwargs)
+            return self.resolve_type_name_full(ptype, ptype, names, **kwargs)
         else:
             raise AssertionError("Unhandled param: %r" % (ptype, ))
         return ptype
@@ -685,3 +692,8 @@ class Transformer(object):
             else:
                 break
         return type_name
+
+    def iter_enums(self):
+        for node in self._namespace.nodes:
+            if isinstance(node, Enum):
+                yield node