diff --git a/dvc/daemon.py b/dvc/daemon.py index 48c197bb9..4891680e5 100644 --- a/dvc/daemon.py +++ b/dvc/daemon.py @@ -9,6 +9,7 @@ from subprocess import Popen import dvc.logger as logger from dvc.utils import is_binary, fix_env +from dvc.utils.compat import cast_bytes_py2 CREATE_NEW_PROCESS_GROUP = 0x00000200 @@ -78,7 +79,9 @@ def daemon(args): env = fix_env() file_path = os.path.abspath(inspect.stack()[0][1]) - env["PYTHONPATH"] = os.path.dirname(os.path.dirname(file_path)) + env["PYTHONPATH"] = cast_bytes_py2( + os.path.dirname(os.path.dirname(file_path)) + ) logger.debug("Trying to spawn '{}' with env '{}'".format(cmd, env)) diff --git a/dvc/utils/__init__.py b/dvc/utils/__init__.py index 7c500a1a4..88cf7dd54 100644 --- a/dvc/utils/__init__.py +++ b/dvc/utils/__init__.py @@ -1,7 +1,7 @@ """Helpers for other modules.""" from __future__ import unicode_literals -from dvc.utils.compat import str, builtin_str, open +from dvc.utils.compat import str, builtin_str, open, cast_bytes_py2 import os import sys @@ -195,7 +195,8 @@ def fix_env(env=None): lp_key = "LD_LIBRARY_PATH" lp_orig = env.get(lp_key + "_ORIG", None) if lp_orig is not None: - env[lp_key] = lp_orig + # NOTE: py2 doesn't like unicode strings in environ + env[lp_key] = cast_bytes_py2(lp_orig) else: env.pop(lp_key, None) diff --git a/dvc/utils/compat.py b/dvc/utils/compat.py index f59b2fd7d..011928a90 100644 --- a/dvc/utils/compat.py +++ b/dvc/utils/compat.py @@ -12,6 +12,27 @@ is_py2 = _ver[0] == 2 #: Python 3.x? is_py3 = _ver[0] == 3 +# NOTE: cast_bytes_py2 is taken from https://github.com/ipython/ipython_genutils + +# simplified version of ipython_genutils/encoding.py +DEFAULT_ENCODING = sys.getdefaultencoding() + + +def no_code(x, encoding=None): + return x + + +def encode(u, encoding=None): + encoding = encoding or DEFAULT_ENCODING + return u.encode(encoding, "replace") + + +def cast_bytes(s, encoding=None): + if not isinstance(s, bytes): + return encode(s, encoding) + return s + + if is_py2: from urlparse import urlparse, urljoin # noqa: F401 from StringIO import StringIO # noqa: F401 @@ -27,6 +48,7 @@ if is_py2: numeric_types = (int, long, float) # noqa: F821 integer_types = (int, long) # noqa: F821 input = raw_input # noqa: F821 + cast_bytes_py2 = cast_bytes elif is_py3: from urllib.parse import urlparse, urljoin # noqa: F401 @@ -45,3 +67,4 @@ elif is_py3: integer_types = (int,) # noqa: F821 input = input # noqa: F821 open = open # noqa: F821 + cast_bytes_py2 = no_code