This is a modified version of a patch proposed by Humberto Diogenes in the discussion of issue1883. I will merge manually this change into the py3k branch: the tests must be adapted.