From 76e3d41c4ca8e0490184460eb848db9cde534249 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Mon, 28 Sep 2020 15:36:00 -0400 Subject: [PATCH] pepy: Remove old Python 2 cruft (#140) --- pepy/pepy.cpp | 115 ++++++++++++++++++-------------------------------- 1 file changed, 40 insertions(+), 75 deletions(-) diff --git a/pepy/pepy.cpp b/pepy/pepy.cpp index 4f42a25..c22a62e 100644 --- a/pepy/pepy.cpp +++ b/pepy/pepy.cpp @@ -35,25 +35,6 @@ using namespace peparse; */ #define PEPY_VERSION PEPARSE_VERSION -/* - * Add some definition for compatibility between python2 and python3 - */ -#if PY_MAJOR_VERSION >= 3 -#define PyString_FromString PyUnicode_FromString -#endif - -/* - * Some macro only available after python 2.6 - * Needed for compatibility with python3 - */ -#ifndef PyVarObject_HEAD_INIT -#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, -#endif - -#ifndef Py_TYPE -#define Py_TYPE(_ob_) (((PyObject *) (_ob_))->ob_type) -#endif - /* These are used to across multiple objects. */ #define PEPY_OBJECT_GET(OBJ, ATTR) \ static PyObject *pepy_##OBJ##_get_##ATTR(PyObject *self, void *closure) { \ @@ -156,9 +137,9 @@ static void pepy_import_dealloc(pepy_import *self) { Py_TYPE(self)->tp_free((PyObject *) self); } -PEPY_OBJECT_GET(import, name) -PEPY_OBJECT_GET(import, sym) -PEPY_OBJECT_GET(import, addr) +PEPY_OBJECT_GET(import, name); +PEPY_OBJECT_GET(import, sym); +PEPY_OBJECT_GET(import, addr); static PyGetSetDef pepy_import_getseters[] = { OBJECTGETTER(import, name, "Name"), @@ -230,9 +211,9 @@ static void pepy_export_dealloc(pepy_export *self) { Py_TYPE(self)->tp_free((PyObject *) self); } -PEPY_OBJECT_GET(export, mod) -PEPY_OBJECT_GET(export, func) -PEPY_OBJECT_GET(export, addr) +PEPY_OBJECT_GET(export, mod); +PEPY_OBJECT_GET(export, func); +PEPY_OBJECT_GET(export, addr); static PyGetSetDef pepy_export_getseters[] = { OBJECTGETTER(export, mod, "Module"), @@ -304,8 +285,8 @@ static void pepy_relocation_dealloc(pepy_relocation *self) { Py_TYPE(self)->tp_free((PyObject *) self); } -PEPY_OBJECT_GET(relocation, type) -PEPY_OBJECT_GET(relocation, addr) +PEPY_OBJECT_GET(relocation, type); +PEPY_OBJECT_GET(relocation, addr); static PyGetSetDef pepy_relocation_getseters[] = { OBJECTGETTER(relocation, type, "Type"), @@ -392,15 +373,15 @@ static void pepy_section_dealloc(pepy_section *self) { Py_TYPE(self)->tp_free((PyObject *) self); } -PEPY_OBJECT_GET(section, name) -PEPY_OBJECT_GET(section, base) -PEPY_OBJECT_GET(section, length) -PEPY_OBJECT_GET(section, virtaddr) -PEPY_OBJECT_GET(section, virtsize) -PEPY_OBJECT_GET(section, numrelocs) -PEPY_OBJECT_GET(section, numlinenums) -PEPY_OBJECT_GET(section, characteristics) -PEPY_OBJECT_GET(section, data) +PEPY_OBJECT_GET(section, name); +PEPY_OBJECT_GET(section, base); +PEPY_OBJECT_GET(section, length); +PEPY_OBJECT_GET(section, virtaddr); +PEPY_OBJECT_GET(section, virtsize); +PEPY_OBJECT_GET(section, numrelocs); +PEPY_OBJECT_GET(section, numlinenums); +PEPY_OBJECT_GET(section, characteristics); +PEPY_OBJECT_GET(section, data); static PyGetSetDef pepy_section_getseters[] = { OBJECTGETTER(section, name, "Name"), @@ -497,16 +478,16 @@ static void pepy_resource_dealloc(pepy_resource *self) { Py_TYPE(self)->tp_free((PyObject *) self); } -PEPY_OBJECT_GET(resource, type_str) -PEPY_OBJECT_GET(resource, name_str) -PEPY_OBJECT_GET(resource, lang_str) -PEPY_OBJECT_GET(resource, type) -PEPY_OBJECT_GET(resource, name) -PEPY_OBJECT_GET(resource, lang) -PEPY_OBJECT_GET(resource, codepage) -PEPY_OBJECT_GET(resource, RVA) -PEPY_OBJECT_GET(resource, size) -PEPY_OBJECT_GET(resource, data) +PEPY_OBJECT_GET(resource, type_str); +PEPY_OBJECT_GET(resource, name_str); +PEPY_OBJECT_GET(resource, lang_str); +PEPY_OBJECT_GET(resource, type); +PEPY_OBJECT_GET(resource, name); +PEPY_OBJECT_GET(resource, lang); +PEPY_OBJECT_GET(resource, codepage); +PEPY_OBJECT_GET(resource, RVA); +PEPY_OBJECT_GET(resource, size); +PEPY_OBJECT_GET(resource, data); static PyObject *pepy_resource_type_as_str(PyObject *self, PyObject *args) { PyObject *ret; @@ -589,7 +570,7 @@ static PyObject *pepy_resource_type_as_str(PyObject *self, PyObject *args) { break; } - ret = PyString_FromString(str); + ret = PyUnicode_FromString(str); if (!ret) { PyErr_SetString(pepy_error, "Unable to create return string."); return NULL; @@ -715,7 +696,7 @@ static PyObject *pepy_parsed_get_machine_as_str(PyObject *self, if (!str) Py_RETURN_NONE; - ret = PyString_FromString(str); + ret = PyUnicode_FromString(str); if (!ret) { PyErr_SetString(pepy_error, "Unable to create return string."); return NULL; @@ -733,7 +714,7 @@ static PyObject *pepy_parsed_get_subsystem_as_str(PyObject *self, if (!str) Py_RETURN_NONE; - ret = PyString_FromString(str); + ret = PyUnicode_FromString(str); if (!ret) { PyErr_SetString(pepy_error, "Unable to create return string."); return NULL; @@ -1093,13 +1074,13 @@ static PyObject *pepy_parsed_get_relocations(PyObject *self, PyObject *args) { return ret; \ } -PEPY_PARSED_GET(signature, Signature) -PEPY_PARSED_GET(machine, FileHeader.Machine) -PEPY_PARSED_GET(numberofsections, FileHeader.NumberOfSections) -PEPY_PARSED_GET(timedatestamp, FileHeader.TimeDateStamp) -PEPY_PARSED_GET(numberofsymbols, FileHeader.NumberOfSymbols) -PEPY_PARSED_GET(characteristics, FileHeader.Characteristics) -PEPY_PARSED_GET(magic, OptionalMagic) +PEPY_PARSED_GET(signature, Signature); +PEPY_PARSED_GET(machine, FileHeader.Machine); +PEPY_PARSED_GET(numberofsections, FileHeader.NumberOfSections); +PEPY_PARSED_GET(timedatestamp, FileHeader.TimeDateStamp); +PEPY_PARSED_GET(numberofsymbols, FileHeader.NumberOfSymbols); +PEPY_PARSED_GET(characteristics, FileHeader.Characteristics); +PEPY_PARSED_GET(magic, OptionalMagic); /* * This is used to get things from the optional header, which can be either @@ -1130,8 +1111,8 @@ PEPY_PARSED_GET(magic, OptionalMagic) return ret; \ } -PEPY_PARSED_GET_OPTIONAL(majorlinkerver, MajorLinkerVersion) -PEPY_PARSED_GET_OPTIONAL(minorlinkerver, MinorLinkerVersion) +PEPY_PARSED_GET_OPTIONAL(majorlinkerver, MajorLinkerVersion); +PEPY_PARSED_GET_OPTIONAL(minorlinkerver, MinorLinkerVersion); PEPY_PARSED_GET_OPTIONAL(codesize, SizeOfCode); PEPY_PARSED_GET_OPTIONAL(initdatasize, SizeOfInitializedData); PEPY_PARSED_GET_OPTIONAL(uninitdatasize, SizeOfUninitializedData); @@ -1333,7 +1314,7 @@ static PyObject *pepy_parse(PyObject *self, PyObject *args) { static PyMethodDef pepy_methods[] = { {"parse", pepy_parse, METH_VARARGS, "Parse PE from file."}, {NULL}}; -static PyObject *pepi_module_init(void) { +PyMODINIT_FUNC PyInit_pepy(void) { PyObject *m; if (PyType_Ready(&pepy_parsed_type) < 0 || @@ -1344,7 +1325,6 @@ static PyObject *pepi_module_init(void) { PyType_Ready(&pepy_resource_type) < 0) return NULL; -#if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "pepy", @@ -1356,13 +1336,8 @@ static PyObject *pepi_module_init(void) { NULL, NULL, }; -#endif -#if PY_MAJOR_VERSION >= 3 m = PyModule_Create(&moduledef); -#else - m = Py_InitModule3("pepy", pepy_methods, "Python interface to pe-parse."); -#endif if (!m) return NULL; @@ -1454,13 +1429,3 @@ static PyObject *pepi_module_init(void) { return m; } - -#if PY_MAJOR_VERSION >= 3 -PyMODINIT_FUNC PyInit_pepy(void) { - return pepi_module_init(); -} -#else -PyMODINIT_FUNC initpepy(void) { - pepi_module_init(); -} -#endif