我使用的是 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
微软文档:
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;
提前致谢
最佳答案
看起来你已经完成了一个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)]
关于python - python2.5中的Exception_Record问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2496258/