Returns the unicode representation of the supplied value >>> getUnicode('test') == u'test' True >>> getUnicode(1) == u'1' True >>> getUnicode(None) == 'None' True >>> getUnicode(b'/etc/passwd') == '/etc/passwd' True
(value, encoding=None, noneToNull=False)
| 329 | return [_ if isinstance(_, int) else ord(_) for _ in value] |
| 330 | |
| 331 | def getUnicode(value, encoding=None, noneToNull=False): |
| 332 | """ |
| 333 | Returns the unicode representation of the supplied value |
| 334 | |
| 335 | >>> getUnicode('test') == u'test' |
| 336 | True |
| 337 | >>> getUnicode(1) == u'1' |
| 338 | True |
| 339 | >>> getUnicode(None) == 'None' |
| 340 | True |
| 341 | >>> getUnicode(b'/etc/passwd') == '/etc/passwd' |
| 342 | True |
| 343 | """ |
| 344 | |
| 345 | # Best position for --time-limit mechanism |
| 346 | if conf.get("timeLimit") and kb.get("startTime") and (time.time() - kb.startTime > conf.timeLimit): |
| 347 | raise SystemExit |
| 348 | |
| 349 | if noneToNull and value is None: |
| 350 | return NULL |
| 351 | |
| 352 | if isinstance(value, six.text_type): |
| 353 | return value |
| 354 | elif isinstance(value, six.binary_type): |
| 355 | # Heuristics (if encoding not explicitly specified) |
| 356 | candidates = filterNone((encoding, kb.get("pageEncoding") if kb.get("originalPage") else None, conf.get("encoding"), UNICODE_ENCODING, sys.getfilesystemencoding())) |
| 357 | if all(_ in value for _ in (b'<', b'>')): |
| 358 | pass |
| 359 | elif b'\n' not in value and re.search(r"(?i)\w+\.\w{2,3}\Z|\A(\w:\\|/\w+)", six.text_type(value, UNICODE_ENCODING, errors="ignore")): |
| 360 | candidates = filterNone((encoding, sys.getfilesystemencoding(), kb.get("pageEncoding") if kb.get("originalPage") else None, UNICODE_ENCODING, conf.get("encoding"))) |
| 361 | elif conf.get("encoding") and b'\n' not in value: |
| 362 | candidates = filterNone((encoding, conf.get("encoding"), kb.get("pageEncoding") if kb.get("originalPage") else None, sys.getfilesystemencoding(), UNICODE_ENCODING)) |
| 363 | |
| 364 | for candidate in candidates: |
| 365 | try: |
| 366 | return six.text_type(value, candidate) |
| 367 | except (UnicodeDecodeError, LookupError): |
| 368 | pass |
| 369 | |
| 370 | try: |
| 371 | return six.text_type(value, encoding or (kb.get("pageEncoding") if kb.get("originalPage") else None) or UNICODE_ENCODING) |
| 372 | except UnicodeDecodeError: |
| 373 | return six.text_type(value, UNICODE_ENCODING, errors="reversible") |
| 374 | elif isListLike(value): |
| 375 | value = list(getUnicode(_, encoding, noneToNull) for _ in value) |
| 376 | return value |
| 377 | else: |
| 378 | try: |
| 379 | return six.text_type(value) |
| 380 | except UnicodeDecodeError: |
| 381 | return six.text_type(str(value), errors="ignore") # encoding ignored for non-basestring instances |
| 382 | |
| 383 | def getText(value, encoding=None): |
| 384 | """ |
no test coverage detected
searching dependent graphs…