2to3 表示 "No changes needed",然后 "files that need to be modified"

2to3 says "No changes needed", then "files that need to be modified"

我运行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")

如何解释此输出?

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

Files that include something that a fixer cares about, whether or not it gets modified.

在您的情况下,foo.py 代码与 Python 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 仍将其解释为正在更改的令牌树,因此它显示 "Files that need to be modified"。但是实际源码是一样的,所以有"No changes to foo.py".

如果第 3 步 returns None 相反,它会真正说 "No files need to be modified"。

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