(parse_args, *args, **kwargs)
| 167 | |
| 168 | |
| 169 | def 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 | |
| 207 | class ErrorRaisingArgumentParser(argparse.ArgumentParser): |
no test coverage detected
searching dependent graphs…