FWIW, I'm pretty sure that any code-quality tool (like Flake8 or PyLint) will complain about redefinitions here (and below for chardet_detect). While we do not strictly follow those tools, it might be better to move their imports/definitions into an else block for this condition.