tommikaikkonen / prettyprinter

Syntax-highlighting, declarative and composable pretty printer for Python 3.5+
https://prettyprinter.readthedocs.io
MIT License
336 stars 20 forks source link

Infinite loops for hard cases? #25

Closed amueller closed 5 years ago

amueller commented 6 years ago

Description

prettyprinter.pprint({'a': {'a': {'a': {'a': {'a': {'a': {'a': {'a': {'a': {'a': {'a': {'a': {'a': 1}}}}}}}}}}}}}, width=30)

doesn't terminate

carlwgeorge commented 5 years ago

I can reproduce with 0.14.0, Python 3.7.2, Fedora 29.

You don't need a complex object either, you just have to set the width to shorter than what is logically needed. prettyprinter.pprint('a', width=3) works, but prettyprinter.pprint('a', width=2) hangs. The original reproducer works if you set the width to at least 55.

tommikaikkonen commented 5 years ago

Thanks for reporting this. Fixed in #33. Will craft a release soon.

The culprit was the string prettyprinter, which side-steps the normal layout algorithm using the Contextual doc type to decide how it wants to split up a string to multiple lines based on remaining available width at that position. It didn't account for the case where a nested structure is deep enough to eat up all the available width and got stuck in an infinite loop.

tommikaikkonen commented 5 years ago

The fix for this is released in v0.15.0.