ducminh-phan / reformat-gherkin

Formatter for Gherkin language
MIT License
24 stars 13 forks source link

A comment block of 332 lines or longer causes a RecursionError #33

Closed rayjolt closed 4 years ago

rayjolt commented 4 years ago

When reformat-gherkin tries to format a comment block of longer than 332 lines, a RecursionError occurs.

Steps to reproduce

Run the following script:

from reformat_gherkin.core import format_file_contents
from reformat_gherkin.options import (
    Options,
    WriteBackMode,
    AlignmentMode,
    NewlineMode,
    TagLineMode,
)

options = Options(
    write_back=WriteBackMode.CHECK,
    step_keyword_alignment=AlignmentMode.NONE,
    newline=NewlineMode.KEEP,
    tag_line_mode=TagLineMode.SINGLELINE,
    fast=False,
)
format_file_contents("#a\n" * 332, options=options)

332 is the least number of lines required to cause the error on my system. If no error occurs, try increasing the number (1001 should do it, as 1000 is the default stack size limit in Python).

Expected result

The file should reformat successfully, causing the script to exit with no errors.

Actual result

The following error occurs:

Traceback (most recent call last):
  File "recursion_limit.py", line 11, in <module>
    format_file_contents("#a\n" * 332, options=options)
  File "/path/to/reformat_gherkin/core.py", line 98, in format_file_contents
    assert_stable(src_contents, dst_contents, options=options)
  File "/path/to/reformat_gherkin/core.py", line 160, in assert_stable
    new_dst = format_str(dst, options=options)
  File "/path/to/reformat_gherkin/core.py", line 114, in format_str
    return "\n".join(lines)
  File "/path/to/reformat_gherkin/formatter.py", line 339, in generate
    yield from self.visit(node)
  File "/path/to/reformat_gherkin/formatter.py", line 349, in visit
    )(node)
  File "/path/to/reformat_gherkin/formatter.py", line 409, in visit_comment
    next_line = next(self.visit(context))
  File "/path/to/reformat_gherkin/formatter.py", line 349, in visit
    )(node)
  File "/path/to/reformat_gherkin/formatter.py", line 409, in visit_comment
    next_line = next(self.visit(context))
  File "/path/to/reformat_gherkin/formatter.py", line 349, in visit
    )(node)

<snip>

  File "/path/to/reformat_gherkin/formatter.py", line 409, in visit_comment
    next_line = next(self.visit(context))
  File "/path/to/reformat_gherkin/formatter.py", line 345, in visit
    class_name = type(node).__name__
RecursionError: maximum recursion depth exceeded while calling a Python object

Environment