jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'integer' (support for hex, octal, and binary integer literals)

huangapple go评论138阅读模式
英文:

jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'integer' (support for hex, octal, and binary integer literals)

问题

在版本 3.1.2 中,这会抛出一个异常:

>>> print(jinja2.Template("{{ '%04d' | format(0777 - '0022' | int) }}").render())

这在版本 2.11.3 中曾经正常工作:

>>> print(jinja2.Template("{{ '%04d' | format(0777 - '0022' | int) }}").render())
0755

我认为行为在 此 PR 中发生了变化。我需要做什么来修复它?

上面的代码片段来自此 Ansible 角色配置:

deploy_cakephp_default_umask: '0022'
deploy_cakephp_default_owner: root
deploy_cakephp_default_group: root
deploy_cakephp_default_directory_mode: "{{ '%04d' | format(0777 - deploy_cakephp_default_umask | int) }}"
deploy_cakephp_default_file_mode: "{{ '%04d' | format(0666 - deploy_cakephp_default_umask | int) }}"
英文:

In version 3.1.2 this is throwing an exception:

>>> print(jinja2.Template("{{ '%04d' | format(0777 - '0022' | int) }}").render())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/.virtualenvs/ansible-playbooks/lib/python3.8/site-packages/jinja2/environment.py", line 1208, in __new__
    return env.from_string(source, template_class=cls)
  File "/root/.virtualenvs/ansible-playbooks/lib/python3.8/site-packages/jinja2/environment.py", line 1105, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
  File "/root/.virtualenvs/ansible-playbooks/lib/python3.8/site-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/root/.virtualenvs/ansible-playbooks/lib/python3.8/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'integer'

While this used to work just fine in version 2.11.3:

>>> print(jinja2.Template("{{ '%04d' | format(0777 - '0022' | int) }}").render())
0755

I think the behavior is changed in this PR. What do I need to do to fix it?

The snippet above it coming from this Ansible role configuration:

deploy_cakephp_default_umask: '0022'
deploy_cakephp_default_owner: root
deploy_cakephp_default_group: root
deploy_cakephp_default_directory_mode: "{{ '%04d' | format(0777 - deploy_cakephp_default_umask | int) }}"
deploy_cakephp_default_file_mode: "{{ '%04d' | format(0666 - deploy_cakephp_default_umask | int) }}"

答案1

得分: 2

I think your problem comes from Jinja and the version of python your are (were) using. C-style octal literals have been dropped in python 3.0 (see https://docs.python.org/3/reference/lexical_analysis.html#integer-literals)

Meanwhile, I was able to achieve your requirement using this Jinja2 expression taking advantage of the octal integer literal notation and the format() function:

{{ '0{0:o}'.format(0o777 - 0o22) }}

See the following test session:

$ pip list | grep Jinja2
Jinja2                 3.1.2

$ python --version
Python 3.10.6
# same result with 3.8.16

$ python
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2
>>> print(jinja2.Template("{{ '0{0:o}'.format(0o777 - 0o22) }}").render())
0755
英文:

I think your problem comes from Jinja and the version of python your are (were) using. C-style octal literals have been dropped in python 3.0 (see https://docs.python.org/3/reference/lexical_analysis.html#integer-literals)

Meanwhile, I was able to achieve your requirement using this Jinja2 expression taking advantage of the octal integer literal notation and the format() function:

{{ '0{0:o}'.format(0o777 - 0o22) }}

See the following test session:

$ pip list | grep Jinja2
Jinja2                 3.1.2

$ python --version
Python 3.10.6
# same result with 3.8.16

$ python
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2
>>> print(jinja2.Template("{{ '0{0:o}'.format(0o777 - 0o22) }}").render())
0755

答案2

得分: 1

现在我正在使用这个:

deploy_cakephp_default_umask: '0022'
deploy_cakephp_default_directory_mode: "{{ '0{0:o}'.format((('0777' | int(base=8)) - (deploy_cakephp_default_umask | int(base=8)))) }}"
deploy_cakephp_default_file_mode: "{{ '0{0:o}'.format((('0666' | int(base=8)) - (deploy_cakephp_default_umask | int(base=8)))) }}"

这在两个版本中都可以工作(对我来说已经足够好了)。对于更新的 Python / Jinja 版本,('0777' | int(base=8)) 也可以简化为 0o777(对于 0o666 也是同样)。

英文:

For now I'm using this:

deploy_cakephp_default_umask: '0022'
deploy_cakephp_default_directory_mode: "{{ '0{0:o}'.format((('0777' | int(base=8)) - (deploy_cakephp_default_umask | int(base=8)))) }}"
deploy_cakephp_default_file_mode: "{{ '0{0:o}'.format((('0666' | int(base=8)) - (deploy_cakephp_default_umask | int(base=8)))) }}"

This works in both versions (and is good enough for me). For newer Python / Jinja versions ('0777' | int(base=8)) could also be simplified to 0o777 (same for 0o666).

huangapple
  • 本文由 发表于 2023年7月3日 22:45:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定