以下示例代码:
import token, tokenize, StringIO
def generate_tokens(src):
rawstr = StringIO.StringIO(unicode(src))
tokens = tokenize.generate_tokens(rawstr.readline)
for i, item in enumerate(tokens):
toktype, toktext, (srow,scol), (erow,ecol), line = item
print i, token.tok_name[toktype], toktext
s = \
"""
def test(x):
\"\"\" test with an unterminated docstring
"""
generate_tokens(s)
导致以下触发:
... (stripped a little)
File "/usr/lib/python2.6/tokenize.py", line 296, in generate_tokens
raise TokenError, ("EOF in multi-line string", strstart)
tokenize.TokenError: ('EOF in multi-line string', (3, 5))
关于这种行为的一些问题:
- 我应该在这里捕获并“选择性地”忽略 tokenize.TokenError 吗?还是我应该停止尝试从不合规/不完整的代码生成令牌?如果是这样,我将如何检查?
- 此错误(或类似错误)是否由未终止的文档字符串以外的任何内容引起?