Piotr Dabkowski
Piotr Dabkowski

Reputation: 5939

Python SyntaxError with a correct syntax

Why this code gives me a syntax error:

eval(1485*'not ' + '1')

Obviously the syntax is correct. Below 1485 works fine. And with 1496 and above I get a memory error. I think it should rise a MemoryError instead of SyntaxError.

Upvotes: 1

Views: 265

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125058

The parser has limits, and you are hitting them. See http://bugs.python.org/issue1881 for a discussion, but you managed to add enough not operators in there to run out of parser stack space.

You can hit the same limit by nesting lists, see http://bugs.python.org/issue215555:

>>> eval(100 * '[' + 100 * ']')
s_push: parser stack overflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError

Quoting that latter report:

There is a limit that is based on the C stack, because the parser is recursive descent.

Providing code that hits that limit is considered a syntax error too; e.g. 1485 not operators in a row is insane and cannot possibly be considered valid Python. :-)

The reason you are getting a EOF error rather than a MemoryError for certain lengths is simply that the tokenizer manages to get to the end of the line (and signals an EOF) for those lengths, and then the parser stack overflows, rather than that the parser stack overflowed before the tokenizer had seen the end of the line.

Upvotes: 5

Related Questions