diff mbox series

fetch2/wget: clean up netrc usage

Message ID 20230214160751.2391568-1-ross.burton@arm.com
State Accepted, archived
Commit f4ebb27616ac2df27c29a6052b1526a4c48db607
Headers show
Series fetch2/wget: clean up netrc usage | expand

Commit Message

Ross Burton Feb. 14, 2023, 4:07 p.m. UTC
Assigning a return value which is potentially None to a tuple and
catching TypeError is pretty ugly.  Rewrite the code to explicitly check
the value for clarity.

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 bitbake/lib/bb/fetch2/wget.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Paulo Neves Feb. 16, 2023, 9:58 a.m. UTC | #1
Your commit mentions TypeError but you also removed ImportError and
IOError. Why were they there and why are they removed?

Paulo Neves
On 2/14/23 17:07, Ross Burton wrote:
> Assigning a return value which is potentially None to a tuple and
> catching TypeError is pretty ugly.  Rewrite the code to explicitly check
> the value for clarity.
>
> Signed-off-by: Ross Burton <ross.burton@arm.com>
> ---
>   bitbake/lib/bb/fetch2/wget.py | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/bitbake/lib/bb/fetch2/wget.py b/bitbake/lib/bb/fetch2/wget.py
> index 696e9180303..6d365729e0c 100644
> --- a/bitbake/lib/bb/fetch2/wget.py
> +++ b/bitbake/lib/bb/fetch2/wget.py
> @@ -361,10 +361,11 @@ class Wget(FetchMethod):
>
>                   try:
>                       import netrc
> -                    n = netrc.netrc()
> -                    login, unused, password = n.authenticators(urllib.parse.urlparse(uri).hostname)
> -                    add_basic_auth("%s:%s" % (login, password), r)
> -                except (TypeError, ImportError, IOError, netrc.NetrcParseError):
> +                    auth_data = netrc.netrc().authenticators(urllib.parse.urlparse(uri).hostname)
> +                    if auth_data:
> +                        login, _, password = auth_data
> +                        add_basic_auth("%s:%s" % (login, password), r)
> +                except (FileNotFoundError, netrc.NetrcParseError):
>                       pass
>
>                   with opener.open(r, timeout=30) as response:
> --
> 2.34.1
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#14419): https://lists.openembedded.org/g/bitbake-devel/message/14419
> Mute This Topic: https://lists.openembedded.org/mt/96962989/4454782
> Group Owner: bitbake-devel+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [paulo@myneves.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
Ross Burton Feb. 16, 2023, 11:11 a.m. UTC | #2
On 16 Feb 2023, at 09:58, Paulo Neves <paulo@myneves.com> wrote:
> 
> Your commit mentions TypeError but you also removed ImportError and
> IOError. Why were they there and why are they removed?

ImportError makes no sense as netrc is a standard module.

IOError was used to catch FileNotFoundError, but the netrc class documents that it throws FileNotFound if there isn’t a netrc, so that’s expected behaviour that we should silently handle.  Any other disk IO errors are a problem that should be raised.

Ross
diff mbox series

Patch

diff --git a/bitbake/lib/bb/fetch2/wget.py b/bitbake/lib/bb/fetch2/wget.py
index 696e9180303..6d365729e0c 100644
--- a/bitbake/lib/bb/fetch2/wget.py
+++ b/bitbake/lib/bb/fetch2/wget.py
@@ -361,10 +361,11 @@  class Wget(FetchMethod):
 
                 try:
                     import netrc
-                    n = netrc.netrc()
-                    login, unused, password = n.authenticators(urllib.parse.urlparse(uri).hostname)
-                    add_basic_auth("%s:%s" % (login, password), r)
-                except (TypeError, ImportError, IOError, netrc.NetrcParseError):
+                    auth_data = netrc.netrc().authenticators(urllib.parse.urlparse(uri).hostname)
+                    if auth_data:
+                        login, _, password = auth_data
+                        add_basic_auth("%s:%s" % (login, password), r)
+                except (FileNotFoundError, netrc.NetrcParseError):
                     pass
 
                 with opener.open(r, timeout=30) as response: