From 578d992a19a1d2e8345dc5522764e49aa33e6169 Mon Sep 17 00:00:00 2001 From: Fuxino Date: Thu, 8 Oct 2026 17:50:47 +0200 Subject: [PATCH] Only turn warnings into errors where needed warnings.filterwarnings('error') applied to the whole program, so any warning raised by a library (e.g. a DeprecationWarning) would crash it. Limit it to the ssh connection (unknown host key) and the password prompt (no terminal available). --- simple_backup/simple_backup.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index 9b1a9ed..246b4c8 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -37,8 +37,6 @@ from glob import glob from dotenv import load_dotenv -warnings.filterwarnings('error') - try: import paramiko from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey @@ -346,7 +344,10 @@ class Backup: ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) try: - ssh.connect(self.ssh_host, username=self.ssh_user) + # WarningPolicy emits a UserWarning for unknown host keys + with warnings.catch_warnings(): + warnings.simplefilter('error', UserWarning) + ssh.connect(self.ssh_host, username=self.ssh_user) return ssh except UserWarning: @@ -374,8 +375,12 @@ class Backup: if self.ssh_keyfile is None: try: - password = getpass( - f'{self.ssh_user}@{self.ssh_host}\'s password: ') + # Fail instead of reading the password with echo if no terminal is available + with warnings.catch_warnings(): + warnings.simplefilter('error', GetPassWarning) + password = getpass( + f'{self.ssh_user}@{self.ssh_host}\'s password: ') + ssh.connect(self.ssh_host, username=self.ssh_user, password=password)