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
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:

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 ''
            return super(SuperFormatter, self).format_field(value, spec)

data = {

tmpl = '''

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

Causes error:

$ python3.6 my_progs/ 
Traceback (most recent call last):
  File "my_progs/", line 37, in <module>
    out = sf.format(tmpl, **data)
  File "/usr/lib64/python3.6/", line 190, in format
    return self.vformat(format_string, args, kwargs)
  File "/usr/lib64/python3.6/", line 194, in vformat
    result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
  File "/usr/lib64/python3.6/", line 244, in _vformat
  File "/usr/lib64/python3.6/", line 244, in _vformat
  File "/usr/lib64/python3.6/", line 244, in _vformat
  File "/usr/lib64/python3.6/", 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.
Date User Action Args
2018-04-13 11:41:47eric.smithsetnosy: + eric.smith
2018-04-13 11:12:03mv.gavrilovcreate