[elbe-devel] [PATCH] debinstall/hashes: improve error handling

Torben Hohn torben.hohn at linutronix.de
Wed Nov 21 09:33:32 CET 2018


On Fri, Nov 16, 2018 at 05:28:24PM +0100, Manuel Traut wrote:
> preinit file variable that close doesn't use uninitialized variable.
> 
> print info about IOError that is raised if urlopen fails.

the IOError should be printed in higher layers. 

the correct pattern is also:

--------------------------------------------
rf = urlopen(url, None, 10)
try:
	with open(local_fname, "w") as wf:
		copyfileobj(rf, wf)
finally:
        rf.close()
--------------------------------------------

the exception should be caught 
in copy_kinitrd()


> 
> Signed-off-by: Manuel Traut <manut at linutronix.de>
> ---
>  elbepack/debinstaller.py | 11 ++++++++++-
>  elbepack/hashes.py       |  7 +++++++
>  2 files changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/elbepack/debinstaller.py b/elbepack/debinstaller.py
> index d251f202..9d838188 100644
> --- a/elbepack/debinstaller.py
> +++ b/elbepack/debinstaller.py
> @@ -115,10 +115,14 @@ def setup_apt_keyring(gpg_home, keyring_fname):
>  
>  
>  def download(url, local_fname):
> +    rf = None
>      try:
>          rf = urlopen(url, None, 10)
>          with open(local_fname, "w") as wf:
>              copyfileobj(rf, wf)
> +    except IOError as e:
> +        print("download: %s FAILED" % url, file=sys.stderr)
> +        raise e
>      finally:
>          rf.close()
>  
> @@ -138,8 +142,10 @@ def download_release(tmp, base_url):
>      # open downloaded plaintext file, and
>      # use the urlopen object of the Release.gpg
>      # directtly.
> +    sig = None
>      try:
> -        sig = urlopen(base_url + 'Release.gpg', None, 10)
> +        url = base_url + 'Release.gpg'
> +        sig = urlopen(url, None, 10)
>          with tmp.open("Release", "r") as signed:
>  
>              overall_status = OverallStatus()
> @@ -153,6 +159,9 @@ def download_release(tmp, base_url):
>  
>              if overall_status.to_exitcode():
>                  raise InvalidSignature('Failed to verify Release file')
> +    except IOError as e:
> +        print("download: %s FAILED" % url, file=sys.stderr)
> +        raise e
>      finally:
>          sig.close()
>  
> diff --git a/elbepack/hashes.py b/elbepack/hashes.py
> index d2dfe053..f27d7811 100644
> --- a/elbepack/hashes.py
> +++ b/elbepack/hashes.py
> @@ -3,7 +3,11 @@
>  #
>  # SPDX-License-Identifier: GPL-3.0-or-later
>  
> +from __future__ import print_function
> +
>  import hashlib
> +import sys
> +
>  from shutil import copyfileobj
>  
>  # different module names in python 2 and 3
> @@ -62,6 +66,9 @@ class HashValidator(object):
>              rf = urlopen(url, None, 10)
>              with open(local_fname, "w") as wf:
>                  copyfileobj(rf, wf)
> +        except IOError as e:
> +            print("download: %s FAILED" % url, file=sys.stderr)
> +            raise e
>          finally:
>              if rf is not None:
>                  rf.close()
> -- 
> 2.19.1
> 
> 
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel

-- 
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/kontakt/Datenschutz.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://lists.linutronix.de/pipermail/elbe-devel/attachments/20181121/90421684/attachment.sig>


More information about the elbe-devel mailing list