The documentation for shlex does not prohibit the user from setting .posix=True after creating a shlex object. When doing so, the .eof attribute is inconsistent, creating an infinite loop in the __next__ method. Here's some sample code to recreate the issue: import shlex s = shlex.shlex(r"", posix=False) s.posix = True list(s) One possible solution is to make .posix a read-only property. Another is to make .posix a property which sets .eof correctly.
Making .posix read-only is technically backward-incompatible, but I'm not sure if there are cases where people might have changed its value without incurring in the bug. Leaving .posix read/writable and changing .eof accordingly might be a better solution. As a side note, you can use assertRaises (possibly as a context manager) in the tests.
There are 18 public writable attributes in shlex object, and unthinking setting some of them can make shlex to produce incorrect results or create an infinite loop. For example there is nothing to prevent your from setting the eof attribute. Only 14 of 18 attributes is documented and posix is not in this set. One of solutions is just does nothing. We are all consenting adults here. More protective solution is to make undocumented attributes (filestack, posix, pushback, state) private. For backward compatibility we can temporary add properties that will emit deprecation warnings.