diff options
| author | Christian Breunig <christian@breunig.cc> | 2026-03-05 21:05:36 +0000 |
|---|---|---|
| committer | Christian Breunig <christian@breunig.cc> | 2026-03-05 22:18:56 +0100 |
| commit | 838cf65b1c181b7b4f00ae712166065bad0fbf54 (patch) | |
| tree | e687dff62b2ec17016cd599fcc95cbf027f6c2bd /python | |
| parent | 2d73f1e8195a2f5edffcf95a4741885cfcd0bf6c (diff) | |
| download | vyos-1x-838cf65b1c181b7b4f00ae712166065bad0fbf54.tar.gz vyos-1x-838cf65b1c181b7b4f00ae712166065bad0fbf54.zip | |
remote: T8347: fix variable assignment for try/except block
Pylint is pointing out that because sftp.put(location, path) is located inside
a finally block, it is guaranteed to execute regardless of whether the try
block succeeds or fails.
If an exception other than an IOError is raised inside the try block,
"except IOError" block gets skipped, Python then jumps straight to the finally
block. When it executes the finally block, it attempts to read the variable
path, but path was never assigned!
This triggers UnboundLocalError, local variable 'path' referenced before
assignment.
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/remote.py | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/python/vyos/remote.py b/python/vyos/remote.py index d4efac0d9..417f9d42c 100644 --- a/python/vyos/remote.py +++ b/python/vyos/remote.py @@ -220,23 +220,21 @@ class SshC: def upload(self, location: str): with self._establish() as ssh, ssh.open_sftp() as sftp: + # A file exists at this destination. We're simply going to clobber it. + # This is our default fallback + path = self.path try: # If the remote path is a directory, use the original filename. if stat.S_ISDIR(sftp.stat(self.path).st_mode): path = os.path.join(self.path, os.path.basename(location)) - # A file exists at this destination. We're simply going to clobber it. - else: - path = self.path - # This path doesn't point at any existing file. We can freely use this filename. except IOError: - path = self.path - finally: - if self.progressbar: - with Progressbar() as p: - sftp.put(location, path, callback=p.progress) - else: - sftp.put(location, path) + pass + if self.progressbar: + with Progressbar() as p: + sftp.put(location, path, callback=p.progress) + else: + sftp.put(location, path) class HttpC: def __init__(self, |
