-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
* Force environment variables to str With Python2 on Windows, force environment variables to str to avoid "TypeError: environment can only contain strings " in Python's subprocess.py. * PEP8 fix * Add Python3 compatible * Fix flake8 fails with F821 * Fix flake8 fails with F821
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import sys | ||
try: | ||
from StringIO import StringIO # noqa | ||
except ImportError: | ||
from io import StringIO # noqa | ||
|
||
PY2 = sys.version_info[0] == 2 | ||
WIN = sys.platform.startswith('win') | ||
text_type = unicode if PY2 else str # noqa |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
import warnings | ||
from collections import OrderedDict | ||
|
||
from .compat import StringIO | ||
from .compat import StringIO, PY2, WIN, text_type | ||
|
||
__escape_decoder = codecs.getdecoder('unicode_escape') | ||
__posix_variable = re.compile('\$\{[^\}]*\}') # noqa | ||
|
@@ -95,6 +95,12 @@ def set_as_environment_variables(self, override=False): | |
for k, v in self.dict().items(): | ||
if k in os.environ and not override: | ||
continue | ||
# With Python2 on Windows, force environment variables to str to avoid | ||
# "TypeError: environment can only contain strings" in Python's subprocess.py. | ||
if PY2 and WIN: | ||
if isinstance(k, text_type) or isinstance(v, text_type): | ||
k = k.encode('ascii') | ||
v = v.encode('ascii') | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
greyli
Author
Contributor
|
||
os.environ[k] = v | ||
|
||
return True | ||
|
Is ASCII really the right encoding to use here? Environment variables often contain filesystem paths and on Windows these often contain usernames, which may have non-ASCII chars in them.
I think the default encoding to use here should be mbcs, but really
load_dotenv
andset_as_environment_variables
should allow to pass an encoding.