|
15 | 15 | from collections import OrderedDict |
16 | 16 | from contextlib import contextmanager |
17 | 17 |
|
18 | | -from .compat import StringIO, PY2, WIN, text_type |
| 18 | +from .compat import StringIO, PY2 |
19 | 19 |
|
20 | 20 | if TYPE_CHECKING: # pragma: no cover |
21 | 21 | if sys.version_info >= (3, 6): |
@@ -109,6 +109,17 @@ def parse_stream(stream): |
109 | 109 | yield binding |
110 | 110 |
|
111 | 111 |
|
| 112 | +def to_env(text): |
| 113 | + # type: (Text) -> str |
| 114 | + """ |
| 115 | + Encode a string the same way whether it comes from the environment or a `.env` file. |
| 116 | + """ |
| 117 | + if PY2: |
| 118 | + return text.encode(sys.getfilesystemencoding() or "utf-8") |
| 119 | + else: |
| 120 | + return text |
| 121 | + |
| 122 | + |
112 | 123 | class DotEnv(): |
113 | 124 |
|
114 | 125 | def __init__(self, dotenv_path, verbose=False, encoding=None): |
@@ -156,13 +167,7 @@ def set_as_environment_variables(self, override=False): |
156 | 167 | for k, v in self.dict().items(): |
157 | 168 | if k in os.environ and not override: |
158 | 169 | continue |
159 | | - # With Python2 on Windows, force environment variables to str to avoid |
160 | | - # "TypeError: environment can only contain strings" in Python's subprocess.py. |
161 | | - if PY2 and WIN: |
162 | | - if isinstance(k, text_type) or isinstance(v, text_type): |
163 | | - k = k.encode('ascii') |
164 | | - v = v.encode('ascii') |
165 | | - os.environ[k] = v # type: ignore |
| 170 | + os.environ[to_env(k)] = to_env(v) |
166 | 171 |
|
167 | 172 | return True |
168 | 173 |
|
|
0 commit comments