|
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__ = "foxfile.ini" |
347 | 347 | __use_json_file__ = False |
348 | 348 | __use_json_name__ = "foxfile.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('PYARCHIVEFILE_CONFIG_FILE' in os.environ and os.path.exists(os.environ['PYARCHIVEFILE_CONFIG_FILE']) and __use_env_file__): |
352 | 352 | scriptconf = os.environ['PYARCHIVEFILE_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 './' |
@@ -1751,51 +1711,56 @@ def MkTempFile(data=None, inmem=__use_inmemfile__, isbytes=True, prefix=__projec |
1751 | 1711 | return f |
1752 | 1712 |
|
1753 | 1713 |
|
1754 | | -def _normalize_initial_data(data, isbytes, encoding): |
1755 | | - """ |
1756 | | - Coerce `data` to the correct type for the chosen mode: |
1757 | | - - bytes mode: return `bytes` (Py2: str; Py3: bytes) |
1758 | | - - text mode : return unicode/str (Py2: unicode; Py3: str) |
1759 | | - """ |
| 1714 | +def _as_bytes_like(data): |
| 1715 | + if isinstance(data, bytes): |
| 1716 | + return data |
| 1717 | + if isinstance(data, bytearray): |
| 1718 | + return bytes(data) |
| 1719 | + try: |
| 1720 | + mv = memoryview |
| 1721 | + except NameError: |
| 1722 | + mv = () |
| 1723 | + if mv and isinstance(data, mv): |
| 1724 | + return bytes(data) |
| 1725 | + return None |
| 1726 | + |
| 1727 | +def _normalize_initial_data(data, isbytes, encoding, *, errors="strict"): |
| 1728 | + """Return bytes (if isbytes) or text (unicode on Py2, str on Py3).""" |
1760 | 1729 | if data is None: |
1761 | 1730 | return None |
1762 | 1731 |
|
1763 | 1732 | if isbytes: |
1764 | | - # Need a byte sequence |
1765 | | - if isinstance(data, bytes): |
1766 | | - return data |
1767 | | - if isinstance(data, bytearray): |
1768 | | - return bytes(data) |
1769 | | - # memoryview may not exist on very old Py2 builds; guard dynamically |
1770 | | - mv_t = getattr(__builtins__, 'memoryview', type(None)) |
1771 | | - if isinstance(data, mv_t): |
1772 | | - return bytes(data) |
1773 | | - if isinstance(data, str): |
1774 | | - # Py2 str is already bytes; Py3 str must be encoded |
1775 | | - return data if PY2 else data.encode(encoding) |
1776 | | - if PY2 and isinstance(data, unicode): # noqa: F821 (unicode only in Py2) |
1777 | | - return data.encode(encoding) |
| 1733 | + b = _as_bytes_like(data) |
| 1734 | + if b is not None: |
| 1735 | + return b |
| 1736 | + if PY2: |
| 1737 | + if isinstance(data, unicode_type): |
| 1738 | + return data.encode(encoding, errors) |
| 1739 | + if isinstance(data, str): # Py2: str is already bytes-like |
| 1740 | + return data |
| 1741 | + else: |
| 1742 | + if isinstance(data, str): |
| 1743 | + return data.encode(encoding, errors) |
1778 | 1744 | raise TypeError("data must be bytes-like or text for isbytes=True (got %r)" % (type(data),)) |
1779 | 1745 | else: |
1780 | | - # Need text (unicode in Py2, str in Py3) |
1781 | 1746 | if PY2: |
1782 | | - if isinstance(data, unicode): # noqa: F821 |
| 1747 | + if isinstance(data, unicode_type): |
1783 | 1748 | return data |
| 1749 | + b = _as_bytes_like(data) |
| 1750 | + if b is not None: |
| 1751 | + return b.decode(encoding, errors) |
1784 | 1752 | if isinstance(data, str): |
1785 | | - return data.decode(encoding) |
1786 | | - if isinstance(data, bytearray): |
1787 | | - return bytes(data).decode(encoding) |
1788 | | - mv_t = getattr(__builtins__, 'memoryview', type(None)) |
1789 | | - if isinstance(data, mv_t): |
1790 | | - return bytes(data).decode(encoding) |
| 1753 | + return data.decode(encoding, errors) |
1791 | 1754 | raise TypeError("data must be unicode or bytes-like for text mode (got %r)" % (type(data),)) |
1792 | 1755 | else: |
1793 | 1756 | if isinstance(data, str): |
1794 | 1757 | return data |
1795 | | - if isinstance(data, (bytes, bytearray, memoryview)): |
1796 | | - return bytes(data).decode(encoding) |
| 1758 | + b = _as_bytes_like(data) |
| 1759 | + if b is not None: |
| 1760 | + return b.decode(encoding, errors) |
1797 | 1761 | raise TypeError("data must be str or bytes-like for text mode (got %r)" % (type(data),)) |
1798 | 1762 |
|
| 1763 | + |
1799 | 1764 | def MkTempFile(data=None, |
1800 | 1765 | inmem=True, |
1801 | 1766 | isbytes=True, |
|
0 commit comments