(p, size, mapping="latin-1", errors="strict")
| 1686 | |
| 1687 | |
| 1688 | def PyUnicode_EncodeCharmap(p, size, mapping="latin-1", errors="strict"): |
| 1689 | ## /* the following variable is used for caching string comparisons |
| 1690 | ## * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 1691 | ## * 3=ignore, 4=xmlcharrefreplace */ |
| 1692 | |
| 1693 | # /* Default to Latin-1 */ |
| 1694 | if mapping == "latin-1": |
| 1695 | return PyUnicode_EncodeLatin1(p, size, errors) |
| 1696 | if size == 0: |
| 1697 | return b"" |
| 1698 | inpos = 0 |
| 1699 | res = [] |
| 1700 | while inpos < size: |
| 1701 | # /* try to encode it */ |
| 1702 | try: |
| 1703 | x = charmapencode_output(ord(p[inpos]), mapping) |
| 1704 | res += x |
| 1705 | except KeyError: |
| 1706 | x = unicode_call_errorhandler( |
| 1707 | errors, |
| 1708 | "charmap", |
| 1709 | "character maps to <undefined>", |
| 1710 | p, |
| 1711 | inpos, |
| 1712 | inpos + 1, |
| 1713 | False, |
| 1714 | ) |
| 1715 | replacement = x[0] |
| 1716 | if isinstance(replacement, bytes): |
| 1717 | res += list(replacement) |
| 1718 | else: |
| 1719 | try: |
| 1720 | for y in replacement: |
| 1721 | res += charmapencode_output(ord(y), mapping) |
| 1722 | except KeyError: |
| 1723 | raise UnicodeEncodeError( |
| 1724 | "charmap", p, inpos, inpos + 1, "character maps to <undefined>" |
| 1725 | ) |
| 1726 | inpos += 1 |
| 1727 | return res |
| 1728 | |
| 1729 | |
| 1730 | def PyUnicode_DecodeCharmap(s, size, mapping, errors): |
no test coverage detected