【问题标题】:2to3 says "No changes needed", then "files that need to be modified"2to3 说“不需要更改”,然后是“需要修改的文件”
【发布时间】:2016-10-25 12:39:41
【问题描述】:

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

我如何解释这个输出?

【问题讨论】:

    标签: python python-2to3


    【解决方案1】:

    修改由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 仍将其解释为令牌树正在更改,因此它显示“需要修改的文件”。但是实际的源代码是一样的,所以有"No changes to foo.py"。

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 通过“修复者关心的东西”......这是什么意思?我需要关心吗?
      • @JETM 澄清一下,这意味着文件已经过检查,而不是需要更改
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多