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
This commit is contained in:
Alex Pyrgiotis 2024-04-30 13:13:50 +03:00
parent d6202cd028
commit e11aaec3ac
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA
6 changed files with 11 additions and 8 deletions

View file

@ -1,5 +1,6 @@
import functools import functools
import os import os
import sys
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
import click import click
@ -86,7 +87,7 @@ def check_suspicious_options(args: List[str]) -> None:
f" current working directory: {filenames_str}" f" current working directory: {filenames_str}"
) )
click.echo(msg) click.echo(msg)
exit(1) sys.exit(1)
def override_parser_and_check_suspicious_options(click_main: click.Command) -> None: def override_parser_and_check_suspicious_options(click_main: click.Command) -> None:

View file

@ -67,7 +67,7 @@ def cli_main(
dangerzone.add_document_from_filename(filenames[0], output_filename, archive) dangerzone.add_document_from_filename(filenames[0], output_filename, archive)
elif len(filenames) > 1 and output_filename: elif len(filenames) > 1 and output_filename:
click.echo("--output-filename can only be used with one input file.") click.echo("--output-filename can only be used with one input file.")
exit(1) sys.exit(1)
else: else:
for filename in filenames: for filename in filenames:
dangerzone.add_document_from_filename(filename, archive=archive) dangerzone.add_document_from_filename(filename, archive=archive)
@ -83,7 +83,7 @@ def cli_main(
click.echo("Invalid OCR language code. Valid language codes:") click.echo("Invalid OCR language code. Valid language codes:")
for lang in dangerzone.ocr_languages: for lang in dangerzone.ocr_languages:
click.echo(f"{dangerzone.ocr_languages[lang]}: {lang}") click.echo(f"{dangerzone.ocr_languages[lang]}: {lang}")
exit(1) sys.exit(1)
# Ensure container is installed # Ensure container is installed
dangerzone.isolation_provider.install() dangerzone.isolation_provider.install()

View file

@ -107,6 +107,6 @@ def handle_document_errors(func: F) -> F:
msg = "An exception occured while validating a document" msg = "An exception occured while validating a document"
log.exception(msg) log.exception(msg)
click.echo(str(e)) click.echo(str(e))
exit(1) sys.exit(1)
return cast(F, wrapper) return cast(F, wrapper)

View file

@ -430,7 +430,7 @@ class Reference:
" to date with the respective doc section, and then update the cached" " to date with the respective doc section, and then update the cached"
" section in this file." " section in this file."
) )
exit(1) sys.exit(1)
def find_section_text(self, md_text): def find_section_text(self, md_text):
"""Find a section's content in a provided Markdown string.""" """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: if not args.check_refs and not args.platform:
parser.print_help(sys.stderr) parser.print_help(sys.stderr)
exit(1) sys.exit(1)
return args return args

View file

@ -7,6 +7,7 @@ import itertools
import os import os
import shutil import shutil
import subprocess import subprocess
import sys
root = os.path.dirname( root = os.path.dirname(
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 # Detect if create-dmg is installed
if not os.path.exists(app_path): if not os.path.exists(app_path):
print(f"ERROR: Dangerzone.app not found in {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") dmg_path = os.path.join(dist_path, "Dangerzone.dmg")
icon_path = os.path.join(root, "install", "macos", "dangerzone.icns") icon_path = os.path.join(root, "install", "macos", "dangerzone.icns")

View file

@ -1,6 +1,7 @@
import os import os
import re import re
import subprocess import subprocess
import sys
import time import time
from pathlib import Path from pathlib import Path
from typing import List from typing import List
@ -23,7 +24,7 @@ FORMATS_REGEX = (
def ensure_test_data_exists() -> None: def ensure_test_data_exists() -> None:
if len(os.listdir(test_docs_repo_dir)) == 0: if len(os.listdir(test_docs_repo_dir)) == 0:
print("Test data repository it empty. Skipping large tests.") 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]: def get_test_docs(min_size: int, max_size: int) -> List[Path]: