Improve parsing of invalid keys

This should handle "<>" and "\x1f" correctly.
This commit is contained in:
Florian Bruhin 2018-03-06 06:29:03 +01:00
parent 7a9f8fda72
commit 41dfa29648
2 changed files with 17 additions and 9 deletions

View File

@ -413,12 +413,12 @@ class KeySequence:
def _validate(self, keystr=None): def _validate(self, keystr=None):
for info in self: for info in self:
assert Qt.Key_Space <= info.key <= Qt.Key_unknown, info.key if info.key < Qt.Key_Space or info.key >= Qt.Key_unknown:
if info.key == Qt.Key_unknown: raise KeyParseError(keystr, "Got invalid key!")
raise KeyParseError(keystr, "Got unknown key!")
for seq in self._sequences: for seq in self._sequences:
assert seq if not seq:
raise KeyParseError(keystr, "Got invalid key!")
def matches(self, other): def matches(self, other):
"""Check whether the given KeySequence matches with this one. """Check whether the given KeySequence matches with this one.

View File

@ -19,6 +19,8 @@
import operator import operator
import hypothesis
from hypothesis import strategies
import pytest import pytest
from PyQt5.QtCore import Qt, QEvent, pyqtSignal from PyQt5.QtCore import Qt, QEvent, pyqtSignal
from PyQt5.QtGui import QKeyEvent, QKeySequence from PyQt5.QtGui import QKeyEvent, QKeySequence
@ -203,15 +205,11 @@ class TestKeySequence:
seq = keyutils.KeySequence() seq = keyutils.KeySequence()
assert not seq assert not seq
@pytest.mark.parametrize('key', [Qt.Key_unknown, -1]) @pytest.mark.parametrize('key', [Qt.Key_unknown, -1, '\x1f', 0])
def test_init_unknown(self, key): def test_init_unknown(self, key):
with pytest.raises(keyutils.KeyParseError): with pytest.raises(keyutils.KeyParseError):
keyutils.KeySequence(key) keyutils.KeySequence(key)
def test_init_invalid(self):
with pytest.raises(AssertionError):
keyutils.KeySequence(0)
@pytest.mark.parametrize('orig, normalized', [ @pytest.mark.parametrize('orig, normalized', [
('<Control+x>', '<Ctrl+x>'), ('<Control+x>', '<Ctrl+x>'),
('<Windows+x>', '<Meta+x>'), ('<Windows+x>', '<Meta+x>'),
@ -410,6 +408,7 @@ class TestKeySequence:
('<alt+<>', keyutils.KeyParseError), ('<alt+<>', keyutils.KeyParseError),
('<alt+>>', keyutils.KeyParseError), ('<alt+>>', keyutils.KeyParseError),
('<blub>', keyutils.KeyParseError), ('<blub>', keyutils.KeyParseError),
('<>', keyutils.KeyParseError),
('\U00010000', keyutils.KeyParseError), ('\U00010000', keyutils.KeyParseError),
]) ])
def test_parse(self, keystr, expected): def test_parse(self, keystr, expected):
@ -419,6 +418,15 @@ class TestKeySequence:
else: else:
assert keyutils.KeySequence.parse(keystr) == expected assert keyutils.KeySequence.parse(keystr) == expected
@hypothesis.given(strategies.text())
def test_parse_hypothesis(self, keystr):
try:
seq = keyutils.KeySequence.parse(keystr)
except keyutils.KeyParseError:
pass
else:
str(seq)
def test_key_info_from_event(): def test_key_info_from_event():
ev = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.ShiftModifier, 'A') ev = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.ShiftModifier, 'A')