File CVE-2024-34062-CLI-injection.patch of Package python3-tqdm

---
 tqdm/cli.py |   50 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 18 deletions(-)

--- a/tqdm/cli.py
+++ b/tqdm/cli.py
@@ -15,26 +15,36 @@ def cast(val, typ):
                 return cast(val, t)
             except TqdmTypeError:
                 pass
-        raise TqdmTypeError(val + ' : ' + typ)
+        raise TqdmTypeError("{} : {}".format(val, typ))
 
     # sys.stderr.write('\ndebug | `val:type`: `' + val + ':' + typ + '`.\n')
     if typ == 'bool':
         if (val == 'True') or (val == ''):
             return True
-        elif val == 'False':
+        if val == 'False':
             return False
-        else:
-            raise TqdmTypeError(val + ' : ' + typ)
-    try:
-        return eval(typ + '("' + val + '")')
-    except:
-        if typ == 'chr':
-            return chr(ord(eval('"' + val + '"')))
-        else:
-            raise TqdmTypeError(val + ' : ' + typ)
-
+        raise TqdmTypeError("{} : {}".format(val, typ))
+    if typ == 'chr':
+        if len(val) == 1:
+            return val.encode()
+        if re.match(r"^\\\w+$", val):
+            return eval('"{}"'.format(val)).encode()
+        raise TqdmTypeError("{} : {}".format(val, typ))
+    if typ == 'str':
+        return val
+    if typ == 'int':
+        try:
+            return int(val)
+        except ValueError as exc:
+            raise TqdmTypeError("{} : {}".format(val, typ)) from exc
+    if typ == 'float':
+        try:
+            return float(val)
+        except ValueError as exc:
+            raise TqdmTypeError("{} : {}".format(val, typ)) from exc
+    raise TqdmTypeError("{} : {}".format(val, typ))
 
-def posix_pipe(fin, fout, delim='\n', buf_size=256,
+def posix_pipe(fin, fout, delim=b'\\n', buf_size=256,
                callback=lambda int: None  # pragma: no cover
                ):
     """
@@ -50,6 +60,8 @@ def posix_pipe(fin, fout, delim='\n', bu
     if not delim:
         while True:
             tmp = fin.read(buf_size)
+            if isinstance(tmp, str):
+                tmp = tmp.encode()
 
             # flush at EOF
             if not tmp:
@@ -60,10 +72,12 @@ def posix_pipe(fin, fout, delim='\n', bu
             callback(len(tmp))
         # return
 
-    buf = ''
+    buf = b''
     # n = 0
     while True:
         tmp = fin.read(buf_size)
+        if isinstance(tmp, str):
+            tmp = tmp.encode()
 
         # flush at EOF
         if not tmp:
@@ -82,7 +96,7 @@ def posix_pipe(fin, fout, delim='\n', bu
             else:
                 fp_write(buf + tmp[:i + len(delim)])
                 callback(1)  # n += 1
-                buf = ''
+                buf = b''
                 tmp = tmp[i + len(delim):]
 
 
@@ -197,7 +211,7 @@ Options:
         raise
     else:
         buf_size = tqdm_args.pop('buf_size', 256)
-        delim = tqdm_args.pop('delim', '\n')
+        delim = tqdm_args.pop('delim', b'\\n')
         delim_per_char = tqdm_args.pop('bytes', False)
         manpath = tqdm_args.pop('manpath', None)
         stdin = getattr(sys.stdin, 'buffer', sys.stdin)
@@ -218,10 +232,10 @@ Options:
             log.debug(tqdm_args)
             with tqdm(**tqdm_args) as t:
                 posix_pipe(stdin, stdout, '', buf_size, t.update)
-        elif delim == '\n':
+        elif delim == b'\\n':
             log.debug(tqdm_args)
             for i in tqdm(stdin, **tqdm_args):
-                stdout.write(i)
+                stdout.write(i.encode() if isinstance(i, str) else i)
         else:
             log.debug(tqdm_args)
             with tqdm(**tqdm_args) as t:
openSUSE Build Service is sponsored by