Skip to content

Commit

Permalink
Merge pull request from GHSA-g7vv-2v7x-gj9p
Browse files Browse the repository at this point in the history
cli: eval safety
  • Loading branch information
casperdcl committed May 2, 2024
2 parents cc372d0 + b53348c commit 4e613f8
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions tqdm/cli.py
Expand Up @@ -21,23 +21,34 @@ def cast(val, typ):
return cast(val, t)
except TqdmTypeError:
pass
raise TqdmTypeError(val + ' : ' + typ)
raise TqdmTypeError(f"{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 Exception:
if typ == 'chr':
return chr(ord(eval('"' + val + '"'))).encode()
else:
raise TqdmTypeError(val + ' : ' + typ)
raise TqdmTypeError(val + ' : ' + typ)
if typ == 'chr':
if len(val) == 1:
return val.encode()
if re.match(r"^\\\w+$", val):
return eval(f'"{val}"').encode()
raise TqdmTypeError(f"{val} : {typ}")
if typ == 'str':
return val
if typ == 'int':
try:
return int(val)
except ValueError as exc:
raise TqdmTypeError(f"{val} : {typ}") from exc
if typ == 'float':
try:
return float(val)
except ValueError as exc:
raise TqdmTypeError(f"{val} : {typ}") from exc
raise TqdmTypeError(f"{val} : {typ}")


def posix_pipe(fin, fout, delim=b'\\n', buf_size=256,
Expand Down

0 comments on commit 4e613f8

Please sign in to comment.