py2: fix environ issue

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2019-02-09 03:59:45 +02:00
parent 9210e66b0c
commit 40c84fe4ed
3 changed files with 30 additions and 3 deletions
+4 -1
View File
@@ -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))
+3 -2
View File
@@ -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)
+23
View File
@@ -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