2

我在看Python: Exception in the separate module works wrong它使用多用途 GnuLibError 类来“代替”各种不同的错误。每个子错误都有自己的 ID 号和错误格式字符串。

我认为最好将其编写为 Exception 类的层次结构,并着手这样做:

class GNULibError(Exception):
    sub_exceptions = 0  # patched with dict of subclasses once subclasses are created
    err_num = 0
    err_format = None

    def __new__(cls, *args):
        print("new {}".format(cls)) # DEBUG
        if len(args) and args[0] in GNULibError.sub_exceptions:
            print("  factory -> {} {}".format(GNULibError.sub_exceptions[args[0]], args[1:])) # DEBUG
            return super(GNULibError, cls).__new__(GNULibError.sub_exceptions[args[0]], *(args[1:]))
        else:
            print("  plain {} {}".format(cls, args)) # DEBUG
            return super(GNULibError, cls).__new__(cls, *args)

    def __init__(self, *args):
        cls = type(self)
        print("init {} {}".format(cls, args)) # DEBUG
        self.args = args
        if cls.err_format is None:
            self.message = str(args)
        else:
            self.message = "[GNU Error {}] ".format(cls.err_num) + cls.err_format.format(*args)

    def __str__(self):
        return self.message

    def __repr__(self):
        return '{}{}'.format(type(self).__name__, self.args)

class GNULibError_Directory(GNULibError):
    err_num = 1
    err_format = "destination directory does not exist: {}"

class GNULibError_Config(GNULibError):
    err_num = 2
    err_format = "configure file does not exist: {}"

class GNULibError_Module(GNULibError):
    err_num = 3
    err_format = "selected module does not exist: {}"

class GNULibError_Cache(GNULibError):
    err_num = 4
    err_format = "{} is expected to contain gl_M4_BASE({})"

class GNULibError_Sourcebase(GNULibError):
    err_num = 5
    err_format = "missing sourcebase argument: {}"

class GNULibError_Docbase(GNULibError):
    err_num = 6
    err_format = "missing docbase argument: {}"

class GNULibError_Testbase(GNULibError):
    err_num = 7
    err_format = "missing testsbase argument: {}"

class GNULibError_Libname(GNULibError):
    err_num = 8
    err_format = "missing libname argument: {}"

# patch master class with subclass reference
# (TO DO: auto-detect all available subclasses instead of hardcoding them)
GNULibError.sub_exceptions = {
    1: GNULibError_Directory,
    2: GNULibError_Config,
    3: GNULibError_Module,
    4: GNULibError_Cache,
    5: GNULibError_Sourcebase,
    6: GNULibError_Docbase,
    7: GNULibError_Testbase,
    8: GNULibError_Libname
}

这以 GNULibError 作为工厂类开始 - 如果您使用属于可识别子类的错误号调用它,它会返回一个属于该子类的对象,否则它会将自身作为默认错误类型返回。

基于此代码,以下内容应该完全等效(但不是):

e = GNULibError(3, 'missing.lib')
f = GNULibError_Module('missing.lib')

print e  # -> '[GNU Error 3] selected module does not exist: 3'
print f  # -> '[GNU Error 3] selected module does not exist: missing.lib'

我添加了一些战略打印语句,错误似乎在GNULibError.__new__

>>> e = GNULibError(3, 'missing.lib')

new <class '__main__.GNULibError'>
  factory -> <class '__main__.GNULibError_Module'> ('missing.lib',)  # good...
init <class '__main__.GNULibError_Module'> (3, 'missing.lib')        # NO!
                                            ^
                                           why?

我称子类构造函数为subclass.__new__(*args[1:])- 这应该删除 3,子类类型 ID - 但它__init__仍然得到 3!如何修剪传递给的参数列表subclass.__init__

4

3 回答 3

2

您不能影响传递给 的内容__init__,只要您使用像现在这样返回自身子类的“工厂类”来执行此操作。仍然传递“3”参数的原因是因为您仍然从__new__. 等到__new__被调用时,再决定传递给 . 的内容为时已晚__init__。如文档中所述(强调添加):

如果__new__() 返回一个 cls 的实例,则新实例的__init__() 方法将像 一样被调用__init__(self[, ...]),其中 self 是新实例,其余参数与传递给 的参数相同__new__()

换句话说,当您调用 时GNULibError(3, 'missing.lib'),为时已晚 --- 通过使用这些参数调用类,您已确保这些参数将传递给__init__. __new__可以返回与您可能获得的实例不同的实例,但它不能阻止正常初始化的发生。

正如@Ned Batchelder 所建议的,你最好使用工厂函数而不是“工厂类”,因为函数没有这个__new__/__init__机器,你可以只返回你想要的类的实例。

于 2012-06-24T02:59:14.013 回答
0

这比它需要的要复杂得多。不要试图让一个类创建另一个类的对象。编写一个工厂函数来创建你的异常,不要乱用__new__. 正如您所发现的那样,这太棘手了。

于 2012-06-24T02:34:42.993 回答
0

在您的用例中 - 我同意 Ned - 它需要更复杂。

您可以尝试诸如(基于您的派生类似乎没有做任何事情但与错误消息不同的事实)。

class GNULibError(Exception):
    pass # put logic code here

GNULibErrors = {
    1: type('GNULibError_Directory', (GNULibError,), {'message': 'suitable whatever here'})
}

并从那里调整...

于 2012-06-24T03:07:17.400 回答