classification
Title: Which are reasonable reason for recursion limit in function _vformat of class Formatter lib string?
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, mv.gavrilov
Priority: normal Keywords:

Created on 2018-04-13 11:12 by mv.gavrilov, last changed 2018-04-13 11:41 by eric.smith.

Messages (1)
msg315242 - (view) Author: Mikhail (mv.gavrilov) Date: 2018-04-13 11:12
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.
History
Date User Action Args
2018-04-13 11:41:47eric.smithsetnosy: + eric.smith
2018-04-13 11:12:03mv.gavrilovcreate