fix argv errors for 'sh' files
@@ -6,7 +6,7 @@ import logging
import threading
import functools
-from six import StringIO, text_type, PY3
+from six import StringIO, text_type, binary_type, PY3
try:
file
except NameError:
@@ -487,10 +487,7 @@ class ShRuntime(object):
# First argument is the script name
argv = [os.path.basename(filename)] + (args or [])
- # convert sys.argv to unicode if on python3
- if PY3:
- # todo: this is only a temporary solution and needs to be redone. This may be buggy.
- argv = [c if isinstance(c, text_type) else c.decode("utf-8") for c in argv]
+ argv = self.encode_argv(argv)
sys.argv = argv
# Set current os environ to the threading environ
@@ -549,6 +546,7 @@ class ShRuntime(object):
if args is None:
args = []
+ args = self.encode_argv(args)
for i, arg in enumerate([filename] + args):
current_state.temporary_environ[str(i)] = arg
@@ -557,7 +555,7 @@ class ShRuntime(object):
# Enclosing variables will be merged to environ when creating new thread
try:
- with open(filename) as fins:
+ with open(filename, "rU") as fins:
child_worker = self.run(fins.readlines(),
final_ins=ins,
final_outs=outs,
@@ -577,6 +575,19 @@ class ShRuntime(object):
self.stash.write_message('%s: error while executing shell script\n' % filename)
current_state.return_value = 2
+ def encode_argv(self, argv):
+ """
+ Convert an argv list into the appropiate string type depending
+ on the currently used python version.
+ """
+ if PY3:
+ # we need unicode argv
+ argv = [c if isinstance(c, text_type) else c.decode("utf-8") for c in argv]
+ else:
+ # we need bytestring argv
+ argv = [c if isinstance(c, binary_type) else c.encode("utf-8") for c in argv]
+ return argv
+
def get_prompt(self):
"""
Get the prompt string. Fill with current working directory if required
