使用元类将Python2转换为Python3会导致错误的流程

时间:2015-02-24 13:56:57

标签: python metaclass python-2to3

我有一个非常大的Python 2.7.6项目,我需要将其转换为Python 3.4。我使用了2to3脚本但是'元类'处理似乎被打破了。

我过滤了代码以缩短并查明问题。以下片段适用于Python 2.7.6:

class Base(object):
    class __metaclass__(type):
        def __new__(cls, classname, bases, dict):
            new = type.__new__(cls, classname, bases, dict)
            new.classname = classname
            print ("Base::__metaclass__::new. Called.")
            return new                 

class Heir(Base):
    class __metaclass__(Base.__metaclass__):
        def __new__(self, *args):
            new = Base.__metaclass__.__new__(self, *args)
            print ("Heir::__metaclass__::new. Called.")
            return new

    @classmethod
    def define(cls, nexttype):
        print ("Heir::define. Called.")

class HeirOfHeir(Heir):
    pass

Heir.define(HeirOfHeir)

代码按预期打印:

Base::__metaclass__::new. Called.
Base::__metaclass__::new. Called.
Heir::__metaclass__::new. Called.
Base::__metaclass__::new. Called.
Heir::__metaclass__::new. Called.
Heir::define. Called.

但是当使用Python 3.4运行代码时,我只有最后一个打印:

Heir::define. Called.

2to3计算错误或需要手动工作。不幸的是,我对元类很少有经验。

1 个答案:

答案 0 :(得分:8)

您的原始代码使用以下事实:类主体中的名称 __metaclass__用作元类,但2to3修复程序仅查找直接分配

__metaclass__ = MetaClassName

而不是class __metaclass__语句或其他定义名称的方式(from somemodule import MetaClassName as __metaclass__可以在Python 2类体中使用,而2to3也会错过。)

您可以通过将元类移动到单独的class定义来解决此问题:

class BaseMeta(type):
    def __new__(cls, classname, bases, dict):
        new = type.__new__(cls, classname, bases, dict)
        new.classname = classname
        print ("BaseMeta::new. Called.")
        return new                 

class Base(object):
    __metaclass__ = BaseMeta

class HeirMeta(BaseMeta):
    def __new__(self, *args):
        new = BaseMeta.__new__(self, *args)
        print ("HeirMeta::new. Called.")
        return new

class Heir(Base):    
    __metaclass__ = HeirMeta

    @classmethod
    def define(cls, nexttype):
        print ("Heir::define. Called.")

class HeirOfHeir(Heir):
    pass

Heir.define(HeirOfHeir)

来执行此操作来定义Python 3 中的元类,因为定义元类的机制已更改为确定元类之前 em>类主体是运行而不是在运行期间(因此元类也可以影响该步骤)。

现在2to3将正确检测到您的类上有__metaclass__属性并重写它们以使用新的Python 3语法:

stackoverflow-2.7 $ bin/python -m lib2to3 fixed.py 
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored fixed.py
--- fixed.py    (original)
+++ fixed.py    (refactored)
@@ -5,8 +5,8 @@
         print ("BaseMeta::new. Called.")
         return new                 

-class Base(object):
-    __metaclass__ = BaseMeta
+class Base(object, metaclass=BaseMeta):
+    pass

 class HeirMeta(BaseMeta):
     def __new__(self, *args):
@@ -14,9 +14,7 @@
         print ("HeirMeta::new. Called.")
         return new

-class Heir(Base):    
-    __metaclass__ = HeirMeta
-
+class Heir(Base, metaclass=HeirMeta):    
     @classmethod
     def define(cls, nexttype):
         print ("Heir::define. Called.")
RefactoringTool: Files that need to be modified:
RefactoringTool: fixed.py

并且重构的代码按预期工作:

stackoverflow-2.7 $ bin/python -m lib2to3 -o ../stackoverflow-3.4 -nw --no-diffs fixed.py 
lib2to3.main: Output in '../stackoverflow-3.4' will mirror the input directory '' layout.
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored fixed.py
RefactoringTool: Writing converted fixed.py to ../stackoverflow-3.4/fixed.py.
RefactoringTool: Files that were modified:
RefactoringTool: fixed.py
stackoverflow-2.7 $ cd ../stackoverflow-3.4
stackoverflow-3.4 $ bin/python -V
Python 3.4.2
stackoverflow-3.4 $ bin/python fixed.py 
BaseMeta::new. Called.
BaseMeta::new. Called.
HeirMeta::new. Called.
BaseMeta::new. Called.
HeirMeta::new. Called.
Heir::define. Called.