Also fixed a py3 incompatibility. Thanks to brutasse for the report. Backport of 3754f4ad from master.