diff options
Diffstat (limited to 'scripts/Python/python-typemaps.swig')
| -rw-r--r-- | scripts/Python/python-typemaps.swig | 225 |
1 files changed, 106 insertions, 119 deletions
diff --git a/scripts/Python/python-typemaps.swig b/scripts/Python/python-typemaps.swig index 9c4e5cecb363..df16a6d27b3b 100644 --- a/scripts/Python/python-typemaps.swig +++ b/scripts/Python/python-typemaps.swig @@ -1,20 +1,22 @@ /* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */ %typemap(in) char ** { + using namespace lldb_private; /* Check if is a list */ - if (PyList_Check($input)) { - int size = PyList_Size($input); + if (PythonList::Check($input)) { + PythonList list(PyRefType::Borrowed, $input); + int size = list.GetSize(); int i = 0; - $1 = (char **) malloc((size+1) * sizeof(char*)); + $1 = (char**)malloc((size+1)*sizeof(char*)); for (i = 0; i < size; i++) { - PyObject *o = PyList_GetItem($input,i); - if (PyString_Check(o)) - $1[i] = PyString_AsString(o); - else { + PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>(); + if (!py_str.IsAllocated()) { PyErr_SetString(PyExc_TypeError,"list must contain strings"); free($1); - return NULL; + return nullptr; } + + $1[i] = const_cast<char*>(py_str.GetString().data()); } $1[i] = 0; } else if ($input == Py_None) { @@ -25,29 +27,17 @@ } } -%typemap(in) lldb::tid_t { - using namespace lldb_private; - if (PythonInteger::Check($input)) - { - PythonInteger py_int(PyRefType::Borrowed, $input); - $1 = static_cast<lldb::tid_t>(py_int.GetInteger()); - } - else - { - PyErr_SetString(PyExc_ValueError, "Expecting an integer"); - return nullptr; - } -} - %typemap(typecheck) char ** { /* Check if is a list */ $1 = 1; - if (PyList_Check($input)) { - int size = PyList_Size($input); + using namespace lldb_private; + if (PythonList::Check($input)) { + PythonList list(PyRefType::Borrowed, $input); + int size = list.GetSize(); int i = 0; for (i = 0; i < size; i++) { - PyObject *o = PyList_GetItem($input,i); - if (!PyString_Check(o)) { $1 = 0; } + PythonString s = list.GetItemAtIndex(i).AsType<PythonString>(); + if (!s.IsAllocated()) { $1 = 0; } } } else @@ -72,63 +62,18 @@ $result = list.release(); } -%typemap(in) char const ** { - /* Check if is a list */ - using namespace lldb_private; - if (PythonList::Check($input)) { - PythonList py_list(PyRefType::Borrowed, $input); - int size = py_list.GetSize(); - $1 = (char**)malloc((size+1)*sizeof(char*)); - for (int i = 0; i < size; i++) { - PythonObject o = py_list.GetItemAtIndex(i); - if (!PythonString::Check(o.get())) { - PyErr_SetString(PyExc_TypeError,"list must contain strings"); - free($1); - return nullptr; - } - auto py_str = o.AsType<PythonString>(); - $1[i] = const_cast<char*>(py_str.GetString().data()); - } - - $1[size] = 0; - } else if ($input == Py_None) { - $1 = nullptr; - } else { - PyErr_SetString(PyExc_TypeError,"not a list"); - return nullptr; - } -} - -%typemap(typecheck) char const ** { - /* Check if is a list */ - $1 = 1; - if (PyList_Check($input)) { - int size = PyList_Size($input); - int i = 0; - for (i = 0; i < size; i++) { - PyObject *o = PyList_GetItem($input,i); - if (!PyString_Check(o)) { $1 = 0; } - } +%typemap(in) lldb::tid_t { + using namespace lldb_private; + if (PythonInteger::Check($input)) + { + PythonInteger py_int(PyRefType::Borrowed, $input); + $1 = static_cast<lldb::tid_t>(py_int.GetInteger()); } else { - $1 = ( ($input == Py_None) ? 1 : 0); - } -} - -%typemap(freearg) char const ** { - free((char *) $1); -} - -%typemap(out) char const ** { - int len; - int i; - len = 0; - while ($1[len]) len++; - $result = PyList_New(len); - for (i = 0; i < len; i++) { - PyList_SetItem($result, i, PyString_FromString($1[i])); + PyErr_SetString(PyExc_ValueError, "Expecting an integer"); + return nullptr; } } @@ -148,6 +93,9 @@ } $1 = (char *) malloc($2); } +// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated +// as char data instead of byte data. +%typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len); // Return the char buffer. Discarding any previous return result // See also SBThread::GetStopDescription. @@ -163,18 +111,29 @@ } free($1); } +// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated +// as char data instead of byte data. +%typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len); // typemap for an outgoing buffer // See also SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len). %typemap(in) (const char *cstr, uint32_t cstr_len) { - if (PyString_Check($input)) { - $1 = (char *) PyString_AsString($input); - $2 = PyString_Size($input); + using namespace lldb_private; + if (PythonString::Check($input)) { + PythonString str(PyRefType::Borrowed, $input); + $1 = (char*)str.GetString().data(); + $2 = str.GetSize(); + } + else if(PythonByteArray::Check($input)) { + PythonByteArray bytearray(PyRefType::Borrowed, $input); + $1 = (char*)bytearray.GetBytes().data(); + $2 = bytearray.GetSize(); } - else if(PyByteArray_Check($input)) { - $1 = (char *) PyByteArray_AsString($input); - $2 = PyByteArray_Size($input); + else if (PythonBytes::Check($input)) { + PythonBytes bytes(PyRefType::Borrowed, $input); + $1 = (char*)bytes.GetBytes().data(); + $2 = bytes.GetSize(); } else { PyErr_SetString(PyExc_ValueError, "Expecting a string"); @@ -183,13 +142,21 @@ } // Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len). %typemap(in) (const char *src, size_t src_len) { - if (PyString_Check($input)) { - $1 = (char *) PyString_AsString($input); - $2 = PyString_Size($input); + using namespace lldb_private; + if (PythonString::Check($input)) { + PythonString str(PyRefType::Borrowed, $input); + $1 = (char*)str.GetString().data(); + $2 = str.GetSize(); } - else if(PyByteArray_Check($input)) { - $1 = (char *) PyByteArray_AsString($input); - $2 = PyByteArray_Size($input); + else if(PythonByteArray::Check($input)) { + PythonByteArray bytearray(PyRefType::Borrowed, $input); + $1 = (char*)bytearray.GetBytes().data(); + $2 = bytearray.GetSize(); + } + else if (PythonBytes::Check($input)) { + PythonBytes bytes(PyRefType::Borrowed, $input); + $1 = (char*)bytes.GetBytes().data(); + $2 = bytes.GetSize(); } else { PyErr_SetString(PyExc_ValueError, "Expecting a string"); @@ -198,32 +165,48 @@ } // And SBProcess::WriteMemory. %typemap(in) (const void *buf, size_t size) { - if (PyString_Check($input)) { - $1 = (void *) PyString_AsString($input); - $2 = PyString_Size($input); + using namespace lldb_private; + if (PythonString::Check($input)) { + PythonString str(PyRefType::Borrowed, $input); + $1 = (void*)str.GetString().data(); + $2 = str.GetSize(); } - else if(PyByteArray_Check($input)) { - $1 = (void *) PyByteArray_AsString($input); - $2 = PyByteArray_Size($input); + else if(PythonByteArray::Check($input)) { + PythonByteArray bytearray(PyRefType::Borrowed, $input); + $1 = (void*)bytearray.GetBytes().data(); + $2 = bytearray.GetSize(); + } + else if (PythonBytes::Check($input)) { + PythonBytes bytes(PyRefType::Borrowed, $input); + $1 = (void*)bytes.GetBytes().data(); + $2 = bytes.GetSize(); } else { - PyErr_SetString(PyExc_ValueError, "Expecting a string"); + PyErr_SetString(PyExc_ValueError, "Expecting a buffer"); return NULL; } } // For SBDebugger::DispatchInput %typemap(in) (const void *data, size_t data_len) { - if (PyString_Check($input)) { - $1 = static_cast<void *>(PyString_AsString($input)); - $2 = PyString_Size($input); + using namespace lldb_private; + if (PythonString::Check($input)) { + PythonString str(PyRefType::Borrowed, $input); + $1 = (void*)str.GetString().data(); + $2 = str.GetSize(); } - else if(PyByteArray_Check($input)) { - $1 = static_cast<void *>(PyByteArray_AsString($input)); - $2 = PyByteArray_Size($input); + else if(PythonByteArray::Check($input)) { + PythonByteArray bytearray(PyRefType::Borrowed, $input); + $1 = (void*)bytearray.GetBytes().data(); + $2 = bytearray.GetSize(); + } + else if (PythonBytes::Check($input)) { + PythonBytes bytes(PyRefType::Borrowed, $input); + $1 = (void*)bytes.GetBytes().data(); + $2 = bytes.GetSize(); } else { - PyErr_SetString(PyExc_ValueError, "Expecting a string or byte array"); + PyErr_SetString(PyExc_ValueError, "Expecting a buffer"); return NULL; } } @@ -254,9 +237,8 @@ $result = Py_None; Py_INCREF($result); } else { - llvm::StringRef ref(static_cast<const char*>($1), result); - lldb_private::PythonString string(ref); - $result = string.release(); + lldb_private::PythonBytes bytes(static_cast<const uint8_t*>($1), result); + $result = bytes.release(); } free($1); } @@ -545,22 +527,27 @@ return nullptr; $1 = file.GetStream(); - } + if ($1) + file.Clear(); + } } %typemap(out) FILE * { char mode[4] = {0}; -#ifdef __MACOSX__ +%#ifdef __APPLE__ int i = 0; - short flags = $1->_flags; - - if (flags & __SRD) - mode[i++] = 'r'; - else if (flags & __SWR) - mode[i++] = 'w'; - else // if (flags & __SRW) - mode[i++] = 'a'; -#endif + if ($1) + { + short flags = $1->_flags; + + if (flags & __SRD) + mode[i++] = 'r'; + else if (flags & __SWR) + mode[i++] = 'w'; + else // if (flags & __SRW) + mode[i++] = 'a'; + } +%#endif using namespace lldb_private; File file($1, false); PythonFile py_file(file, mode); |
