From bca7867b866f031ddadb4f4c87c15bf56421932d Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Tue, 22 Nov 2011 17:32:47 -0500 Subject: [PATCH] pushed exception setting into the relevant function, removed redundant exceptions The following functions set exceptions automatically: PyImport_ImportModule, PyObject_GetAttrString, PyArg_ParseTuple. All the exception setting functions in c_overviewer should act the same way and set their own exceptions before returning. Notably, PyDict_GetItemString does *not* set exceptions, so it is handled specially in render_mode_find_interface. --- overviewer_core/src/iterate.c | 26 +++++++++++++------------- overviewer_core/src/rendermodes.c | 14 +++++++------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/overviewer_core/src/iterate.c b/overviewer_core/src/iterate.c index 5b62941..adc790d 100644 --- a/overviewer_core/src/iterate.c +++ b/overviewer_core/src/iterate.c @@ -45,47 +45,47 @@ PyObject *init_chunk_render(PyObject *self, PyObject *args) { textures = PyImport_ImportModule("overviewer_core.textures"); /* ensure none of these pointers are NULL */ if ((!textures)) { - return PyErr_Format(PyExc_ImportError, "Failed to import overviewer_core.textures"); + return NULL; } - + chunk_mod = PyImport_ImportModule("overviewer_core.chunk"); /* ensure none of these pointers are NULL */ if ((!chunk_mod)) { - return PyErr_Format(PyExc_ImportError, "Failed to import overviewer_core.chunk"); + return NULL; } blockmap = PyObject_GetAttrString(textures, "blockmap"); if (!blockmap) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.blockmap"); + return NULL; tmp = PyObject_GetAttrString(textures, "max_blockid"); if (!tmp) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.max_blockid"); + return NULL; max_blockid = PyInt_AsLong(tmp); Py_DECREF(tmp); tmp = PyObject_GetAttrString(textures, "max_data"); if (!tmp) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.max_blockid"); + return NULL; max_data = PyInt_AsLong(tmp); Py_DECREF(tmp); /* assemble the property table */ known_blocks = PyObject_GetAttrString(textures, "known_blocks"); if (!known_blocks) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.known_blocks"); + return NULL; transparent_blocks = PyObject_GetAttrString(textures, "transparent_blocks"); if (!transparent_blocks) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.transparent_blocks"); + return NULL; solid_blocks = PyObject_GetAttrString(textures, "solid_blocks"); if (!solid_blocks) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.solid_blocks"); + return NULL; fluid_blocks = PyObject_GetAttrString(textures, "fluid_blocks"); if (!fluid_blocks) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.fluid_blocks"); + return NULL; nospawn_blocks = PyObject_GetAttrString(textures, "nospawn_blocks"); if (!nospawn_blocks) - return PyErr_Format(PyExc_ImportError, "Failed to get textures.nospawn_blocks"); + return NULL; block_properties = calloc(max_blockid, sizeof(unsigned char)); for (i = 0; i < max_blockid; i++) { @@ -104,7 +104,7 @@ PyObject *init_chunk_render(PyObject *self, PyObject *args) { Py_DECREF(block); } - + Py_RETURN_NONE; } @@ -362,7 +362,7 @@ chunk_render(PyObject *self, PyObject *args) { PyObject *t = NULL; if (!PyArg_ParseTuple(args, "OOiiO", &state.self, &state.img, &xoff, &yoff, &state.blockdata_expanded)) - return PyErr_Format(PyExc_ValueError, "Failed to ParseTuple"); + return NULL; /* fill in important modules */ state.textures = textures; diff --git a/overviewer_core/src/rendermodes.c b/overviewer_core/src/rendermodes.c index ceaf9d4..fb523fb 100644 --- a/overviewer_core/src/rendermodes.c +++ b/overviewer_core/src/rendermodes.c @@ -106,13 +106,13 @@ render_mode_find_interface(const char *mode) { /* check for custom modes */ custom = PyDict_GetItemString(custom_render_modes, mode); if (custom == NULL) - return NULL; + return PyErr_Format(PyExc_RuntimeError, "Failed to find rendermode interface (custom not found)"); custom = PyDict_GetItemString(custom, "parent"); if (custom == NULL) - return NULL; + return PyErr_Format(PyExc_RuntimeError, "Failed to find rendermode interface (parent not found)"); custom_parent = PyString_AsString(custom); if (custom_parent == NULL) - return NULL; + return NULL; // PyString_AsString sets an exception return render_mode_find_interface(custom_parent); } @@ -124,11 +124,11 @@ RenderMode *render_mode_create(const char *mode, RenderState *state) { iface = render_mode_find_interface(mode); if (iface == NULL) - return PyErr_Format(PyExc_RuntimeError, "Failed to find rendermode interface"); + return NULL; options = render_mode_create_options(mode); if (options == NULL) - return PyErr_Format(PyExc_RuntimeError, "Failed to create rendermode options"); + return NULL; ret = calloc(1, sizeof(RenderMode)); if (ret == NULL) { @@ -150,13 +150,13 @@ RenderMode *render_mode_create(const char *mode, RenderState *state) { Py_DECREF(options); free(ret->mode); free(ret); - return PyErr_Format(PyExc_RuntimeError, "Failed to start rendermode interface"); + return NULL; } Py_DECREF(options); return ret; } - + void render_mode_destroy(RenderMode *self) { self->iface->finish(self->mode, self->state); free(self->mode);