Fix initializing of configtypes.QtFont when default_size is None

When initially reading a config.py and trying to validate QtFont values,
Font.default_size is still set to None. This is because it needs to be
initialized later in _late_init, as doing so requires a QApplication.

This results in the following error while running config.py:

  Traceback (most recent call last):
    File "/usr/lib/python3.8/site-packages/qutebrowser/config/configfiles.py", line 731, in read_config_py
      exec(code, module.__dict__)
    File ".../.config/qutebrowser/config.py", line 52, in <module>
      c.fonts.debug_console = 'default_size default_family'
    File "/usr/lib/python3.8/site-packages/qutebrowser/config/config.py", line 611, in __setattr__
      self._config.set_obj(name, value, pattern=self._pattern)
    File "/usr/lib/python3.8/site-packages/qutebrowser/config/config.py", line 457, in set_obj
      self._set_value(opt, value, pattern=pattern,
    File "/usr/lib/python3.8/site-packages/qutebrowser/config/config.py", line 317, in _set_value
      opt.typ.to_py(value)  # for validation
    File "/usr/lib/python3.8/site-packages/qutebrowser/config/configtypes.py", line 1336, in to_py
      if size.lower().endswith('pt'):
  AttributeError: 'NoneType' object has no attribute 'lower'

Similarly to what's done for configtypes.Font, assume that a None size is fine.
Note that the default_size setting already gets validated via a separate regex
anyways.

Our tests didn't catch this because the init_patch patching already set
default_size to a non-None value. While this makes sense for the rest of the
tests, in test_configinit we really should have the real default value.

Fixes #5223

(cherry picked from commit d68f484b6d003e5708fe390a921175c6c7777641)
This commit is contained in:
Florian Bruhin 2020-02-12 13:51:42 +01:00
parent 303bc02175
commit 0e3f6e6b87
2 changed files with 6 additions and 2 deletions

View File

@ -1333,7 +1333,10 @@ class QtFont(Font):
if size == 'default_size':
size = self.default_size
if size.lower().endswith('pt'):
if size is None:
# initial validation before default_size is set up.
pass
elif size.lower().endswith('pt'):
font.setPointSizeF(float(size[:-2]))
elif size.lower().endswith('px'):
font.setPixelSize(int(size[:-2]))

View File

@ -41,7 +41,7 @@ def init_patch(qapp, fake_save_manager, monkeypatch, config_tmpdir,
monkeypatch.setattr(config, 'change_filters', [])
monkeypatch.setattr(configinit, '_init_errors', None)
monkeypatch.setattr(configtypes.Font, 'default_family', None)
monkeypatch.setattr(configtypes.Font, 'default_size', '10pt')
monkeypatch.setattr(configtypes.Font, 'default_size', None)
yield
try:
objreg.delete('config-commands')
@ -356,6 +356,7 @@ class TestLateInit:
"""Ensure setting fonts.default_family at init works properly.
See https://github.com/qutebrowser/qutebrowser/issues/2973
and https://github.com/qutebrowser/qutebrowser/issues/5223
"""
if method == 'temp':
args.temp_settings = settings