from __future__ import absolute_import, print_function

import platform
import sys

_PY2 = sys.version_info[0] == 2


def is_string(s):
    if _PY2:  # pragma: no cover (py2/py3)
        return isinstance(s, basestring)  # pragma: no cover (py2/py3) # noqa
    else:  # pragma: no cover (py2/py3)
        return isinstance(s, str)  # pragma: no cover (py2/py3)


def env_without_unicode(environ):
    # On Windows / Python 2.7, Popen explodes if given unicode strings in the environment.
    if _PY2 and platform.system() == 'Windows':  # pragma: no cover (py2/py3)
        environ_copy = dict()
        for key, value in environ.items():
            assert isinstance(key, basestring)  # noqa
            assert isinstance(key, str)
            if isinstance(value, unicode):  # noqa
                environ_copy[key] = value.encode()
                assert isinstance(environ_copy[key], str)
            elif not isinstance(value, str):
                raise TypeError("Environment contains non-unicode non-str value %r" % value)
            else:
                environ_copy[key] = value
        return environ_copy
    else:  # pragma: no cover (py2/py3)
        return environ

from __future__ import absolute_import, print_function

import platform
import sys

_PY2 = sys.version_info[0] == 2


def is_string(s):
    if _PY2:  # pragma: no cover (py2/py3)
        return isinstance(s, basestring)  # pragma: no cover (py2/py3) # noqa
    else:  # pragma: no cover (py2/py3)
        return isinstance(s, str)  # pragma: no cover (py2/py3)


def env_without_unicode(environ):
    # On Windows / Python 2.7, Popen explodes if given unicode strings in the environment.
    if _PY2 and platform.system() == 'Windows':  # pragma: no cover (py2/py3)
        environ_copy = dict()
        for key, value in environ.items():
            if isinstance(key, unicode):  # noqa
                key = key.encode()
            if isinstance(value, unicode):  # noqa
                value = value.encode()
            assert isinstance(key, str)
            assert isinstance(value, str)
            environ_copy[key] = value
        return environ_copy
    else:  # pragma: no cover (py2/py3)
        return environ

Fix unicode keys in addition to values for windows/py2 environment
Fix unicode keys in addition to values for windows/py2 environment

  from __future__ import absolute_import, print_function
  
  import platform
  import sys
  
  _PY2 = sys.version_info[0] == 2
  
  
  def is_string(s):
      if _PY2:  # pragma: no cover (py2/py3)
          return isinstance(s, basestring)  # pragma: no cover (py2/py3) # noqa
      else:  # pragma: no cover (py2/py3)
          return isinstance(s, str)  # pragma: no cover (py2/py3)
  
  
  def env_without_unicode(environ):
      # On Windows / Python 2.7, Popen explodes if given unicode strings in the environment.
      if _PY2 and platform.system() == 'Windows':  # pragma: no cover (py2/py3)
          environ_copy = dict()
          for key, value in environ.items():
-             assert isinstance(key, basestring)  # noqa
+             if isinstance(key, unicode):  # noqa
+                 key = key.encode()
+             if isinstance(value, unicode):  # noqa
+                 value = value.encode()
              assert isinstance(key, str)
-             if isinstance(value, unicode):  # noqa
-                 environ_copy[key] = value.encode()
-                 assert isinstance(environ_copy[key], str)
-             elif not isinstance(value, str):
+             assert isinstance(value, str)
-                 raise TypeError("Environment contains non-unicode non-str value %r" % value)
-             else:
-                 environ_copy[key] = value
+             environ_copy[key] = value
          return environ_copy
      else:  # pragma: no cover (py2/py3)
          return environ
  
Fix unicode keys in addition to values for windows/py2 environment
## Code Before:
from __future__ import absolute_import, print_function

import platform
import sys

_PY2 = sys.version_info[0] == 2


def is_string(s):
    if _PY2:  # pragma: no cover (py2/py3)
        return isinstance(s, basestring)  # pragma: no cover (py2/py3) # noqa
    else:  # pragma: no cover (py2/py3)
        return isinstance(s, str)  # pragma: no cover (py2/py3)


def env_without_unicode(environ):
    # On Windows / Python 2.7, Popen explodes if given unicode strings in the environment.
    if _PY2 and platform.system() == 'Windows':  # pragma: no cover (py2/py3)
        environ_copy = dict()
        for key, value in environ.items():
            assert isinstance(key, basestring)  # noqa
            assert isinstance(key, str)
            if isinstance(value, unicode):  # noqa
                environ_copy[key] = value.encode()
                assert isinstance(environ_copy[key], str)
            elif not isinstance(value, str):
                raise TypeError("Environment contains non-unicode non-str value %r" % value)
            else:
                environ_copy[key] = value
        return environ_copy
    else:  # pragma: no cover (py2/py3)
        return environ

## Instruction:
Fix unicode keys in addition to values for windows/py2 environment
## Code After:
from __future__ import absolute_import, print_function

import platform
import sys

_PY2 = sys.version_info[0] == 2


def is_string(s):
    if _PY2:  # pragma: no cover (py2/py3)
        return isinstance(s, basestring)  # pragma: no cover (py2/py3) # noqa
    else:  # pragma: no cover (py2/py3)
        return isinstance(s, str)  # pragma: no cover (py2/py3)


def env_without_unicode(environ):
    # On Windows / Python 2.7, Popen explodes if given unicode strings in the environment.
    if _PY2 and platform.system() == 'Windows':  # pragma: no cover (py2/py3)
        environ_copy = dict()
        for key, value in environ.items():
            if isinstance(key, unicode):  # noqa
                key = key.encode()
            if isinstance(value, unicode):  # noqa
                value = value.encode()
            assert isinstance(key, str)
            assert isinstance(value, str)
            environ_copy[key] = value
        return environ_copy
    else:  # pragma: no cover (py2/py3)
        return environ
