|
45 | 45 | import threading |
46 | 46 | import warnings |
47 | 47 |
|
48 | | -if sys.version_info[0] == 3: |
49 | | - string_types = str |
50 | | -else: |
| 48 | +PY2 = sys.version_info[0] == 2 |
| 49 | +if PY2: |
51 | 50 | string_types = basestring |
| 51 | +else: |
| 52 | + string_types = str |
52 | 53 |
|
53 | 54 | #-------- Globals -----------# |
54 | 55 |
|
@@ -491,17 +492,22 @@ def __init__(self, cmd, cwd=None, env=None, flags=None, |
491 | 492 | # http://bugs.activestate.com/show_bug.cgi?id=75467 |
492 | 493 | cmd = '"%s"' % (cmd, ) |
493 | 494 |
|
| 495 | + # Environment variables on Python 2 + Windows must be str. |
494 | 496 | # XXX - subprocess needs to be updated to use the wide string API. |
495 | 497 | # subprocess uses a Windows API that does not accept unicode, so |
496 | 498 | # we need to convert all the environment variables to strings |
497 | 499 | # before we make the call. Temporary fix to bug: |
498 | 500 | # http://bugs.activestate.com/show_bug.cgi?id=72311 |
499 | | - if env: |
| 501 | + if envandPY2: |
500 | 502 | encoding = sys.getfilesystemencoding() |
501 | 503 | _enc_env = {} |
502 | 504 | for key, value in env.items(): |
503 | 505 | try: |
504 | | - _enc_env[key.encode(encoding)] = value.encode(encoding) |
| 506 | + if not isinstance(key, str): |
| 507 | + key = key.encode(encoding) |
| 508 | + if not isinstance(value, str): |
| 509 | + value = value.encode(encoding) |
| 510 | + _enc_env[key] = value |
505 | 511 | except UnicodeEncodeError: |
506 | 512 | # Could not encode it, warn we are dropping it. |
507 | 513 | log.warn("Could not encode environment variable %r " |
|
0 commit comments