Backport patch from python issue17980 PYTHON-522