n = 201
s = ‘(‘ * n  + ‘)‘ * n
print(eval(s))
Traceback (most recent call last):
  File "/mnt/hgfs/Python-3.9.1/test.py", line 7, in <module>
    print(eval(s))
  File "<string>", line 1
    ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
                                                                                                                                                                                                           ^
SyntaxError: too many nested parentheses
当 n <= 200 时,程序是正常的。
在 Python 源码目录下,使用 find 指令寻找源码中对应的错误信息:
$ find ./ -name "*.[ch]" | xargs grep "too many nested parentheses"
./Parser/tokenizer.c:            return syntaxerror(tok, "too many nested parentheses");
在 $Python-3.9.1/Parser/tokenizer.h 中:
#define MAXLEVEL 200    /* Max parentheses level */
在 $Python-3.9.1/Parser/tokenizer.c 中可以看出,这个宏限制的不仅是小括号,中括号和大括号也是一起计算的。
    /* Keep track of parentheses nesting level */
    switch (c) {
    case ‘(‘:
    case ‘[‘:
    case ‘{‘:
        if (tok->level >= MAXLEVEL) {
            return syntaxerror(tok, "too many nested parentheses");
        }
        tok->parenstack[tok->level] = c;
        tok->parenlinenostack[tok->level] = tok->lineno;
        tok->level++;
        break;
    case ‘)‘:
    case ‘]‘:
    case ‘}‘:
        if (!tok->level) {
            return syntaxerror(tok, "unmatched ‘%c‘", c);
        }
        tok->level--;
        int opening = tok->parenstack[tok->level];
	    ...
把 MAXLEVEL 改为 300 ,然后重新编译 Python ,程序成功执行:
()
当 n 增大到 301 时,再次报错 SyntaxError: too many nested parentheses
MAXLEVEL 这个宏不是一直就有的,我记得它是在 3.8 的某个版本引入的,详细信息参考 issue33306
我下载了 Python 3.7.9 ,这个版本在 n >= 94 时报错:
s_push: parser stack overflow
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    print(eval(s))
MemoryError
寻找错误信息对应的源码:
$ find ./ -name "*.[ch]" | xargs grep "parser stack overflow"
./Parser/parser.c:        fprintf(stderr, "s_push: parser stack overflow\n");
然后在 ./Parser/parser.h 中找到 DFA 栈的大小:
#define MAXSTACK 1500
将这个值改为 3000 然后重新编译,最大括号层数从 93 增长到了 187
stackentry       s_base[MAXSTACK]; 里面都存了什么东西,为什么 MAXSTACK 的值都上千了,最大括号层数还不是很多?这些问题只有在仔细研究源码以后才能了解,暂时搁置了,以后有时间再回来填上这个坑。
Python 括号层数限制(SyntaxError: too many nested parentheses)
原文:https://www.cnblogs.com/adjwang/p/14268551.html