From fc4430de36d033563d21413ae009409fd7cb3a59 Mon Sep 17 00:00:00 2001 From: toonn Date: Sun, 8 Aug 2021 19:38:44 +0200 Subject: py2_compat: Added check for with Popen Popen objects became context managers after Python 3.2 so we can't use them as such without a wrapper. --- tests/pylint/py2_compat.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'tests/pylint') 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. -- cgit 1.4.1-2-gfad0 dafe3ad75'>root/themes/shade
blob: 0b61a0f746d33c1917bdc264373f4bf688670cb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36