pygobject r967 - in trunk: . codegen



Author: paulp
Date: Mon Sep  8 20:26:13 2008
New Revision: 967
URL: http://svn.gnome.org/viewvc/pygobject?rev=967&view=rev

Log:
2008-09-08  Paul Pogonyshev  <pogonyshev gmx net>

	Bug 551056 â make codegen not import when corresponding argument
	types are not registered

	* codegen/override.py (import_pat): Add support for optional 'for'
	clause.
	(Overrides.__parse_override): Handle it.

	* codegen/codegen.py (SourceWriter.write_imports)
	(SourceWriter.write_object_imports): Adapt for changes in
	get_imports() return value.
	(is_registered_object): New function.


Modified:
   trunk/ChangeLog
   trunk/codegen/codegen.py
   trunk/codegen/override.py

Modified: trunk/codegen/codegen.py
==============================================================================
--- trunk/codegen/codegen.py	(original)
+++ trunk/codegen/codegen.py	Mon Sep  8 20:26:13 2008
@@ -1374,9 +1374,10 @@
 
     def write_imports(self):
         self.fp.write('/* ---------- types from other modules ---------- */\n')
-        for module, pyname, cname in self.overrides.get_imports():
-            self.fp.write('static PyTypeObject *_%s;\n' % cname)
-            self.fp.write('#define %s (*_%s)\n' % (cname, cname))
+        for module, pyname, cname, importing_for in self.overrides.get_imports():
+            if importing_for is None or is_registered_object(importing_for):
+                self.fp.write('static PyTypeObject *_%s;\n' % cname)
+                self.fp.write('#define %s (*_%s)\n' % (cname, cname))
         self.fp.write('\n\n')
 
     def write_type_declarations(self):
@@ -1485,8 +1486,9 @@
             return
 
         bymod = {}
-        for module, pyname, cname in imports:
-            bymod.setdefault(module, []).append((pyname, cname))
+        for module, pyname, cname, importing_for in imports:
+            if importing_for is None or is_registered_object(importing_for):
+                bymod.setdefault(module, []).append((pyname, cname))
         self.fp.write('    PyObject *module;\n\n')
         for module in bymod:
             self.fp.write(
@@ -1625,6 +1627,9 @@
 
 _objects = {}
 
+def is_registered_object(c_name):
+    return c_name in _objects
+
 def get_object_by_name(c_name):
     global _objects
     return _objects[c_name]

Modified: trunk/codegen/override.py
==============================================================================
--- trunk/codegen/override.py	(original)
+++ trunk/codegen/override.py	Mon Sep  8 20:26:13 2008
@@ -19,7 +19,10 @@
             c_name += c
     return c_name[1:] + '_'  + method
 
-import_pat = re.compile(r'\s*import\s+(\S+)\.([^\s.]+)\s+as\s+(\S+)')
+# import python_type as c_name [for arg_type]
+# Last ('for') clause is optional.  If present, the type will be
+# imported only if given 'arg_type' is registered.
+import_pat = re.compile(r'\s*import\s+(\S+)\.([^\s.]+)\s+as\s+(\S+)(\s+for\s+(\S+))?')
 
 class Overrides:
     def __init__(self, filename=None):
@@ -166,7 +169,8 @@
             for line in string.split(buffer, '\n'):
                 match = import_pat.match(line)
                 if match:
-                    self.imports.append(match.groups())
+                    module, pyname, cname, conditional, importing_for = match.groups()
+                    self.imports.append((module, pyname, cname, importing_for or None))
         elif command == 'define':
             "define funcname [kwargs|noargs|onearg] [classmethod|staticmethod]"
             "define Class.method [kwargs|noargs|onearg] [classmethod|staticmethod]"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]