about summary refs log tree commit diff stats
path: root/tests/pylint
diff options
context:
space:
mode:
Diffstat (limited to 'tests/pylint')
-rw-r--r--tests/pylint/py2_compat.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/tests/pylint/py2_compat.py b/tests/pylint/py2_compat.py
index f3c4398c..5cc5b911 100644
--- a/tests/pylint/py2_compat.py
+++ b/tests/pylint/py2_compat.py
@@ -45,7 +45,12 @@ class Py2CompatibilityChecker(BaseChecker):
         "E4220": ('Use explicit format spec numbering',
                   "implicit-format-spec",
                   'Python 2.6 does not support implicit format spec numbering'
-                  ' "{}", use explicit numbering "{0}" or keywords "{key}".')
+                  ' "{}", use explicit numbering "{0}" or keywords "{key}".'),
+        "E4230": ("Use popen23.Popen with with-statements",
+                  "with-popen23",
+                  "Python 2 subprocess.Popen objects were not contextmanagers,"
+                  "popen23.Popen wraps them to enable use with"
+                  "with-statements."),
     }
     # This class variable declares the options
     # that are configurable by the user.
@@ -116,6 +121,19 @@ class Py2CompatibilityChecker(BaseChecker):
                     self.add_message("implicit-format-spec", node=node,
                                      confidence=HIGH)
 
+    def visit_with(self, node):
+        """Make sure subprocess.Popen objects aren't used in with-statements"""
+        for (cm, _) in node.items:
+            if isinstance(cm, astroid.nodes.Call):
+                if ((isinstance(cm.func, astroid.nodes.Name)
+                    and cm.func.name.endswith("Popen")
+                    and (node.root().scope_lookup(node.root(), "Popen")[1][0]
+                        ).modname == "subprocess")
+                    or (isinstance(cm.func, astroid.nodes.Attribute)
+                    and cm.func.expr == "subprocess"
+                    and cm.func.attrname == "Popen")):
+                    self.add_message("with-popen23", node=node, confidence=HIGH)
+
 
 def register(linter):
     """This required method auto registers the checker.