Avoid trying to load .netrc if $HOME isn't set.

This logged an error on Windows:

ERROR    misc       networkmanager:on_authentication_required:269 Unable to read the netrc file
Traceback (most recent call last):
  File "c:\python34\Lib\netrc.py", line 27, in __init__
    file = os.path.join(os.environ['HOME'], ".netrc")
  File "C:\Users\florian\buildbot\slave\win8\build\.tox\py34\lib\os.py", line 633, in __getitem__
    raise KeyError(key) from None
KeyError: 'HOME'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\florian\buildbot\slave\win8\build\qutebrowser\browser\network\networkmanager.py", line 262, in on_authentication_required
    net = netrc.netrc()
  File "c:\python34\Lib\netrc.py", line 29, in __init__
    raise OSError("Could not find .netrc: $HOME is not set")

Since this case is pretty common, we don't want to log it - and checking the
variable beforehand is easier than parsing the exception message.

This should fix the failing tests on Windows.
This commit is contained in:
Florian Bruhin 2016-01-15 06:57:43 +01:00
parent 40721a2b6b
commit 9d520b7312
2 changed files with 7 additions and 1 deletions

View File

@ -32,6 +32,8 @@ Fixed
~~~~~
- Fixed completion for various config values when using `:set`.
- Prevented an error being logged when a website with HTTP authentication was
opened on Windows.
v0.5.0
------

View File

@ -19,6 +19,7 @@
"""Our own QNetworkAccessManager."""
import os
import collections
import netrc
@ -256,7 +257,10 @@ class NetworkManager(QNetworkAccessManager):
def on_authentication_required(self, reply, authenticator):
"""Called when a website needs authentication."""
user, password = None, None
if not hasattr(reply, "netrc_used"):
if not hasattr(reply, "netrc_used") and 'HOME' in os.environ:
# We'll get an OSError by netrc if 'HOME' isn't available in
# os.environ. We don't want to log that, so we prevent it
# altogether.
reply.netrc_used = True
try:
net = netrc.netrc()