Returns textual representation of a given value safe for writing to stdout >>> stdoutEncode(b"foobar") 'foobar'
(value)
| 404 | return retVal |
| 405 | |
| 406 | def stdoutEncode(value): |
| 407 | """ |
| 408 | Returns textual representation of a given value safe for writing to stdout |
| 409 | >>> stdoutEncode(b"foobar") |
| 410 | 'foobar' |
| 411 | """ |
| 412 | |
| 413 | if value is None: |
| 414 | value = "" |
| 415 | |
| 416 | if IS_WIN and IS_TTY and kb.get("codePage", -1) is None: |
| 417 | output = shellExec("chcp") |
| 418 | match = re.search(r": (\d{3,})", output or "") |
| 419 | |
| 420 | if match: |
| 421 | try: |
| 422 | candidate = "cp%s" % match.group(1) |
| 423 | codecs.lookup(candidate) |
| 424 | kb.codePage = candidate |
| 425 | except (LookupError, TypeError): |
| 426 | pass |
| 427 | |
| 428 | kb.codePage = kb.codePage or "" |
| 429 | |
| 430 | encoding = kb.get("codePage") or getattr(sys.stdout, "encoding", None) or UNICODE_ENCODING |
| 431 | |
| 432 | if six.PY3: |
| 433 | if isinstance(value, (bytes, bytearray)): |
| 434 | value = getUnicode(value, encoding) |
| 435 | elif not isinstance(value, str): |
| 436 | value = str(value) |
| 437 | |
| 438 | try: |
| 439 | retVal = value.encode(encoding, errors="replace").decode(encoding, errors="replace") |
| 440 | except (LookupError, TypeError): |
| 441 | retVal = value.encode("ascii", errors="replace").decode("ascii", errors="replace") |
| 442 | else: |
| 443 | if isinstance(value, six.text_type): |
| 444 | try: |
| 445 | retVal = value.encode(encoding, errors="replace") |
| 446 | except (LookupError, TypeError): |
| 447 | retVal = value.encode("ascii", errors="replace") |
| 448 | else: |
| 449 | retVal = value |
| 450 | |
| 451 | return retVal |
| 452 | |
| 453 | def getConsoleLength(value): |
| 454 | """ |
no test coverage detected
searching dependent graphs…