MCPcopy Index your code
hub / github.com/python/cpython / stderr_to_parser_error

Function stderr_to_parser_error

Lib/test/test_argparse.py:169–204  ·  view source on GitHub ↗
(parse_args, *args, **kwargs)

Source from the content-addressed store, hash-verified

167
168
169def stderr_to_parser_error(parse_args, *args, **kwargs):
170 # if this is being called recursively and stderr or stdout is already being
171 # redirected, simply call the function and let the enclosing function
172 # catch the exception
173 if isinstance(sys.stderr, StdIOBuffer) or isinstance(sys.stdout, StdIOBuffer):
174 return parse_args(*args, **kwargs)
175
176 # if this is not being called recursively, redirect stderr and
177 # use it as the ArgumentParserError message
178 old_stdout = sys.stdout
179 old_stderr = sys.stderr
180 sys.stdout = StdIOBuffer()
181 sys.stderr = StdIOBuffer()
182 try:
183 try:
184 result = parse_args(*args, **kwargs)
185 for key in list(vars(result)):
186 attr = getattr(result, key)
187 if attr is sys.stdout:
188 setattr(result, key, old_stdout)
189 elif attr is sys.stdout.buffer:
190 setattr(result, key, getattr(old_stdout, 'buffer', BIN_STDOUT_SENTINEL))
191 elif attr is sys.stderr:
192 setattr(result, key, old_stderr)
193 elif attr is sys.stderr.buffer:
194 setattr(result, key, getattr(old_stderr, 'buffer', BIN_STDERR_SENTINEL))
195 return result
196 except SystemExit as e:
197 code = e.code
198 stdout = sys.stdout.getvalue()
199 stderr = sys.stderr.getvalue()
200 raise ArgumentParserError(
201 "SystemExit", stdout, stderr, code) from None
202 finally:
203 sys.stdout = old_stdout
204 sys.stderr = old_stderr
205
206
207class ErrorRaisingArgumentParser(argparse.ArgumentParser):

Callers 3

parse_argsMethod · 0.85
exitMethod · 0.85
errorMethod · 0.85

Calls 5

StdIOBufferClass · 0.85
listClass · 0.85
ArgumentParserErrorClass · 0.85
parse_argsFunction · 0.70
getvalueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…