Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve non-string iterable handling #715

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/rez/resolved_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rez.resolver import Resolver, ResolverStatus
from rez.system import system
from rez.config import config
from rez.util import shlex_join, dedup
from rez.util import shlex_join, dedup, is_non_string_iterable
from rez.utils.sourcecode import SourceCodeError
from rez.utils.colorize import critical, heading, local, implicit, Printer
from rez.utils.formatting import columnise, PackageRequest, ENV_VAR_REGEX
Expand Down Expand Up @@ -1205,7 +1205,7 @@ def execute_shell(self, shell=None, parent_environ=None, rcfile=None,
"""
sh = create_shell(shell)

if hasattr(command, "__iter__"):
if is_non_string_iterable(command):
command = sh.join(command)

# start a new session if specified
Expand Down
4 changes: 2 additions & 2 deletions src/rez/rex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rez.system import system
from rez.config import config
from rez.exceptions import RexError, RexUndefinedVariableError, RezSystemError
from rez.util import shlex_join
from rez.util import shlex_join, is_non_string_iterable
from rez.utils import reraise
from rez.utils.system import popen
from rez.utils.sourcecode import SourceCode, SourceCodeError
Expand Down Expand Up @@ -624,7 +624,7 @@ def command(self, value):
if self.passive:
return

if hasattr(value, '__iter__'):
if is_non_string_iterable(value):
it = iter(value)
cmd = EscapedString.disallow(it.next())
value = [cmd] + [self.escape_string(x) for x in it]
Expand Down
4 changes: 2 additions & 2 deletions src/rez/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Pluggable API for creating subshells using different programs, such as bash.
"""
from rez.rex import RexExecutor, ActionInterpreter, OutputStyle
from rez.util import shlex_join
from rez.util import shlex_join, is_non_string_iterable
from rez.backport.shutilwhich import which
from rez.utils.logging_ import print_warning
from rez.utils.system import popen
Expand Down Expand Up @@ -379,7 +379,7 @@ def error(self, value):

# escaping is allowed in args, but not in program string
def command(self, value):
if hasattr(value, '__iter__'):
if is_non_string_iterable(value):
it = iter(value)
cmd = EscapedString.disallow(it.next())
args_str = ' '.join(self.escape_string(x) for x in it)
Expand Down
12 changes: 11 additions & 1 deletion src/rez/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"""
import stat
import sys
import collections
import atexit
import os
import os.path
import copy
from rez.exceptions import RezError
from rez.utils.yaml import dump_yaml
from rez.vendor.progress.bar import Bar
from rez.vendor.six import six


DEV_NULL = open(os.devnull, 'w')
Expand Down Expand Up @@ -95,7 +97,7 @@ def shlex_join(value):
def quote(s):
return pipes.quote(s) if '$' not in s else s

if hasattr(value, '__iter__'):
if is_non_string_iterable(value):
return ' '.join(quote(x) for x in value)
else:
return str(value)
Expand Down Expand Up @@ -175,6 +177,14 @@ def _atexit():
pass


def is_non_string_iterable(arg):
"""Python 2 and 3 compatible non-string iterable identifier"""

return (
isinstance(arg, collections.Iterable)
and not isinstance(arg, six.string_types)
)

# Copyright 2013-2016 Allan Johns.
#
# This library is free software: you can redistribute it and/or
Expand Down