changeset 379:6d7659a709f2

FIX: treesum: str.translate() behaves differently in Python3: make it PY3-compatible
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 12 May 2025 12:12:44 +0200
parents 32b937a73068
children 58552d3d1766
files cutils/treesum.py cutils/util/__init__.py
diffstat 2 files changed, 20 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Mon May 12 09:48:23 2025 +0200
+++ b/cutils/treesum.py	Mon May 12 12:12:44 2025 +0200
@@ -1844,10 +1844,9 @@
                 if mo:
                     self._update_crc(line)
                     if mo.group(2):
-                        return ("SIZE", mo.group(1),
-                                int(util.n(mo.group(3))
-                                        .translate(None, "., '_"),
-                                    10))
+                        return ("SIZE",
+                                mo.group(1),
+                                util.parse_grouped_decimal_number(mo.group(3)))
                     else:
                         return ("SIZE", mo.group(1), None)
                 else:
@@ -1868,9 +1867,8 @@
                                 digest = None
                             if mo.group(5):
                                 if mo.group(6):
-                                    size = int(util.n(mo.group(6))
-                                                   .translate(None, "., '_"),
-                                               10)
+                                    size = util.parse_grouped_decimal_number(
+                                        mo.group(6))
                                 else:
                                     size = None
                             else:
--- a/cutils/util/__init__.py	Mon May 12 09:48:23 2025 +0200
+++ b/cutils/util/__init__.py	Mon May 12 12:12:44 2025 +0200
@@ -13,6 +13,7 @@
 __all__ = ["PY2",
            "PY35",
            "n", "b", "u",
+           "parse_grouped_decimal_number",
            "normalize_filename",
            "escape_for_output",
            "argv2algo",
@@ -57,6 +58,9 @@
             return s.decode(encoding)
         return s
 
+    def parse_grouped_decimal_number(s):
+        return int(n(s).translate(None, "., '_"), 10)
+
 else:
 
     def n(s, encoding="ascii"):
@@ -73,6 +77,17 @@
 
     u = n
 
+    def parse_grouped_decimal_number(s):
+        return int(n(s).translate(NORMALIZATION_DELETE_CHARS), 10)
+
+    NORMALIZATION_DELETE_CHARS = {
+        ord('.'): None,
+        ord(','): None,
+        ord(' '): None,
+        ord("'"): None,
+        ord('_'): None
+    }
+
 
 def escape_for_output(what):
     """Escape `what` in such a way that the output can be safely written into