From abfd8fa70b54e5b9afe0f25abc4bddb4b9164ac1 Mon Sep 17 00:00:00 2001 From: Paul Kuberry Date: Thu, 26 Aug 2021 14:51:08 -0600 Subject: Conditionally remove 'context' from kwargs in _urlopen (#25316) * Conditionally remove 'context' from kwargs in _urlopen Previously, 'context' is purged from kwargs in _urlopen to conform to varying support for 'context' in different versions of urllib. This fix tries to use 'context', and then removes it if an exception is thrown and tries again. * Specify error type in try statement in _urlopen Specify TypeError when checking if 'context' is in kwargs for _urlopen. Also, if try fails, check that 'context' is in the error message before removing from kwargs. --- lib/spack/spack/util/web.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/spack/spack/util/web.py b/lib/spack/spack/util/web.py index 0922508155..72f7abc2c4 100644 --- a/lib/spack/spack/util/web.py +++ b/lib/spack/spack/util/web.py @@ -507,9 +507,9 @@ def _urlopen(req, *args, **kwargs): except AttributeError: pass - # We don't pass 'context' parameter because it was only introduced starting + # Note: 'context' parameter was only introduced starting # with versions 2.7.9 and 3.4.3 of Python. - if 'context' in kwargs: + if __UNABLE_TO_VERIFY_SSL: del kwargs['context'] opener = urlopen @@ -517,7 +517,13 @@ def _urlopen(req, *args, **kwargs): import spack.s3_handler opener = spack.s3_handler.open - return opener(req, *args, **kwargs) + try: + return opener(req, *args, **kwargs) + except TypeError as err: + # If the above fails because of 'context', call without 'context'. + if 'context' in kwargs and 'context' in str(err): + del kwargs['context'] + return opener(req, *args, **kwargs) def find_versions_of_archive( -- cgit v1.2.3-60-g2f50