From 48f62edc3974d8b5efdc76be2054486c80982f20 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Mon, 30 Aug 2021 21:39:07 -0600 Subject: [PATCH] factored out some wrapping code to avoid name collisions --- src/ep_resources.c | 91 ++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/src/ep_resources.c b/src/ep_resources.c index 903c70e..a84edb4 100644 --- a/src/ep_resources.c +++ b/src/ep_resources.c @@ -27,12 +27,34 @@ #include "ep_init.h" #include "ep_types.h" +static PyObject *wrap_resource_file_handle(HRESFILE handle) +{ + PyObject *rc = NULL, *args, *kwargs; + ResFileObject *presfile; + + args = PyTuple_New(0); + if (args) + { + kwargs = PyDict_New(); + if (kwargs) + { + rc = PyType_GenericNew(&ResFileType, args, kwargs); + if (rc) + { + presfile = (ResFileObject *)rc; + presfile->hresfile = handle; + } + Py_DECREF(kwargs); + } + Py_DECREF(args); + } + return rc; +} static PyObject *resfile_load_classmethod(ResFileType *cls, PyObject *args) { const char *filename; - PyObject *rc = NULL, *args, *kwargs; - ResFileObject *presfile; + PyObject *rc = NULL; HRESULT hr; HRESFILE handle; @@ -41,23 +63,7 @@ static PyObject *resfile_load_classmethod(ResFileType *cls, PyObject *args) hr = Rsrc_load_file((PCSTR)filename, &handle); if (SUCCEEDED(hr)) { - args = PyTuple_New(0); - if (args) - { - kwargs = PyDict_New(); - if (kwargs) - { - rc = PyType_GenericNew(&ResFileType, args, kwargs); - if (rc) - { - presfile = (ResFileObject *)rc; - presfile->hresfile = handle; - } - Py_DECREF(kwargs); - } - Py_DECREF(args); - } - + rc = wrap_resource_file_handle(handle); if (!rc) { Rsrc_close_file(handle); @@ -77,11 +83,34 @@ static PyObject *resfile_close(ResFileObject *self, PyObject *args) return NULL; } +static PyObject *wrap_resource_handle(HRSRC hrsrc) +{ + PyObject *rc = NULL, *args, *kwargs; + ResourceObject *pres; + + args = PyTuple_New(0); + if (args) + { + kwargs = PyDict_New(); + if (kwargs) + { + rc = PyType_GenericNew(&ResourceType, args, kwargs); + if (rc) + { + pres = (ResourceObject *)rc; + pres->hrsrc = hrsrc; + } + Py_DECREF(kwargs); + } + Py_DECREF(args); + } + return rc; +} + static PyObject *resfile_find_resource(ResFileObject *self, PyObject *args) { const char *name; - PyObject *rc = NULL, *args, *kwargs; - ResourceObject *pres; + PyObject *rc = NULL; HRSRC hrsrc; HRESULT hr; @@ -90,23 +119,7 @@ static PyObject *resfile_find_resource(ResFileObject *self, PyObject *args) hr = Rsrc_find_resource(self->hresfile, (PCSTR)name, NULL, &hrsrc); if (SUCCEEDED(hr)) { - args = PyTuple_New(0); - if (args) - { - kwargs = PyDict_New(); - if (kwargs) - { - rc = PyType_GenericNew(&ResourceType, args, kwargs); - if (rc) - { - pres = (ResourceObject *)rc; - pres->hrsrc = hrsrc; - } - Py_DECREF(kwargs); - } - Py_DECREF(args); - } - + rc = wrap_resource_handle(hrsrc); if (!rc) { Rsrc_free_resource(hrsrc); @@ -114,7 +127,7 @@ static PyObject *resfile_find_resource(ResFileObject *self, PyObject *args) } } else - PyErr_Format(PyExc_RuntimeError, "unable to load resource file '%s' (%08x)", filename, hr); + PyErr_Format(PyExc_RuntimeError, "unable to load resource ''%s' (%08x)", name, hr); return rc; }