1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
4
5import textwrap
6
7from astroid import nodes
8from astroid.brain.helpers import register_module_extender
9from astroid.builder import parse
10from astroid.manager import AstroidManager
11
12
13def _subprocess_transform() -> nodes.Module:
14 communicate = (bytes("string", "ascii"), bytes("string", "ascii"))
15 communicate_signature = "def communicate(self, input=None, timeout=None)"
16 args = """\
17 self, args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None,
18 preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None,
19 universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True,
20 start_new_session=False, pass_fds=(), *, encoding=None, errors=None, text=None,
21 user=None, group=None, extra_groups=None, umask=-1, pipesize=-1,
22 process_group=None"""
23
24 init = f"""
25 def __init__({args}):
26 pass"""
27 wait_signature = "def wait(self, timeout=None)"
28 ctx_manager = """
29 def __enter__(self): return self
30 def __exit__(self, *args): pass
31 """
32 py3_args = "args = []"
33
34 check_output_signature = """
35 check_output(
36 args, *,
37 stdin=None,
38 stderr=None,
39 shell=False,
40 cwd=None,
41 encoding=None,
42 errors=None,
43 universal_newlines=False,
44 timeout=None,
45 env=None,
46 text=None,
47 restore_signals=True,
48 preexec_fn=None,
49 pass_fds=(),
50 input=None,
51 bufsize=0,
52 executable=None,
53 close_fds=False,
54 startupinfo=None,
55 creationflags=0,
56 start_new_session=False
57 ):
58 """.strip()
59
60 code = textwrap.dedent(f"""
61 def {check_output_signature}
62 if universal_newlines:
63 return ""
64 return b""
65
66 class Popen(object):
67 returncode = pid = 0
68 stdin = stdout = stderr = file()
69 {py3_args}
70
71 {communicate_signature}:
72 return {communicate!r}
73 {wait_signature}:
74 return self.returncode
75 def poll(self):
76 return self.returncode
77 def send_signal(self, signal):
78 pass
79 def terminate(self):
80 pass
81 def kill(self):
82 pass
83 {ctx_manager}
84 @classmethod
85 def __class_getitem__(cls, item):
86 pass
87 """)
88
89 init_lines = textwrap.dedent(init).splitlines()
90 indented_init = "\n".join(" " * 4 + line for line in init_lines)
91 code += indented_init
92 return parse(code)
93
94
95def register(manager: AstroidManager) -> None:
96 register_module_extender(manager, "subprocess", _subprocess_transform)