def shellsafe(s, quote='', doescape=True):
    """Returns the value string, wrapped in the specified quotes (if not empty), but checks and raises an Exception if the string is at risk of causing code injection"""
    if sys.version[0] == '2' and not isinstance(s,unicode): #pylint: disable=undefined-variable
        s = unicode(s,'utf-8') #pylint: disable=undefined-variable
    if len(s) > 1024:
        raise ValueError("Variable value rejected for security reasons: too long")
    if quote:
        if quote in s:
            if doescape:
                s = escape(s,quote)
            else:
                raise ValueError("Variable value rejected for security reasons: " + s)
        return quote + s + quote
    else:
        for c in s:
            if c in DISALLOWINSHELLSAFE:
                raise ValueError("Variable value rejected for security reasons: " + s)
        return s
def shellsafe ( s , quote = '' , doescape = True ) : if sys . version [ 0 ] == '2' and not isinstance ( s , unicode ) : s = unicode ( s , 'utf-8' ) if len ( s ) > 1024 : raise ValueError ( "Variable value rejected for security reasons: too long" ) if quote : if quote in s : if doescape : s = escape ( s , quote ) else : raise ValueError ( "Variable value rejected for security reasons: " + s ) return quote + s + quote else : for c in s : if c in DISALLOWINSHELLSAFE : raise ValueError ( "Variable value rejected for security reasons: " + s ) return s
Returns the value string, wrapped in the specified quotes (if not empty), but checks and raises an Exception if the string is at risk of causing code injection