Skip to content

(Group 10 - Case Study 3) Refactor: remove duplicated KeyboardInterrupt/SystemExit handling in raw_main() #2013

Backlog

Description

@orjuelanico

Summary

raw_main() in httpie/core.py contains the same KeyboardInterrupt / SystemExit handling twice: once around parser.parse_args() and again around main_program().

Location

  • httpie/core.py, lines 86–96 (first try, argument parsing)
  • httpie/core.py, lines 104–114 (second try, main program)

Both blocks are token-for-token identical (apart from indentation):

except KeyboardInterrupt:
    env.stderr.write('\n')
    if include_traceback:
        raise
    exit_status = ExitStatus.ERROR_CTRL_C
except SystemExit as e:
    if e.code != ExitStatus.SUCCESS:
        env.stderr.write('\n')
        if include_traceback:
            raise
        exit_status = ExitStatus.ERROR

Detected with PMD CPD (--minimum-tokens 25): 51 tokens, 11 lines, Type 1 (exact) clone. It is the largest duplicated block in httpie/ and the only one that survives at 50 tokens.

Proposed change

Extract the shared logic into one nested helper in raw_main(), next to the existing handle_generic_error(), and use it in both try blocks:

def handle_interrupt(exc: BaseException) -> ExitStatus:
    if isinstance(exc, SystemExit) and exc.code == ExitStatus.SUCCESS:
        return ExitStatus.SUCCESS
    env.stderr.write('\n')
    if include_traceback:
        raise
    if isinstance(exc, KeyboardInterrupt):
        return ExitStatus.ERROR_CTRL_C
    return ExitStatus.ERROR

...
except (KeyboardInterrupt, SystemExit) as exc:
    exit_status = handle_interrupt(exc)

Activity

  1. added
    new
    on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    new

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions