summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2020-05-26 20:20:56 +0200
committerFlorian Bruhin <me@the-compiler.org>2020-05-26 20:25:08 +0200
commite8b0ce7597627a5b5312a214a84ff2aed9bcdfe0 (patch)
treebe8423aa6432e4e014743b884ec6fff69945efac
parent0d191a47bb34959102ddc2e1ecc07896dab3eda6 (diff)
downloadqutebrowser-e8b0ce7597627a5b5312a214a84ff2aed9bcdfe0.tar.gz
qutebrowser-e8b0ce7597627a5b5312a214a84ff2aed9bcdfe0.zip
Add filename if QtOSError is used with QFileDevice
If we have a filename available, let's add it to the error message. This also effectively reverts 00747be9d3790534e8b32464605d1b5b6c2d6627 since that's not needed anymore (Qt 5.7 is the oldest supported release).
-rw-r--r--qutebrowser/utils/qtutils.py19
-rw-r--r--tests/unit/utils/test_qtutils.py6
2 files changed, 13 insertions, 12 deletions
diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py
index 3e8afae3f..109d2dfed 100644
--- a/qutebrowser/utils/qtutils.py
+++ b/qutebrowser/utils/qtutils.py
@@ -35,7 +35,7 @@ import typing
import pkg_resources
from PyQt5.QtCore import (qVersion, QEventLoop, QDataStream, QByteArray,
- QIODevice, QSaveFile, QT_VERSION_STR,
+ QIODevice, QFileDevice, QSaveFile, QT_VERSION_STR,
PYQT_VERSION_STR, QObject, QUrl)
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QApplication
@@ -44,9 +44,6 @@ try:
except ImportError: # pragma: no cover
qWebKitVersion = None # type: ignore[assignment] # noqa: N816
-if typing.TYPE_CHECKING:
- from PyQt5.QtCore import QFileDevice
-
from qutebrowser.misc import objects
from qutebrowser.utils import usertypes
@@ -74,13 +71,17 @@ class QtOSError(OSError):
if msg is None:
msg = dev.errorString()
+ self.qt_errno = None # type: typing.Optional[QFileDevice.FileError]
+ if isinstance(dev, QFileDevice):
+ msg = self._init_filedev(dev, msg)
+
super().__init__(msg)
- self.qt_errno = None # type: typing.Optional[QFileDevice.FileError]
- try:
- self.qt_errno = dev.error()
- except AttributeError:
- pass
+ def _init_filedev(self, dev: QFileDevice, msg: str) -> str:
+ self.qt_errno = dev.error()
+ filename = dev.fileName()
+ msg += ": {!r}".format(filename)
+ return msg
def version_check(version: str,
diff --git a/tests/unit/utils/test_qtutils.py b/tests/unit/utils/test_qtutils.py
index 150a03f6e..81d198946 100644
--- a/tests/unit/utils/test_qtutils.py
+++ b/tests/unit/utils/test_qtutils.py
@@ -467,9 +467,9 @@ class TestSavefileOpen:
with pytest.raises(OSError) as excinfo:
with qtutils.savefile_open(str(filename)):
pass
- errors = ["Filename refers to a directory", # Qt >= 5.4
- "Commit failed!"] # older Qt versions
- assert str(excinfo.value) in errors
+
+ msg = "Filename refers to a directory: {!r}".format(str(filename))
+ assert str(excinfo.value) == msg
assert tmpdir.listdir() == [filename]
def test_failing_flush(self, tmpdir):