]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Check `~/.netrc` and `~/_netrc` files by default (#189)
authorCan Sarıgöl <cansarigol@derinbilgi.com.tr>
Tue, 6 Aug 2019 13:31:04 +0000 (16:31 +0300)
committerSeth Michael Larson <sethmichaellarson@gmail.com>
Tue, 6 Aug 2019 13:31:04 +0000 (08:31 -0500)
httpx/utils.py

index 4831578dd5d173b19ab9c6ed729eac7d9f3c0491..61493a6a3de686914150ac42082428a37f761a6e 100644 (file)
@@ -83,10 +83,24 @@ def guess_json_utf(data: bytes) -> typing.Optional[str]:
     return None
 
 
+NETRC_STATIC_FILES = tuple(
+    os.path.expanduser(path) for path in ("~/.netrc", "~/_netrc")
+)
+
+
 def get_netrc_login(host: str) -> typing.Optional[typing.Tuple[str, str, str]]:
-    try:
-        netrc_info = netrc.netrc(os.environ.get("NETRC"))  # type: ignore
-    except FileNotFoundError:
+    NETRC_FILES = (
+        (os.environ["NETRC"],) if "NETRC" in os.environ else NETRC_STATIC_FILES
+    )
+    netrc_path = None
+
+    for file_path in NETRC_FILES:
+        if os.path.isfile(file_path):
+            netrc_path = file_path
+            break
+
+    if netrc_path is None:
         return None
 
+    netrc_info = netrc.netrc(netrc_path)
     return netrc_info.authenticators(host)  # type: ignore