Thanks Simon Charette for the initial patch, and Jan Bednařík for his work on the ticket. Backport of b740da35 from master.