python2.5问题中的异常记录

2024-06-16 11:05:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python2.5&下面的代码产生2个错误。 有人能帮我吗?你知道吗

class EXCEPTION_RECORD(Structure):
    _fields_ = [
        ("ExceptionCode", DWORD),
        ("ExceptionFlags", DWORD),
        ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
        ("ExceptionAddress", LPVOID),
        ("NumberParameters", DWORD),
        ("ExceptionInformation", ULONG_PTR * EXCEPTION_MAXIMUM_PARAMETERS)]

Python错误:

Traceback (most recent call last):
  File "E:\Python25\my_debugger_defines.py", line 70, in <module>
    class EXCEPTION_RECORD(Structure):
  File "E:\Python25\my_debugger_defines.py", line 74, in EXCEPTION_RECORD
    ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
NameError: name 'EXCEPTION_RECORD' is not defined

Microsoft文档:

The EXCEPTION_RECORD structure describes an exception. 

typedef struct _EXCEPTION_RECORD { // exr  
    DWORD ExceptionCode; 
    DWORD ExceptionFlags; 
    struct _EXCEPTION_RECORD *ExceptionRecord; 
    PVOID ExceptionAddress; 
    DWORD NumberParameters; 
    DWORD ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]; 
} EXCEPTION_RECORD; 

提前谢谢


Tags: 错误exceptionrecordstructureclassparameterspointermaximum
3条回答

显然,在定义类时不能引用类类型,例如:

>>> class C:
    f = C



Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    class C:
  File "<pyshell#17>", line 2, in C
    f = C
NameError: name 'C' is not defined

但是,您可以通过执行以下操作来解决此问题:

>>> class C:
    pass

>>> C.f = C    

我将重新编写您的代码,如下所示:

class EXCEPTION_RECORD(Structure):
    pass

EXCEPTION_RECORD._fields_ = [
        ("ExceptionCode", DWORD),
        ("ExceptionFlags", DWORD),
        ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
        ("ExceptionAddress", LPVOID),
        ("NumberParameters", DWORD),
        ("ExceptionInformation", ULONG_PTR * EXCEPTION_MAXIMUM_PARAMETERS)]

看起来你做了一个from ctypes import *(糟糕的做法,因为人们只能猜测像DWORD这样的标识符到底是从哪里来的!-)但错过了ctypes.Structure's docs中一段关键的短文:

It is possible to define the fields class variable after the class statement that defines the Structure subclass, this allows to create data types that directly or indirectly reference themselves:

class List(Structure):
    pass
List._fields_ = [("pnext", POINTER(List)),
                 ...
                ]

The fields class variable must, however, be defined before the type is first used (an instance is created, sizeof() is called on it, and so on). Later assignments to the fields class variable will raise an AttributeError.

因此,只需将这部分文档应用到您的代码中,您就需要将代码更改为:

class EXCEPTION_RECORD(Structure):
    pass
EXCEPTION_RECORD._fields_ = [
        ("ExceptionCode", DWORD),
        ("ExceptionFlags", DWORD),
        ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
        ("ExceptionAddress", LPVOID),
        ("NumberParameters", DWORD),
        ("ExceptionInformation", ULONG_PTR * EXCEPTION_MAXIMUM_PARAMETERS)]

类可以在__new____init__方法中引用自身。你知道吗

class Test(object):
    def __new__(cls):
        inst = object.__new__(cls)
        inst.me = Test
        return inst

或:

class Test(object):
    def __init__(self):
        super(Test, self).__init__()
        self.me = Test

或者,如果你真的想得到幻想,你可以使用__metaclass__。你知道吗

相关问题 更多 >