Blob Blame History Raw
# HG changeset patch
# User David Shea <dshea@redhat.com>
# Date 1436366590 14400
#      Wed Jul 08 10:43:10 2015 -0400
# Branch pygobject-require_version
# Node ID 57b1f06de1794f1fdc4b89f995d6db7c0e2f7b9e
# Parent  4e95eaf062feba0e0505b471ccb3190fd9f19ac0
Duplicate calls to gi.require_version.

gobject-introspection uses gi.require_version to choose which version of a
library to import from gi.repository, and pygobject now issues a warning if
gi.require_version is not used. Find calls to require_version and duplicate
them in py2gi.py so that the version data is present in the context of the
import.

diff -r 4e95eaf062fe -r 57b1f06de179 astroid/brain/py2gi.py
--- a/astroid/brain/py2gi.py	Tue Jul 07 12:51:30 2015 +0300
+++ b/astroid/brain/py2gi.py	Wed Jul 08 10:43:10 2015 -0400
@@ -9,7 +9,7 @@
 import re
 import warnings
 
-from astroid import MANAGER, AstroidBuildingException
+from astroid import MANAGER, AstroidBuildingException, nodes
 from astroid.builder import AstroidBuilder
 
 
@@ -158,6 +158,34 @@
         raise AstroidBuildingException('Failed to import module %r' % modname)
     return astng
 
+def _looks_like_require_version(node):
+    # Return whether this looks like a call to gi.require_version(<name>, <version>)
+    # Only accept function calls with two constant arguments
+    if len(node.args) != 2:
+        return False
+
+    if not all(isinstance(arg, nodes.Const) for arg in node.args):
+        return False
+
+    func = node.func
+    if isinstance(func, nodes.Getattr):
+        if func.attrname != 'require_version':
+            return False
+        if isinstance(func.expr, nodes.Name) and func.expr.name == 'gi':
+            return True
+
+        return False
+
+    if isinstance(func, nodes.Name):
+        return func.name == 'require_version'
+
+    return False
+
+def _register_require_version(node):
+    # Load the gi.require_version locally
+    import gi
+    gi.require_version(node.args[0].value, node.args[1].value)
+    return node
 
 MANAGER.register_failed_import_hook(_import_gi_module)
-
+MANAGER.register_transform(nodes.CallFunc, _register_require_version, _looks_like_require_version)