[New-bugs-announce] [issue33272] Which are reasonable reason for recursion limit in function _vformat of class Formatter lib string?

Mikhail report at bugs.python.org
Fri Apr 13 07:12:03 EDT 2018


New submission from Mikhail <mikhail.v.gavrilov at gmail.com>:

The presence of the restriction of recursion prevent making nested conditions for the superformatter: https://github.com/ebrehault/superformatter

for example:

import string


class SuperFormatter(string.Formatter):
    """World's simplest Template engine."""

    def format_field(self, value, spec):
        if spec.startswith('repeat'):
            template = spec.partition(':')[-1]
            if type(value) is dict:
                value = value.items()
            return ''.join([template.format(item=item) for item in value])
        elif spec == 'call':
            return value()
        elif spec.startswith('if'):
            return (value and spec.partition(':')[-1]) or ''
        else:
            return super(SuperFormatter, self).format_field(value, spec)


data = {
	'a1':1,
	'a2':2,
	'a3':3
}

tmpl = '''
{a1:if:
	{a2:if:
		{a2}
	}
	{a1}
}
'''

sf = SuperFormatter()
out = sf.format(tmpl, **data)
print(out)


Causes error:

$ python3.6 my_progs/test.py 
Traceback (most recent call last):
  File "my_progs/test.py", line 37, in <module>
    out = sf.format(tmpl, **data)
  File "/usr/lib64/python3.6/string.py", line 190, in format
    return self.vformat(format_string, args, kwargs)
  File "/usr/lib64/python3.6/string.py", line 194, in vformat
    result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
  File "/usr/lib64/python3.6/string.py", line 244, in _vformat
    auto_arg_index=auto_arg_index)
  File "/usr/lib64/python3.6/string.py", line 244, in _vformat
    auto_arg_index=auto_arg_index)
  File "/usr/lib64/python3.6/string.py", line 244, in _vformat
    auto_arg_index=auto_arg_index)
  File "/usr/lib64/python3.6/string.py", line 201, in _vformat
    raise ValueError('Max string recursion exceeded')
ValueError: Max string recursion exceeded


If there was not this restriction, then the example code is worked.

----------
components: Library (Lib)
messages: 315242
nosy: mv.gavrilov
priority: normal
severity: normal
status: open
title: Which are reasonable reason for recursion limit in function _vformat of class Formatter lib string?
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33272>
_______________________________________


More information about the New-bugs-announce mailing list