Skip to content

Commit

Permalink
Check terminal encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickdowne committed Aug 27, 2024
1 parent 7add368 commit 1dfa326
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion ethstaker_deposit/deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,30 @@ def check_python_version() -> None:
'''
if sys.version_info < (3, 9):
click.pause(load_text(['err_python_version']))
sys.exit()
sys.exit(78)


def check_term_encoding() -> None:
'''
Checks that the terminal uses utf-8 encoding and exits if not. This is done to avoid issues with passwords
'''
encoding = sys.stdin.encoding.lower()
acceptable_encodings = ['utf-8']

if sys.platform == 'win32':
acceptable_encodings.append('cp1252') # Legacy single-byte in Americas, Western Europe, Oceania, Africa
acceptable_encodings.append('cp936') # simplified Chinese
acceptable_encodings.append('cp950') # traditional Chinese
acceptable_encodings.append('cp949') # Korean
acceptable_encodings.append('cp932') # Japanese

if encoding not in acceptable_encodings:
click.pause(
f'Your terminal is using {encoding} encoding which can present problems with passwords. '
'Please use "utf-8".'
+ (' On Windows, "cp1252" is acceptable but not recommended.' if sys.platform == 'win32' else '')
)
sys.exit(78)


def check_connectivity() -> None:
Expand Down Expand Up @@ -109,6 +132,7 @@ def cli(ctx: click.Context, language: str, non_interactive: bool, ignore_connect
def run() -> None:
freeze_support() # Needed when running under Windows in a frozen bundle
check_python_version()
check_term_encoding()

try:
cli()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def test_should_notify_user_and_exit_if_invalid_python_version(monkeypatch) -> None:
exit_called = False

def _mock_sys_exit():
def _mock_sys_exit(arg):
nonlocal exit_called
exit_called = True

Expand Down

0 comments on commit 1dfa326

Please sign in to comment.