mattrobenolt / jinja2-cli

CLI for Jinja2
BSD 2-Clause "Simplified" License
516 stars 182 forks source link

bug in querystring parsing #116

Closed ece-mohammad closed 1 year ago

ece-mohammad commented 1 year ago

python version: 3.10.12 os: Linux 6.2.0-34-generic #34~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 7 13:12:03 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Running jinja2 with the querystring format results in the error:

Traceback (most recent call last):
  File "/home/pegasus/Projects/Jinja2Client/.venv/lib/python3.10/site-packages/jinja2cli/cli.py", line 304, in cli
    data = fn(data) or {}
  File "/home/pegasus/Projects/Jinja2Client/.venv/lib/python3.10/site-packages/jinja2cli/cli.py", line 161, in _parse_qs
    v = v[0] if len(v) == 1 else v
TypeError: object of type 'map' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pegasus/Projects/Jinja2Client/.venv/bin/jinja2", line 8, in <module>
    sys.exit(main())
  File "/home/pegasus/Projects/Jinja2Client/.venv/lib/python3.10/site-packages/jinja2cli/cli.py", line 449, in main
    sys.exit(cli(opts, args))
  File "/home/pegasus/Projects/Jinja2Client/.venv/lib/python3.10/site-packages/jinja2cli/cli.py", line 306, in cli
    raise raise_exc("%s ..." % data[:60])
jinja2cli.cli.MalformedQuerystring: title=foobar
 ...

Reproduction steps

$ md jinj2cli && cd jinja2cli
$ python -m virtualenv .venv
$ source .venv/bin/activate
$ pip install jinja2cli
$ echo title=foobar > data.qs
$ echo "{{ title }}" > template.j2
$ jinja2 -f querystring template.j2 data.qs
 ...
 ...
 TypeError: object of type `map` has no len()
 ...
 ...
 ...
 jinja2cli.cli.MalformedQuerystring: title=foobar

The error is a result of the following line in _parse_qs function:

https://github.com/mattrobenolt/jinja2-cli/blob/c540c4cd3566ce8608370b41656a978a07d0cf54/jinja2cli/cli.py#L169

In Python3, the map() function returns a map object. But that can be fixed by casting v to a list:

v = list(map(lambda x: x.strip(), v))