From: <md...@us...> - 2007-08-02 18:49:21
|
Revision: 3665 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3665&view=rev Author: mdboom Date: 2007-08-02 11:49:17 -0700 (Thu, 02 Aug 2007) Log Message: ----------- Improved exception handling. Remove compiler warning. Modified Paths: -------------- trunk/matplotlib/src/_ttconv.cpp Modified: trunk/matplotlib/src/_ttconv.cpp =================================================================== --- trunk/matplotlib/src/_ttconv.cpp 2007-08-02 18:37:32 UTC (rev 3664) +++ trunk/matplotlib/src/_ttconv.cpp 2007-08-02 18:49:17 UTC (rev 3665) @@ -8,6 +8,10 @@ #include "ttconv/pprdrv.h" #include <vector> +class PythonExceptionOccurred { + +}; + /** * An implementation of TTStreamWriter that writes to a Python * file-like object. @@ -35,33 +39,40 @@ virtual void write(const char* a) { if (_write_method) - PyObject_CallFunction(_write_method, "s", a); + if (! PyObject_CallFunction(_write_method, "s", a)) + throw PythonExceptionOccurred(); } }; int fileobject_to_PythonFileWriter(PyObject* object, void* address) { PythonFileWriter* file_writer = (PythonFileWriter*)address; + PyObject* write_method = PyObject_GetAttrString(object, "write"); if (write_method == NULL || ! PyCallable_Check(write_method)) { PyErr_SetString(PyExc_TypeError, "Expected a file-like object with a write method."); return 0; } + file_writer->set(write_method); + return 1; } int pyiterable_to_vector_int(PyObject* object, void* address) { std::vector<int>* result = (std::vector<int>*)address; + PyObject* iterator = PyObject_GetIter(object); if (! iterator) return 0; + PyObject* item; - while (item = PyIter_Next(iterator)) { + while ( (item = PyIter_Next(iterator)) ) { long value = PyInt_AsLong(item); if (value == -1 && PyErr_Occurred()) return 0; result->push_back(value); } + return 1; } @@ -96,6 +107,8 @@ } catch (TTException& e) { PyErr_SetString(PyExc_RuntimeError, e.getMessage()); return NULL; + } catch (PythonExceptionOccurred& e) { + return NULL; } catch (...) { PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception"); return NULL; @@ -116,7 +129,8 @@ virtual void add_pair(const char* a, const char* b) { PyObject* value = PyString_FromString(b); if (value) - PyDict_SetItemString(_dict, a, value); + if (PyDict_SetItemString(_dict, a, value)) + throw PythonExceptionOccurred(); } }; @@ -146,6 +160,8 @@ } catch (TTException& e) { PyErr_SetString(PyExc_RuntimeError, e.getMessage()); return NULL; + } catch (PythonExceptionOccurred& e) { + return NULL; } catch (...) { PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception"); return NULL; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |