11

我跑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")

我如何解释这个输出?

4

2 回答 2

8

修改由fixerunicode触发。此修复程序将解释每个字符串文字的内容,并尝试重新转义无效的 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 仍将其解释为令牌树正在更改,因此它显示“需要修改的文件”。但是,实际的源代码是一样的,所以有“没有改变 foo.py”。

如果第 3 步返回 None ,它会真正说“没有文件需要修改”。

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

于 2016-10-25T13:01:09.173 回答
7

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

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

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

于 2016-10-25T12:54:02.540 回答