views:

33

answers:

3

I'm using Python2.5 & the following code produce 2 errors. Can any body help me?

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

Python Error:

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 Document:

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; 

Thanks in advance

+2  A: 

Apparently, you can't refer to a class type while defining a class, e.g.:

>>> 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

However, you can work around that by doing this:

>>> class C:
    pass

>>> C.f = C    

I would re-write your code as follows:

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)]
Claudiu
A: 

A class can reference itself in the __new__ or __init__ methods.

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

or:

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

Or, if you wanted to really get fancy you could use a __metaclass__.

Brendan Abel
+1  A: 

Looks like you've done a from ctypes import * (terrible practice, as one is reduced to guess where all those identifiers like DWORD are actually coming from!-) but missed a crucial short passage in 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.

So, simply applying this bit of the docs to your code, you need to change that code into:

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)]
Alex Martelli