Updated and improved fix for #21179

I've updated the text following several suggestions by bmispelon, and also made the code Python 3 compatible.