Returns byte representation of provided Unicode value >>> getBytes(u"foo\\\\x01\\\\x83\\\\xffbar") == b"foo\\x01\\x83\\xffbar" True
(value, encoding=None, errors="strict", unsafe=True)
| 278 | return retVal |
| 279 | |
| 280 | def getBytes(value, encoding=None, errors="strict", unsafe=True): |
| 281 | """ |
| 282 | Returns byte representation of provided Unicode value |
| 283 | |
| 284 | >>> getBytes(u"foo\\\\x01\\\\x83\\\\xffbar") == b"foo\\x01\\x83\\xffbar" |
| 285 | True |
| 286 | """ |
| 287 | |
| 288 | retVal = value |
| 289 | |
| 290 | if encoding is None: |
| 291 | encoding = conf.get("encoding") or UNICODE_ENCODING |
| 292 | |
| 293 | try: |
| 294 | codecs.lookup(encoding) |
| 295 | except (LookupError, TypeError): |
| 296 | encoding = UNICODE_ENCODING |
| 297 | |
| 298 | if isinstance(value, six.text_type): |
| 299 | if INVALID_UNICODE_PRIVATE_AREA: |
| 300 | if unsafe: |
| 301 | for char in xrange(0xF0000, 0xF00FF + 1): |
| 302 | value = value.replace(_unichr(char), "%s%02x" % (SAFE_HEX_MARKER, char - 0xF0000)) |
| 303 | |
| 304 | retVal = value.encode(encoding, errors) |
| 305 | |
| 306 | if unsafe: |
| 307 | retVal = re.sub((r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER).encode(), lambda _: decodeHex(_.group(1)), retVal) |
| 308 | else: |
| 309 | try: |
| 310 | retVal = value.encode(encoding, errors) |
| 311 | except UnicodeError: |
| 312 | retVal = value.encode(UNICODE_ENCODING, errors="replace") |
| 313 | |
| 314 | if unsafe: |
| 315 | retVal = re.sub(b"\\\\x([0-9a-f]{2})", lambda _: decodeHex(_.group(1)), retVal) |
| 316 | |
| 317 | return retVal |
| 318 | |
| 319 | def getOrds(value): |
| 320 | """ |
no test coverage detected
searching dependent graphs…