From ac946803a5d96cbb0d5d31c8a29230d9b7b69196 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sat, 2 Mar 2019 18:35:27 -0800 Subject: Fix default encoding issue --- CHANGELOG.md | 1 + isort/__main__.py | 6 +++++- isort/pie_slice.py | 27 ++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f6b2758..20261be4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Changelog - Fixed relative import sorting bug (Issue #417) - Fixed "no_lines_before" to also be respected from previous empty sections. - Fixed slow-down introduced by finders mechanism by adding a LRU cache (issue #848) +- Fixed issue #842 default encoding not-set in Python2 ### 4.3.9 - Feburary 25, 2019 - hot fix release - Fixed a bug that led to an incompatibility with black: #831 diff --git a/isort/__main__.py b/isort/__main__.py index 186c98e8..91cc154d 100644 --- a/isort/__main__.py +++ b/isort/__main__.py @@ -1,5 +1,9 @@ from __future__ import absolute_import -from isort.main import main +from isort.pie_slice import apply_changes_to_python_environment + +apply_changes_to_python_environment() + +from isort.main import main # noqa: E402 isort:skip main() diff --git a/isort/pie_slice.py b/isort/pie_slice.py index 2209cb4e..569ea76e 100644 --- a/isort/pie_slice.py +++ b/isort/pie_slice.py @@ -30,14 +30,39 @@ PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 VERSION = sys.version_info -__all__ = ['PY2', 'PY3', 'lru_cache'] +__all__ = ['PY2', 'PY3', 'lru_cache', 'apply_changes_to_python_environment'] if PY3: input = input + + def apply_changes_to_python_environment(): + pass else: input = raw_input # noqa: F821 + python_environment_changes_applied = False + + import sys + stdout = sys.stdout + stderr = sys.stderr + + def apply_changes_to_python_environment(): + global python_environment_changes_applied + if python_environment_changes_applied or sys.getdefaultencoding() == 'utf-8': + python_environment_changes_applied = True + return + + try: + reload(sys) + sys.stdout = stdout + sys.stderr = stderr + sys.setdefaultencoding('utf-8') + except NameError: # Python 3 + sys.exit('This should not happen!') + + python_environment_changes_applied = True + if sys.version_info < (3, 2): try: -- cgit v1.2.1