|
23 | 23 | from lib.core.settings import IS_WIN |
24 | 24 | from lib.core.settings import NULL |
25 | 25 | from lib.core.settings import PICKLE_PROTOCOL |
| 26 | +from lib.core.settings import PYVERSION |
26 | 27 | from lib.core.settings import SAFE_HEX_MARKER |
27 | 28 | from lib.core.settings import UNICODE_ENCODING |
28 | 29 | from thirdparty import six |
@@ -100,27 +101,34 @@ def filterNone(values): # Cross-referenced function |
100 | 101 | def isListLike(value): # Cross-referenced function |
101 | 102 | raise NotImplementedError |
102 | 103 |
|
103 | | -def stdoutencode(data): |
104 | | - retVal = data |
| 104 | +def stdoutEncode(value): |
| 105 | + value = value or "" |
105 | 106 |
|
106 | | - if six.PY2: |
107 | | - try: |
108 | | - retVal = getBytes(data or "", sys.stdout.encoding, unsafe=False) |
| 107 | + if isinstance(value, six.text_type) and PYVERSION < "3.6": |
| 108 | + encoding = sys.stdout.encoding or UNICODE_ENCODING |
| 109 | + |
| 110 | + while True: |
| 111 | + try: |
| 112 | + retVal = value.encode(encoding) |
| 113 | + break |
| 114 | + except UnicodeEncodeError as ex: |
| 115 | + value = value[:ex.start] + "?" + value[ex.end:] |
109 | 116 |
|
110 | | - # Reference: http://bugs.python.org/issue1602 |
111 | | - if IS_WIN: |
112 | | - if '?' in retVal and '?' not in retVal: |
113 | | - warnMsg = "cannot properly display Unicode characters " |
| 117 | + if IS_WIN and PYVERSION < "3.6": |
| 118 | + warnMsg = "cannot properly display (some) Unicode characters " |
114 | 119 | warnMsg += "inside Windows OS command prompt " |
115 | | - warnMsg += "(http://bugs.python.org/issue1602). All " |
| 120 | + warnMsg += "(https://bugs.python.org/issue1602). All " |
116 | 121 | warnMsg += "unhandled occurrences will result in " |
117 | 122 | warnMsg += "replacement with '?' character. Please, find " |
118 | 123 | warnMsg += "proper character representation inside " |
119 | 124 | warnMsg += "corresponding output files. " |
120 | 125 | singleTimeWarnMessage(warnMsg) |
121 | 126 |
|
122 | | - except: |
123 | | - retVal = getBytes(data or "", unsafe=False) |
| 127 | + if six.PY3: |
| 128 | + retVal = getUnicode(retVal, encoding) |
| 129 | + |
| 130 | + else: |
| 131 | + retVal = value |
124 | 132 |
|
125 | 133 | return retVal |
126 | 134 |
|
|
0 commit comments