|
26 | 26 | import stat |
27 | 27 | import zlib |
28 | 28 | import mmap |
| 29 | +import hmac |
29 | 30 | import base64 |
30 | 31 | import shutil |
31 | 32 | import socket |
32 | 33 | import struct |
33 | 34 | import hashlib |
34 | 35 | import inspect |
35 | 36 | import datetime |
36 | | -import tempfile |
37 | 37 | import logging |
38 | 38 | import zipfile |
39 | 39 | import binascii |
@@ -346,8 +346,8 @@ def _is_printable(ch): |
346 | 346 | __use_ini_name__ = "catfile.ini" |
347 | 347 | __use_json_file__ = False |
348 | 348 | __use_json_name__ = "catfile.json" |
349 | | -if(__use_ini_file__ and __use_json_name__): |
350 | | - __use_json_name__ = False |
| 349 | +if(__use_ini_file__ and __use_json_file__): |
| 350 | + __use_json_file__ = False |
351 | 351 | if('PYCATFILE_CONFIG_FILE' in os.environ and os.path.exists(os.environ['PYCATFILE_CONFIG_FILE']) and __use_env_file__): |
352 | 352 | scriptconf = os.environ['PYCATFILE_CONFIG_FILE'] |
353 | 353 | else: |
@@ -622,8 +622,8 @@ def _get(section_dict, key, default=None): |
622 | 622 |
|
623 | 623 | # Deterministic category order (handy for consistent output/printing). |
624 | 624 | CATEGORY_ORDER = [ |
625 | | - "files", "hardlinks", "symlinks", "character", "block", |
626 | | - "directories", "fifo", "sockets", "doors", "ports", |
| 625 | + "files", "hardlinks", "symlinks", "characters", "blocks", |
| 626 | + "directories", "fifos", "sockets", "doors", "ports", |
627 | 627 | "whiteouts", "sparsefiles", "junctions", "links", "devices" |
628 | 628 | ] |
629 | 629 |
|
@@ -808,46 +808,6 @@ def VerbosePrintOutReturn(dbgtxt, outtype="log", dbgenable=True, dgblevel=20): |
808 | 808 | return dbgtxt |
809 | 809 |
|
810 | 810 |
|
811 | | -# --- Helpers --- |
812 | | -def _normalize_initial_data(data, isbytes, encoding): |
813 | | - """Return data in the correct type for write(): bytes (if isbytes) or text (if not).""" |
814 | | - if data is None: |
815 | | - return None |
816 | | - |
817 | | - if isbytes: |
818 | | - # Want bytes |
819 | | - if isinstance(data, bytes): |
820 | | - return data |
821 | | - # Py2: str is already bytes, unicode needs encode |
822 | | - if sys.version_info[0] == 2: |
823 | | - try: |
824 | | - unicode # noqa: F821 |
825 | | - except NameError: |
826 | | - pass |
827 | | - else: |
828 | | - if isinstance(data, unicode): # noqa: F821 |
829 | | - return data.encode(encoding) |
830 | | - # Py3 str -> encode |
831 | | - return str(data).encode(encoding) |
832 | | - else: |
833 | | - # Want text (unicode/str) |
834 | | - if sys.version_info[0] == 2: |
835 | | - try: |
836 | | - unicode # noqa: F821 |
837 | | - if isinstance(data, unicode): # noqa: F821 |
838 | | - return data |
839 | | - # bytes/str -> decode |
840 | | - return data.decode(encoding) if isinstance(data, str) else unicode(data) # noqa: F821 |
841 | | - except NameError: |
842 | | - # Very defensive; shouldn't happen |
843 | | - return data |
844 | | - else: |
845 | | - # Py3: want str |
846 | | - if isinstance(data, bytes): |
847 | | - return data.decode(encoding) |
848 | | - return str(data) |
849 | | - |
850 | | - |
851 | 811 | def _split_posix(path_text): |
852 | 812 | """Split POSIX paths regardless of OS; return list of components.""" |
853 | 813 | # Normalize leading './' |
@@ -1712,51 +1672,56 @@ def DetectTarBombCatFileArray(listarrayfiles, |
1712 | 1672 | } |
1713 | 1673 |
|
1714 | 1674 |
|
1715 | | -def _normalize_initial_data(data, isbytes, encoding): |
1716 | | - """ |
1717 | | - Coerce `data` to the correct type for the chosen mode: |
1718 | | - - bytes mode: return `bytes` (Py2: str; Py3: bytes) |
1719 | | - - text mode : return unicode/str (Py2: unicode; Py3: str) |
1720 | | - """ |
| 1675 | +def _as_bytes_like(data): |
| 1676 | + if isinstance(data, bytes): |
| 1677 | + return data |
| 1678 | + if isinstance(data, bytearray): |
| 1679 | + return bytes(data) |
| 1680 | + try: |
| 1681 | + mv = memoryview |
| 1682 | + except NameError: |
| 1683 | + mv = () |
| 1684 | + if mv and isinstance(data, mv): |
| 1685 | + return bytes(data) |
| 1686 | + return None |
| 1687 | + |
| 1688 | +def _normalize_initial_data(data, isbytes, encoding, *, errors="strict"): |
| 1689 | + """Return bytes (if isbytes) or text (unicode on Py2, str on Py3).""" |
1721 | 1690 | if data is None: |
1722 | 1691 | return None |
1723 | 1692 |
|
1724 | 1693 | if isbytes: |
1725 | | - # Need a byte sequence |
1726 | | - if isinstance(data, bytes): |
1727 | | - return data |
1728 | | - if isinstance(data, bytearray): |
1729 | | - return bytes(data) |
1730 | | - # memoryview may not exist on very old Py2 builds; guard dynamically |
1731 | | - mv_t = getattr(__builtins__, 'memoryview', type(None)) |
1732 | | - if isinstance(data, mv_t): |
1733 | | - return bytes(data) |
1734 | | - if isinstance(data, str): |
1735 | | - # Py2 str is already bytes; Py3 str must be encoded |
1736 | | - return data if PY2 else data.encode(encoding) |
1737 | | - if PY2 and isinstance(data, unicode): # noqa: F821 (unicode only in Py2) |
1738 | | - return data.encode(encoding) |
| 1694 | + b = _as_bytes_like(data) |
| 1695 | + if b is not None: |
| 1696 | + return b |
| 1697 | + if PY2: |
| 1698 | + if isinstance(data, unicode_type): |
| 1699 | + return data.encode(encoding, errors) |
| 1700 | + if isinstance(data, str): # Py2: str is already bytes-like |
| 1701 | + return data |
| 1702 | + else: |
| 1703 | + if isinstance(data, str): |
| 1704 | + return data.encode(encoding, errors) |
1739 | 1705 | raise TypeError("data must be bytes-like or text for isbytes=True (got %r)" % (type(data),)) |
1740 | 1706 | else: |
1741 | | - # Need text (unicode in Py2, str in Py3) |
1742 | 1707 | if PY2: |
1743 | | - if isinstance(data, unicode): # noqa: F821 |
| 1708 | + if isinstance(data, unicode_type): |
1744 | 1709 | return data |
| 1710 | + b = _as_bytes_like(data) |
| 1711 | + if b is not None: |
| 1712 | + return b.decode(encoding, errors) |
1745 | 1713 | if isinstance(data, str): |
1746 | | - return data.decode(encoding) |
1747 | | - if isinstance(data, bytearray): |
1748 | | - return bytes(data).decode(encoding) |
1749 | | - mv_t = getattr(__builtins__, 'memoryview', type(None)) |
1750 | | - if isinstance(data, mv_t): |
1751 | | - return bytes(data).decode(encoding) |
| 1714 | + return data.decode(encoding, errors) |
1752 | 1715 | raise TypeError("data must be unicode or bytes-like for text mode (got %r)" % (type(data),)) |
1753 | 1716 | else: |
1754 | 1717 | if isinstance(data, str): |
1755 | 1718 | return data |
1756 | | - if isinstance(data, (bytes, bytearray, memoryview)): |
1757 | | - return bytes(data).decode(encoding) |
| 1719 | + b = _as_bytes_like(data) |
| 1720 | + if b is not None: |
| 1721 | + return b.decode(encoding, errors) |
1758 | 1722 | raise TypeError("data must be str or bytes-like for text mode (got %r)" % (type(data),)) |
1759 | 1723 |
|
| 1724 | + |
1760 | 1725 | def MkTempFile(data=None, |
1761 | 1726 | inmem=True, |
1762 | 1727 | isbytes=True, |
|
0 commit comments