diff options
author | Filipe Cabecinhas <me@filcab.net> | 2012-08-22 13:25:10 +0000 |
---|---|---|
committer | Filipe Cabecinhas <me@filcab.net> | 2012-08-22 13:25:10 +0000 |
commit | 6eb31e7391d27fc4a78122d45138e51eae9d6743 (patch) | |
tree | a3bd9bffdd8b1b3583607a0c101f70f1df71e63c /lldb/scripts/Python/python-wrapper.swig | |
parent | de17f866559c49c8f8317e42cfaf1468b59922ff (diff) | |
download | bcm5719-llvm-6eb31e7391d27fc4a78122d45138e51eae9d6743.tar.gz bcm5719-llvm-6eb31e7391d27fc4a78122d45138e51eae9d6743.zip |
Added a typemap and wrappers for SBInputReader callbacks
Now it's possible to use SBInputReader callbacks in Python.
We leak the callback object, unfortunately. A __del__ method can be added
to SBInputReader, but we have no way to check the callback function that
is on the reader. So we can't call Py_DECREF on it when we have our
PythonCallback function. One way to do it is to assume that reified
SBInputReaders always have a Python callback (and always call Py_DECREF).
Another one is to add methods or properties to SBInputReader (or make the
m_callback_function property public).
llvm-svn: 162356
Diffstat (limited to 'lldb/scripts/Python/python-wrapper.swig')
-rw-r--r-- | lldb/scripts/Python/python-wrapper.swig | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lldb/scripts/Python/python-wrapper.swig b/lldb/scripts/Python/python-wrapper.swig index 73bad8d11d2..9b48f3b8b1f 100644 --- a/lldb/scripts/Python/python-wrapper.swig +++ b/lldb/scripts/Python/python-wrapper.swig @@ -823,5 +823,61 @@ LLDBSwigPythonCallModuleInit } return retval; } +%} + + +%runtime %{ +// Forward declaration to be inserted at the start of LLDBWrapPython.h +// I used runtime as a hack to make SWIG place it where it's needed. +// This is needed to use LLDBSwigPythonCallSBInputReaderCallback in the +// typemaps and in the extensions (SBInputReader.__del__()). +#include "SBInputReader.h" +size_t +LLDBSwigPythonCallSBInputReaderCallback(void *baton, + lldb::SBInputReader *reader, + lldb::InputReaderAction notification, + const char*bytes, + size_t bytes_len); +%} + +%wrapper %{ +// For the InputReader Callback functions +SWIGEXPORT size_t +LLDBSwigPythonCallSBInputReaderCallback(void *baton, + lldb::SBInputReader *reader, + lldb::InputReaderAction notification, + const char*bytes, + size_t bytes_len) { + SWIG_PYTHON_THREAD_BEGIN_BLOCK; + + PyObject *py_InputReader = SWIG_NewPointerObj(reader, SWIGTYPE_p_lldb__SBInputReader, false); + PyObject *py_Notification = PyInt_FromLong(notification); + PyObject *py_Bytes = PyBytes_FromStringAndSize(bytes, bytes_len); + + PyObject *tuple = PyTuple_Pack(3, py_InputReader, py_Notification, py_Bytes); + PyObject *res = PyObject_Call(reinterpret_cast<PyObject*>(baton), tuple, NULL); + Py_DECREF(tuple); + Py_DECREF(py_InputReader); + Py_DECREF(py_Notification); + Py_DECREF(py_Bytes); + + if (res == NULL) { + PyObject *exc = PyErr_Occurred(); + if (exc) { + ::puts("\nErroring out at LLDBSwigPythonCallSBInputReaderCallback"); + PyErr_Print(); + } + return 0; + } + + size_t result = 0; + // If the callback misbehaves and returns Py_None, assume it returned 0 + if (res != Py_None) + result = static_cast<size_t>(PyInt_AsSsize_t(res)); + + Py_DECREF(res); + SWIG_PYTHON_THREAD_END_BLOCK; + return result; +} %} |