2to3说“不需要更改”,然后是“需要修改的文件”

时间:2016-10-25 12:39:41

标签: python python-2to3

我运行2to3 -f all -f idioms -f buffer -f set_literal -f ws_comma foo.py

输出:

RefactoringTool: No changes to foo.py
RefactoringTool: Files that need to be modified:
RefactoringTool: foo.py

foo.py的内容:

print("Hi")

如何解释此输出?

2 个答案:

答案 0 :(得分:3)

根据Steven D'Aprano,这是一个错误,输出中的第二行文本应解释为:

  

包含修复程序关心的内容的文件,无论是否   它被修改了。

在您的情况下,foo.py代码与Python 3完全兼容,并且不需要进行任何更改作为输出的第一行。

答案 1 :(得分:3)

修改由the unicode fixer触发。此修复程序将解释每个字符串文字的内容,并尝试重新转义无效的Unicode序列,并删除u / U字符串前缀:

def transform(self, node, results):
    ...
    elif node.type == token.STRING:
        # 1. Replace the invalid \u sequences.
        val = node.value
        if not self.unicode_literals and val[0] in '\'"' and '\\' in val:
            val = r'\\'.join([
                v.replace('\\u', r'\\u').replace('\\U', r'\\U')
                for v in val.split(r'\\')
            ])

        # 2. Strip the leading `u` in u"...."
        if val[0] in 'uU':
            val = val[1:]

        # 3. If the whole string is the same, return the original node.
        if val == node.value:
            return node   # <--------------

        # 4. Otherwise, create a new node.
        new = node.clone()
        new.value = val
        return new

由于某些未知原因(错误?),即使在步骤3中返回原始节点,lib2to3仍然将其解释为更改了令牌树,因此它表示&#34;需要修改的文件&#34; 。但是,实际的源代码是相同的,所以有#34; foo.py&#34;没有变化。

如果步骤3返回None,它将真正说出&#34;没有文件需要修改&#34;。

只会使用原始输入重写受影响的文件。所以这个bug是无害的。