Mercurial > hgrepos > Python > apps > py-cutils
changeset 206:73d22943da5a
FIX: CRC-32 validation on Python2.7: normalize to positive numbers
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 22 Jan 2025 17:55:49 +0100 |
| parents | 63088d3675bb |
| children | 0e8c12ff0f41 |
| files | cutils/treesum.py |
| diffstat | 1 files changed, 16 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/cutils/treesum.py Wed Jan 22 17:45:37 2025 +0100 +++ b/cutils/treesum.py Wed Jan 22 17:55:49 2025 +0100 @@ -999,7 +999,22 @@ self._crc32 = zlib.crc32(data, self._crc32) def _hex_crc(self): - return (hex(self._crc32)[2:]).upper() + return (hex(self._get_crc())[2:]).upper() + + def _get_crc(self): + """Get the current CRC always as positive number with the same bit# + pattern because Python2 yields negative numbers also. + + :return: The current CRC value as positive number on all Python + versions + :rtype: int + + """ + if util.PY2: + # Return the bitpattern as unsigned 32-bit number + return (~self._crc32 ^ 0xFFFFFFFF) + else: + return self._crc32 def _get_digest_size(self, algo_name): if self._current_algo_name == algo_name:
