Don't change Python2 str encoding on import
isort has a Python3 compatibility module that, among other things, changes the Python environment when imported. These changes aren't restricted to the module importing; they apply to all code running in the environment. This is fine when executing isort as a command. However, this leads to weird issues when isort is used as a library, e.g. pytest-isort, because once loaded, it changes the behaviour of Python from that point onwards. This led to an interesting issue where, depending on if pytest-isort (which imports isort) is installed, UnicodeDecodeErrors are or aren't raised on Python 2.7 due to the sys.setdefaultencoding call. See https://github.com/moccu/pytest-isort/issues/10 I don't think isort should be changing the Python environment through the simple act of importing the code - it's totally unexpected. In this PR, I've extracted the code that changes the Python environment into a separate function that's only called if isort is executed as a command. An unfortunate side-effect of this PR is that for some users, this may lead to new exceptions now that the default encoding is no longer being changed (arguably it should not be changed at all - https://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script).
Loading
Please register or sign in to comment