MCPcopy Index your code
hub / github.com/RustPython/RustPython / stderr_to_parser_error

Function stderr_to_parser_error

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

Source from the content-addressed store, hash-verified

147
148
149def stderr_to_parser_error(parse_args, *args, **kwargs):
150 # if this is being called recursively and stderr or stdout is already being
151 # redirected, simply call the function and let the enclosing function
152 # catch the exception
153 if isinstance(sys.stderr, StdIOBuffer) or isinstance(sys.stdout, StdIOBuffer):
154 return parse_args(*args, **kwargs)
155
156 # if this is not being called recursively, redirect stderr and
157 # use it as the ArgumentParserError message
158 old_stdout = sys.stdout
159 old_stderr = sys.stderr
160 sys.stdout = StdIOBuffer()
161 sys.stderr = StdIOBuffer()
162 try:
163 try:
164 result = parse_args(*args, **kwargs)
165 for key in list(vars(result)):
166 attr = getattr(result, key)
167 if attr is sys.stdout:
168 setattr(result, key, old_stdout)
169 elif attr is sys.stdout.buffer:
170 setattr(result, key, getattr(old_stdout, 'buffer', BIN_STDOUT_SENTINEL))
171 elif attr is sys.stderr:
172 setattr(result, key, old_stderr)
173 elif attr is sys.stderr.buffer:
174 setattr(result, key, getattr(old_stderr, 'buffer', BIN_STDERR_SENTINEL))
175 return result
176 except SystemExit as e:
177 code = e.code
178 stdout = sys.stdout.getvalue()
179 stderr = sys.stderr.getvalue()
180 raise ArgumentParserError(
181 "SystemExit", stdout, stderr, code) from None
182 finally:
183 sys.stdout = old_stdout
184 sys.stderr = old_stderr
185
186
187class ErrorRaisingArgumentParser(argparse.ArgumentParser):

Callers 3

parse_argsMethod · 0.85
exitMethod · 0.85
errorMethod · 0.85

Calls 9

isinstanceFunction · 0.85
StdIOBufferClass · 0.85
listClass · 0.85
varsFunction · 0.85
getattrFunction · 0.85
setattrFunction · 0.85
ArgumentParserErrorClass · 0.85
parse_argsFunction · 0.50
getvalueMethod · 0.45

Tested by

no test coverage detected