2010-03-22 56 views
1

我正在使用Python2.5 &以下代码会产生2个错误。 任何机构可以帮助我吗?Exception_Record in python2.5 problem

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 

微软文档:

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; 

在此先感谢

回答

2

看起来你已经做了from ctypes import * - 但错过了ctypes.Structure's docs至关重要的短文(可怕的做法,因为一个是减少猜测其中类似DWORD所有这些标识符实际上来自何处!):

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)] 
2

显然,当定义一个类,你不能引用类类型,例如:

>>> 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)] 
0

类可以参考自己在__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__