88#include " ttconv/pprdrv.h"
99#include < vector>
1010
11+ class PythonExceptionOccurred {
12+
13+ };
14+
1115/* *
1216 * An implementation of TTStreamWriter that writes to a Python
1317 * file-like object.
@@ -35,33 +39,40 @@ class PythonFileWriter : public TTStreamWriter {
3539
3640 virtual void write (const char * a) {
3741 if (_write_method)
38- PyObject_CallFunction (_write_method, " s" , a);
42+ if (! PyObject_CallFunction (_write_method, " s" , a))
43+ throw PythonExceptionOccurred ();
3944 }
4045};
4146
4247int fileobject_to_PythonFileWriter (PyObject* object, void * address) {
4348 PythonFileWriter* file_writer = (PythonFileWriter*)address;
49+
4450 PyObject* write_method = PyObject_GetAttrString (object, " write" );
4551 if (write_method == NULL || ! PyCallable_Check (write_method)) {
4652 PyErr_SetString (PyExc_TypeError, " Expected a file-like object with a write method." );
4753 return 0 ;
4854 }
55+
4956 file_writer->set (write_method);
57+
5058 return 1 ;
5159}
5260
5361int pyiterable_to_vector_int (PyObject* object, void * address) {
5462 std::vector<int >* result = (std::vector<int >*)address;
63+
5564 PyObject* iterator = PyObject_GetIter (object);
5665 if (! iterator)
5766 return 0 ;
67+
5868 PyObject* item;
59- while (item = PyIter_Next (iterator)) {
69+ while ( ( item = PyIter_Next (iterator)) ) {
6070 long value = PyInt_AsLong (item);
6171 if (value == -1 && PyErr_Occurred ())
6272 return 0 ;
6373 result->push_back (value);
6474 }
75+
6576 return 1 ;
6677}
6778
@@ -96,6 +107,8 @@ convert_ttf_to_ps(PyObject* self, PyObject* args, PyObject* kwds) {
96107 } catch (TTException& e) {
97108 PyErr_SetString (PyExc_RuntimeError, e.getMessage ());
98109 return NULL ;
110+ } catch (PythonExceptionOccurred& e) {
111+ return NULL ;
99112 } catch (...) {
100113 PyErr_SetString (PyExc_RuntimeError, " Unknown C++ exception" );
101114 return NULL ;
@@ -116,7 +129,8 @@ class PythonDictionaryCallback : public TTDictionaryCallback {
116129 virtual void add_pair (const char * a, const char * b) {
117130 PyObject* value = PyString_FromString (b);
118131 if (value)
119- PyDict_SetItemString (_dict, a, value);
132+ if (PyDict_SetItemString (_dict, a, value))
133+ throw PythonExceptionOccurred ();
120134 }
121135};
122136
@@ -146,6 +160,8 @@ py_get_pdf_charprocs(PyObject* self, PyObject* args, PyObject* kwds) {
146160 } catch (TTException& e) {
147161 PyErr_SetString (PyExc_RuntimeError, e.getMessage ());
148162 return NULL ;
163+ } catch (PythonExceptionOccurred& e) {
164+ return NULL ;
149165 } catch (...) {
150166 PyErr_SetString (PyExc_RuntimeError, " Unknown C++ exception" );
151167 return NULL ;
0 commit comments