python版本2.6和3.1中出现了奇怪的语法错误

时间:2022-08-09 22:43:40

this may not be an earth-shattering deficiency of python, but i still wonder about the rationale behind the following behavior: when i run

这可能不是python的惊人缺陷,但我仍然想知道以下行为背后的基本原理:当我运行时

source = """
print( 'helo' )
if __name__ == '__main__':
  print( 'yeah!' )

#"""

print( compile( source, '<whatever>', 'exec' ) )

i get ::

我::

  File "<whatever>", line 6
    #
    ^
SyntaxError: invalid syntax

i can avoid this exception by (1) deleting the trailing #; (2) deleting or outcommenting the if __name__ == '__main__':\n print( 'yeah!' ) lines; (3) add a newline to very end of the source.

我可以通过(1)删除后面的#来避免这个异常;(2)删除或注释if __name__ = '__main__':\n print(' '是的!”)线;(3)在源代码的最末端添加一个换行符。

moreover, if i have the source end without a trailing newline right behind the print( 'yeah!' ), the source will also compile without error.

此外,如果源端在打印后没有拖尾换行(“是的!”),源代码也将不会出错地编译。

i could also reproduce this behavior with python 2.6, so it’s not new to the 3k series.

我也可以用python 2.6复制这种行为,所以这对3k系列来说并不新鲜。

i find this error to be highly irritating, all the more since when i put above source inside a file and execute it directly or have it imported, no error will occur—which is the expected behavior.

我发现这个错误非常令人恼火,尤其是当我把上面的源代码放在一个文件中并直接执行它或者导入它时,不会出现错误——这是预期的行为。

a # (hash) outside a string literal should always represent the start of a (possibly empty) comment in a python source; moreover, the presence or absence of a if __name__ == '__main__' clause should not change the interpretation of a soure on a syntactical level.

字符串文本之外的#(哈希)应该始终表示python源代码中(可能为空)注释的开始;此外,if __name__ = '__main__'子句的存在或不存在不应该在句法层面上改变对源的解释。

can anyone reproduce the above problem, and/or comment on the phenomenon?

是否有人能重现上述问题,并/或对这一现象发表评论?

cheers

干杯

1 个解决方案

#1


3  

update

turns out this is indeed a bug as pointed out by http://groups.google.com/group/comp.lang.python/msg/b4842cc7abd75fe9; the bug report is at http://bugs.python.org/issue1184112; it appears to be fixed in 2.7 and 3.2.

正如http://groups.google.com/group/comp.lang.python/msg/b4842cc7abd75fe9指出的那样,这确实是一个错误;错误报告位于http://bugs.python.org/issue1184112;它在2.7和3.2中似乎是固定的。

solution

once recognized, this bug is extremely simple to fix: since a valid python source should stay both syntactically valid and semantically unchanged when a newline is added to the source text, just mechanically do just that to any source text. this reminds me of the ; semicolon you mechanically put in between source texts when assembling a multi-file javascript source for efficient gzipped delivery to the remote client.

一旦被识别,这个bug就非常容易修复:因为当向源文本添加换行符时,有效的python源代码在语法上应该是有效的,在语义上应该是不变的,只要对任何源文本进行机械操作即可。这让我想起;在组装多文件javascript源文件时,您可以在源文本之间机械地放入分号,以便高效地将gziked交付到远程客户端。

#1


3  

update

turns out this is indeed a bug as pointed out by http://groups.google.com/group/comp.lang.python/msg/b4842cc7abd75fe9; the bug report is at http://bugs.python.org/issue1184112; it appears to be fixed in 2.7 and 3.2.

正如http://groups.google.com/group/comp.lang.python/msg/b4842cc7abd75fe9指出的那样,这确实是一个错误;错误报告位于http://bugs.python.org/issue1184112;它在2.7和3.2中似乎是固定的。

solution

once recognized, this bug is extremely simple to fix: since a valid python source should stay both syntactically valid and semantically unchanged when a newline is added to the source text, just mechanically do just that to any source text. this reminds me of the ; semicolon you mechanically put in between source texts when assembling a multi-file javascript source for efficient gzipped delivery to the remote client.

一旦被识别,这个bug就非常容易修复:因为当向源文本添加换行符时,有效的python源代码在语法上应该是有效的,在语义上应该是不变的,只要对任何源文本进行机械操作即可。这让我想起;在组装多文件javascript源文件时,您可以在源文本之间机械地放入分号,以便高效地将gziked交付到远程客户端。