Backport of #9500 to stable6

It is not a simple cherry-pick backport because stable6 and master differ too much in this regard. OTOH the changes are not overly big. Also to see whether  autotests runs correctly (locally unrelated tests in preferences are failing for me, but because of my setup).