@@ -105,52 +105,6 @@ def isListLike(value): # Cross-referenced function
105105def shellExec (cmd ): # Cross-referenced function
106106 raise NotImplementedError
107107
108- def stdoutEncode (value ):
109- value = value or ""
110-
111- if IS_WIN and IS_TTY and kb .get ("codePage" , - 1 ) is None :
112- output = shellExec ("chcp" )
113- match = re .search (r": (\d{3,})" , output or "" )
114-
115- if match :
116- try :
117- candidate = "cp%s" % match .group (1 )
118- codecs .lookup (candidate )
119- except LookupError :
120- pass
121- else :
122- kb .codePage = candidate
123-
124- kb .codePage = kb .codePage or ""
125-
126- if isinstance (value , six .text_type ) and PYVERSION < "3.6" :
127- encoding = kb .get ("codePage" ) or sys .stdout .encoding or UNICODE_ENCODING
128-
129- while True :
130- try :
131- retVal = value .encode (encoding )
132- break
133- except UnicodeEncodeError as ex :
134- value = value [:ex .start ] + "?" + value [ex .end :]
135-
136- if IS_WIN and PYVERSION < "3.6" :
137- warnMsg = "cannot properly display (some) Unicode characters "
138- warnMsg += "inside Windows OS command prompt "
139- warnMsg += "(https://bugs.python.org/issue1602). All "
140- warnMsg += "unhandled occurrences will result in "
141- warnMsg += "replacement with '?' character. Please, find "
142- warnMsg += "proper character representation inside "
143- warnMsg += "corresponding output files. "
144- singleTimeWarnMessage (warnMsg )
145-
146- if six .PY3 :
147- retVal = getUnicode (retVal , encoding )
148-
149- else :
150- retVal = value
151-
152- return retVal
153-
154108def jsonize (data ):
155109 """
156110 Returns JSON serialized data
@@ -365,3 +319,51 @@ def getText(value):
365319 pass
366320
367321 return retVal
322+
323+ def stdoutEncode (value ):
324+ """
325+ Returns binary representation of a given Unicode value safe for writing to stdout
326+ """
327+
328+ value = value or ""
329+
330+ if IS_WIN and IS_TTY and kb .get ("codePage" , - 1 ) is None :
331+ output = shellExec ("chcp" )
332+ match = re .search (r": (\d{3,})" , output or "" )
333+
334+ if match :
335+ try :
336+ candidate = "cp%s" % match .group (1 )
337+ codecs .lookup (candidate )
338+ except LookupError :
339+ pass
340+ else :
341+ kb .codePage = candidate
342+
343+ kb .codePage = kb .codePage or ""
344+
345+ if isinstance (value , six .text_type ):
346+ encoding = kb .get ("codePage" ) or sys .stdout .encoding or UNICODE_ENCODING
347+
348+ while True :
349+ try :
350+ retVal = value .encode (encoding )
351+ break
352+ except UnicodeEncodeError as ex :
353+ value = value [:ex .start ] + "?" * (ex .end - ex .start ) + value [ex .end :]
354+
355+ warnMsg = "cannot properly display (some) Unicode characters "
356+ warnMsg += "inside your terminal ('%s') environment. All " % encoding
357+ warnMsg += "unhandled occurrences will result in "
358+ warnMsg += "replacement with '?' character. Please, find "
359+ warnMsg += "proper character representation inside "
360+ warnMsg += "corresponding output files"
361+ singleTimeWarnMessage (warnMsg )
362+
363+ if six .PY3 :
364+ retVal = getUnicode (retVal , encoding )
365+
366+ else :
367+ retVal = value
368+
369+ return retVal
0 commit comments