Reintroduce py3 compatibility from e62180e219dce after merge conflicts