177177from thirdparty .odict import OrderedDict
178178from thirdparty .six .moves import configparser as _configparser
179179from thirdparty .six .moves import http_client as _http_client
180+ from thirdparty .six .moves import input as _input
180181from thirdparty .six .moves import urllib as _urllib
181182from thirdparty .termcolor .termcolor import colored
182183
@@ -942,8 +943,6 @@ def dataToStdout(data, forceOutput=False, bold=False, content_type=None, status=
942943 Writes text to the stdout (console) stream
943944 """
944945
945- message = ""
946-
947946 if not kb .get ("threadException" ):
948947 if forceOutput or not (getCurrentThreadData ().disableStdOut or kb .get ("wizardMode" )):
949948 multiThreadMode = isMultiThreadMode ()
@@ -1082,7 +1081,7 @@ def readInput(message, default=None, checkBatch=True, boolean=False):
10821081 dataToStdout ("%s" % message , forceOutput = not kb .wizardMode , bold = True )
10831082 kb .prependFlag = False
10841083
1085- retVal = raw_input ().strip () or default
1084+ retVal = _input ().strip () or default
10861085 retVal = getUnicode (retVal , encoding = sys .stdin .encoding ) if retVal else retVal
10871086 except :
10881087 try :
@@ -2452,11 +2451,21 @@ def getUnicode(value, encoding=None, noneToNull=False):
24522451 except UnicodeDecodeError :
24532452 return six .text_type (str (value ), errors = "ignore" ) # encoding ignored for non-basestring instances
24542453
2454+ def decodeHex (value ):
2455+ """
2456+ Returns byte representation of provided hexadecimal value
2457+
2458+ >>> decodeHex("313233") == b"123"
2459+ True
2460+ """
2461+
2462+ return bytes .fromhex (value ) if hasattr (bytes , "fromhex" ) else value .decode ("hex" )
2463+
24552464def getBytes (value , encoding = UNICODE_ENCODING , errors = "strict" ):
24562465 """
24572466 Returns byte representation of provided Unicode value
24582467
2459- >>> getBytes(getUnicode("foo\x01 \x83 \xff bar")) == "foo\x01 \x83 \xff bar"
2468+ >>> getBytes(getUnicode("foo\x01 \x83 \xff bar")) == b "foo\x01 \x83 \xff bar"
24602469 True
24612470 """
24622471
@@ -2468,11 +2477,10 @@ def getBytes(value, encoding=UNICODE_ENCODING, errors="strict"):
24682477 value = value .replace (unichr (char ), "%s%02x" % (SAFE_HEX_MARKER , char - 0xF0000 ))
24692478
24702479 retVal = value .encode (encoding , errors )
2471-
2472- retVal = re .sub (r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER , lambda _ : _ .group (1 ).decode ("hex" ), retVal )
2480+ retVal = re .sub (r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER , lambda _ : decodeHex (_ .group (1 )), retVal )
24732481 else :
24742482 retVal = value .encode (encoding , errors )
2475- retVal = re .sub (r "\\x([0-9a-f]{2})" , lambda _ : _ .group (1 ). decode ( "hex" ), retVal )
2483+ retVal = re .sub (b "\\ \\ x([0-9a-f]{2})" , lambda _ : decodeHex ( _ .group (1 )), retVal )
24762484
24772485 return retVal
24782486
@@ -2876,6 +2884,9 @@ def extractRegexResult(regex, content, flags=0):
28762884 retVal = None
28772885
28782886 if regex and content and "?P<result>" in regex :
2887+ if isinstance (content , six .binary_type ) and isinstance (regex , six .text_type ):
2888+ regex = getBytes (regex )
2889+
28792890 match = re .search (regex , content , flags )
28802891
28812892 if match :
@@ -3812,11 +3823,11 @@ def normalizeUnicode(value):
38123823
38133824 # Reference: http://www.peterbe.com/plog/unicode-to-ascii
38143825
3815- >>> normalizeUnicode(u'\u0161 u\u0107 uraj') == b 'sucuraj'
3826+ >>> normalizeUnicode(u'\u0161 u\u0107 uraj') == u 'sucuraj'
38163827 True
38173828 """
38183829
3819- return unicodedata .normalize ("NFKD" , value ).encode ("ascii" , "ignore" ) if isinstance (value , six .text_type ) else value
3830+ return getUnicode ( unicodedata .normalize ("NFKD" , value ).encode ("ascii" , "ignore" ) ) if isinstance (value , six .text_type ) else value
38203831
38213832def safeSQLIdentificatorNaming (name , isTable = False ):
38223833 """
@@ -4656,7 +4667,7 @@ def getRequestHeader(request, name):
46564667
46574668 if request and request .headers and name :
46584669 _ = name .upper ()
4659- retVal = max (value if _ == key .upper () else None for key , value in request .header_items ())
4670+ retVal = max (value if _ == key .upper () else "" for key , value in request .header_items ()) or None
46604671
46614672 return retVal
46624673
0 commit comments