Simplify things a bit.

Instead of having 2 macros for each object simplify by having 1 set of
macros that can work across all objects except the parsed object. I could
make this work for the parsed object by making the parsed object store
PyObject pointers to the parsed values instead of creating them on the fly
while getting an attribute.
This commit is contained in:
Wesley Shields 2013-11-30 21:54:38 -05:00
parent 7abab7bd2e
commit 5fb0afd098

View File

@ -31,6 +31,18 @@
#define PEPY_VERSION "0.1" #define PEPY_VERSION "0.1"
/* These are used to across multiple objects. */
#define PEPY_OBJECT_GET(OBJ, ATTR) \
static PyObject *pepy_##OBJ##_get_##ATTR(PyObject *self, void *closure) { \
Py_INCREF(((pepy_##OBJ *) self)->ATTR); \
return ((pepy_##OBJ *) self)->ATTR; \
}
#define OBJECTGETTER(OBJ, ATTR, DOC) \
{ (char *) #ATTR, (getter) pepy_##OBJ##_get_##ATTR, \
(setter) pepy_attr_not_writable, \
(char *) #DOC, NULL }
static PyObject *pepy_error; static PyObject *pepy_error;
typedef struct { typedef struct {
@ -95,25 +107,14 @@ static void pepy_import_dealloc(pepy_import *self) {
self->ob_type->tp_free((PyObject *) self); self->ob_type->tp_free((PyObject *) self);
} }
#define PEPY_IMPORT_GET(ATTR) \ PEPY_OBJECT_GET(import, name)
static PyObject *pepy_import_get_##ATTR(PyObject *self, void *closure) { \ PEPY_OBJECT_GET(import, sym)
Py_INCREF(((pepy_import *) self)->ATTR); \ PEPY_OBJECT_GET(import, addr)
return ((pepy_import *) self)->ATTR; \
}
PEPY_IMPORT_GET(name)
PEPY_IMPORT_GET(sym)
PEPY_IMPORT_GET(addr)
#define MAKEIMPORTGETSET(GS, DOC) \
{ (char *) #GS, (getter) pepy_import_get_##GS, \
(setter) pepy_attr_not_writable, \
(char *) #DOC, NULL }
static PyGetSetDef pepy_import_getseters[] = { static PyGetSetDef pepy_import_getseters[] = {
MAKEIMPORTGETSET(name, "Name"), OBJECTGETTER(import, name, "Name"),
MAKEIMPORTGETSET(sym, "Symbol"), OBJECTGETTER(import, sym, "Symbol"),
MAKEIMPORTGETSET(addr, "Address"), OBJECTGETTER(import, addr, "Address"),
{ NULL } { NULL }
}; };
@ -180,25 +181,14 @@ static void pepy_export_dealloc(pepy_export *self) {
self->ob_type->tp_free((PyObject *) self); self->ob_type->tp_free((PyObject *) self);
} }
#define PEPY_EXPORT_GET(ATTR) \ PEPY_OBJECT_GET(export, mod)
static PyObject *pepy_export_get_##ATTR(PyObject *self, void *closure) { \ PEPY_OBJECT_GET(export, func)
Py_INCREF(((pepy_export *) self)->ATTR); \ PEPY_OBJECT_GET(export, addr)
return ((pepy_export *) self)->ATTR; \
}
PEPY_EXPORT_GET(mod)
PEPY_EXPORT_GET(func)
PEPY_EXPORT_GET(addr)
#define MAKEEXPORTGETSET(GS, DOC) \
{ (char *) #GS, (getter) pepy_export_get_##GS, \
(setter) pepy_attr_not_writable, \
(char *) #DOC, NULL }
static PyGetSetDef pepy_export_getseters[] = { static PyGetSetDef pepy_export_getseters[] = {
MAKEEXPORTGETSET(mod, "Module"), OBJECTGETTER(export, mod, "Module"),
MAKEEXPORTGETSET(func, "Function"), OBJECTGETTER(export, func, "Function"),
MAKEEXPORTGETSET(addr, "Address"), OBJECTGETTER(export, addr, "Address"),
{ NULL } { NULL }
}; };
@ -270,35 +260,24 @@ static void pepy_section_dealloc(pepy_section *self) {
self->ob_type->tp_free((PyObject *) self); self->ob_type->tp_free((PyObject *) self);
} }
#define PEPY_SECTION_GET(ATTR) \ PEPY_OBJECT_GET(section, name)
static PyObject *pepy_section_get_##ATTR(PyObject *self, void *closure) { \ PEPY_OBJECT_GET(section, base)
Py_INCREF(((pepy_section *) self)->ATTR); \ PEPY_OBJECT_GET(section, length)
return ((pepy_section *) self)->ATTR; \ PEPY_OBJECT_GET(section, virtaddr)
} PEPY_OBJECT_GET(section, virtsize)
PEPY_OBJECT_GET(section, numrelocs)
PEPY_SECTION_GET(name) PEPY_OBJECT_GET(section, numlinenums)
PEPY_SECTION_GET(base) PEPY_OBJECT_GET(section, characteristics)
PEPY_SECTION_GET(length)
PEPY_SECTION_GET(virtaddr)
PEPY_SECTION_GET(virtsize)
PEPY_SECTION_GET(numrelocs)
PEPY_SECTION_GET(numlinenums)
PEPY_SECTION_GET(characteristics)
#define MAKESECTIONGETSET(GS, DOC) \
{ (char *) #GS, (getter) pepy_section_get_##GS, \
(setter) pepy_attr_not_writable, \
(char *) #DOC, NULL }
static PyGetSetDef pepy_section_getseters[] = { static PyGetSetDef pepy_section_getseters[] = {
MAKESECTIONGETSET(name, "Name"), OBJECTGETTER(section, name, "Name"),
MAKESECTIONGETSET(base, "Base address"), OBJECTGETTER(section, base, "Base address"),
MAKESECTIONGETSET(length, "Length"), OBJECTGETTER(section, length, "Length"),
MAKESECTIONGETSET(virtaddr, "Virtual address"), OBJECTGETTER(section, virtaddr, "Virtual address"),
MAKESECTIONGETSET(virtsize, "Virtual size"), OBJECTGETTER(section, virtsize, "Virtual size"),
MAKESECTIONGETSET(numrelocs, "Number of relocations"), OBJECTGETTER(section, numrelocs, "Number of relocations"),
MAKESECTIONGETSET(numlinenums, "Number of line numbers"), OBJECTGETTER(section, numlinenums, "Number of line numbers"),
MAKESECTIONGETSET(characteristics, "Characteristics"), OBJECTGETTER(section, characteristics, "Characteristics"),
{ NULL } { NULL }
}; };
@ -578,18 +557,13 @@ PEPY_PARSED_GET(timedatestamp, nt.FileHeader.TimeDateStamp)
PEPY_PARSED_GET(numberofsymbols, nt.FileHeader.NumberOfSymbols) PEPY_PARSED_GET(numberofsymbols, nt.FileHeader.NumberOfSymbols)
PEPY_PARSED_GET(characteristics, nt.FileHeader.Characteristics) PEPY_PARSED_GET(characteristics, nt.FileHeader.Characteristics)
#define MAKEPARSEDGETSET(GS, DOC) \
{ (char *) #GS, (getter) pepy_parsed_get_##GS, \
(setter) pepy_attr_not_writable, \
(char *) #DOC, NULL }
static PyGetSetDef pepy_parsed_getseters[] = { static PyGetSetDef pepy_parsed_getseters[] = {
MAKEPARSEDGETSET(signature, "PE Signature"), OBJECTGETTER(parsed, signature, "PE Signature"),
MAKEPARSEDGETSET(machine, "Machine"), OBJECTGETTER(parsed, machine, "Machine"),
MAKEPARSEDGETSET(numberofsections, "Number of sections"), OBJECTGETTER(parsed, numberofsections, "Number of sections"),
MAKEPARSEDGETSET(timedatestamp, "Timedate stamp"), OBJECTGETTER(parsed, timedatestamp, "Timedate stamp"),
MAKEPARSEDGETSET(numberofsymbols, "Number of symbols"), OBJECTGETTER(parsed, numberofsymbols, "Number of symbols"),
MAKEPARSEDGETSET(characteristics, "Characteristics"), OBJECTGETTER(parsed, characteristics, "Characteristics"),
{ NULL } { NULL }
}; };