From e11aaec3ac13f133b8e01aa57e646de3c68ae6a3 Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Tue, 30 Apr 2024 13:13:50 +0300 Subject: [PATCH] Always use sys.exit when exiting the application The `exit()` [1] function is not necessarily present in every Python environment, as it's added by the `site` module. Also, this function is "[...] useful for the interactive interpreter shell and should not be used in programs" For this reason, we replace all such occurrences with `sys.exit()` [2], which is the canonical function to exit Python programs. [1]: https://docs.python.org/3/library/constants.html#exit [2]: https://docs.python.org/3/library/sys.html#sys.exit --- dangerzone/args.py | 3 ++- dangerzone/cli.py | 4 ++-- dangerzone/errors.py | 2 +- dev_scripts/qa.py | 4 ++-- install/macos/build-app.py | 3 ++- tests/test_large_set.py | 3 ++- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/dangerzone/args.py b/dangerzone/args.py index f273e65..f73d12d 100644 --- a/dangerzone/args.py +++ b/dangerzone/args.py @@ -1,5 +1,6 @@ import functools import os +import sys from typing import List, Optional, Tuple import click @@ -86,7 +87,7 @@ def check_suspicious_options(args: List[str]) -> None: f" current working directory: {filenames_str}" ) click.echo(msg) - exit(1) + sys.exit(1) def override_parser_and_check_suspicious_options(click_main: click.Command) -> None: diff --git a/dangerzone/cli.py b/dangerzone/cli.py index e93104b..4ba9432 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -67,7 +67,7 @@ def cli_main( dangerzone.add_document_from_filename(filenames[0], output_filename, archive) elif len(filenames) > 1 and output_filename: click.echo("--output-filename can only be used with one input file.") - exit(1) + sys.exit(1) else: for filename in filenames: dangerzone.add_document_from_filename(filename, archive=archive) @@ -83,7 +83,7 @@ def cli_main( click.echo("Invalid OCR language code. Valid language codes:") for lang in dangerzone.ocr_languages: click.echo(f"{dangerzone.ocr_languages[lang]}: {lang}") - exit(1) + sys.exit(1) # Ensure container is installed dangerzone.isolation_provider.install() diff --git a/dangerzone/errors.py b/dangerzone/errors.py index 0f8a557..8d91678 100644 --- a/dangerzone/errors.py +++ b/dangerzone/errors.py @@ -107,6 +107,6 @@ def handle_document_errors(func: F) -> F: msg = "An exception occured while validating a document" log.exception(msg) click.echo(str(e)) - exit(1) + sys.exit(1) return cast(F, wrapper) diff --git a/dev_scripts/qa.py b/dev_scripts/qa.py index d8b3dd4..ecfcb92 100755 --- a/dev_scripts/qa.py +++ b/dev_scripts/qa.py @@ -430,7 +430,7 @@ class Reference: " to date with the respective doc section, and then update the cached" " section in this file." ) - exit(1) + sys.exit(1) def find_section_text(self, md_text): """Find a section's content in a provided Markdown string.""" @@ -978,7 +978,7 @@ def parse_args(): if not args.check_refs and not args.platform: parser.print_help(sys.stderr) - exit(1) + sys.exit(1) return args diff --git a/install/macos/build-app.py b/install/macos/build-app.py index a9617a9..c22f336 100755 --- a/install/macos/build-app.py +++ b/install/macos/build-app.py @@ -7,6 +7,7 @@ import itertools import os import shutil import subprocess +import sys root = os.path.dirname( os.path.dirname( @@ -69,7 +70,7 @@ def sign_app_bundle(build_path, dist_path, app_path): # Detect if create-dmg is installed if not os.path.exists(app_path): print(f"ERROR: Dangerzone.app not found in {app_path}.") - exit(1) + sys.exit(1) dmg_path = os.path.join(dist_path, "Dangerzone.dmg") icon_path = os.path.join(root, "install", "macos", "dangerzone.icns") diff --git a/tests/test_large_set.py b/tests/test_large_set.py index ed663f0..e9c9b43 100644 --- a/tests/test_large_set.py +++ b/tests/test_large_set.py @@ -1,6 +1,7 @@ import os import re import subprocess +import sys import time from pathlib import Path from typing import List @@ -23,7 +24,7 @@ FORMATS_REGEX = ( def ensure_test_data_exists() -> None: if len(os.listdir(test_docs_repo_dir)) == 0: print("Test data repository it empty. Skipping large tests.") - exit(1) + sys.exit(1) def get_test_docs(min_size: int, max_size: int) -> List[Path]: