1/* Typemap definitions, to allow SWIG to properly handle 'char**' data types. 2 3NOTE: If there's logic to free memory in a %typemap(freearg), it will also be 4run if you call SWIG_fail on an error path. Don't manually free() an argument 5AND call SWIG_fail at the same time, because it will result in a double free. 6 7*/ 8 9%inline %{ 10 11#include "../bindings/python/python-typemaps.h" 12 13%} 14 15%typemap(in) char ** { 16 /* Check if is a list */ 17 if (PythonList::Check($input)) { 18 PythonList list(PyRefType::Borrowed, $input); 19 int size = list.GetSize(); 20 int i = 0; 21 $1 = (char **)malloc((size + 1) * sizeof(char *)); 22 for (i = 0; i < size; i++) { 23 PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>(); 24 if (!py_str.IsAllocated()) { 25 PyErr_SetString(PyExc_TypeError, "list must contain strings"); 26 SWIG_fail; 27 } 28 29 $1[i] = const_cast<char *>(py_str.GetString().data()); 30 } 31 $1[i] = 0; 32 } else if ($input == Py_None) { 33 $1 = NULL; 34 } else { 35 PyErr_SetString(PyExc_TypeError, "not a list"); 36 SWIG_fail; 37 } 38} 39 40%typemap(typecheck) char ** { 41 /* Check if is a list */ 42 $1 = 1; 43 if (PythonList::Check($input)) { 44 PythonList list(PyRefType::Borrowed, $input); 45 int size = list.GetSize(); 46 int i = 0; 47 for (i = 0; i < size; i++) { 48 PythonString s = list.GetItemAtIndex(i).AsType<PythonString>(); 49 if (!s.IsAllocated()) { 50 $1 = 0; 51 } 52 } 53 } else { 54 $1 = (($input == Py_None) ? 1 : 0); 55 } 56} 57 58%typemap(freearg) char** { 59 free((char *) $1); 60} 61 62%typecheck(SWIG_TYPECHECK_POINTER) lldb::ScriptObjectPtr { 63 PythonObject obj(PyRefType::Borrowed, $input); 64 if (!obj.IsValid()) { 65 PyErr_Clear(); 66 $1 = 0; 67 } else { 68 $1 = 1; 69 } 70} 71 72%typemap(in) lldb::ScriptObjectPtr { 73 if ($input == Py_None) { 74 $1 = nullptr; 75 } else { 76 PythonObject obj(PyRefType::Borrowed, $input); 77 if (!obj.IsValid()) { 78 PyErr_SetString(PyExc_TypeError, "Script object is not valid"); 79 SWIG_fail; 80 } 81 82 auto lldb_module = PythonModule::Import("lldb"); 83 if (!lldb_module) { 84 std::string err_msg = llvm::toString(lldb_module.takeError()); 85 PyErr_SetString(PyExc_TypeError, err_msg.c_str()); 86 SWIG_fail; 87 } 88 89 auto sb_structured_data_class = lldb_module.get().Get("SBStructuredData"); 90 if (!sb_structured_data_class) { 91 std::string err_msg = llvm::toString(sb_structured_data_class.takeError()); 92 PyErr_SetString(PyExc_TypeError, err_msg.c_str()); 93 SWIG_fail; 94 } 95 96 if (obj.IsInstance(sb_structured_data_class.get())) { 97 $1 = obj.get(); 98 } else { 99 auto type = obj.GetType(); 100 if (!type) { 101 std::string err_msg = llvm::toString(type.takeError()); 102 PyErr_SetString(PyExc_TypeError, err_msg.c_str()); 103 SWIG_fail; 104 } 105 106 auto type_name = As<std::string>(type.get().GetAttribute("__name__")); 107 if (!type_name) { 108 std::string err_msg = llvm::toString(type_name.takeError()); 109 PyErr_SetString(PyExc_TypeError, err_msg.c_str()); 110 SWIG_fail; 111 } 112 113 if (llvm::StringRef(type_name.get()).starts_with("SB")) { 114 std::string error_msg = "Input type is invalid: " + type_name.get(); 115 PyErr_SetString(PyExc_TypeError, error_msg.c_str()); 116 SWIG_fail; 117 } else { 118 $1 = obj.get(); 119 } 120 } 121 } 122} 123 124%typemap(out) lldb::ScriptObjectPtr { 125 $result = (PyObject*) $1; 126 if (!$result) 127 $result = Py_None; 128 Py_INCREF($result); 129} 130 131%typemap(out) lldb::SBScriptObject { 132 $result = nullptr; 133 if (const void* impl = $1.GetPointer()) 134 $result = (PyObject*) impl; 135 if (!$result) { 136 $result = Py_None; 137 Py_INCREF(Py_None); 138 } else { 139 Py_INCREF($result); 140 } 141} 142 143%typemap(out) char** { 144 int len; 145 int i; 146 len = 0; 147 while ($1[len]) 148 len++; 149 PythonList list(len); 150 for (i = 0; i < len; i++) 151 list.SetItemAtIndex(i, PythonString($1[i])); 152 $result = list.release(); 153} 154 155%typemap(in) lldb::tid_t { 156 PythonObject obj = Retain<PythonObject>($input); 157 lldb::tid_t value = unwrapOrSetPythonException(As<unsigned long long>(obj)); 158 if (PyErr_Occurred()) 159 SWIG_fail; 160 $1 = value; 161} 162 163%typemap(in) lldb::StateType { 164 PythonObject obj = Retain<PythonObject>($input); 165 unsigned long long state_type_value = 166 unwrapOrSetPythonException(As<unsigned long long>(obj)); 167 if (PyErr_Occurred()) 168 SWIG_fail; 169 if (state_type_value > lldb::StateType::kLastStateType) { 170 PyErr_SetString(PyExc_ValueError, "Not a valid StateType value"); 171 SWIG_fail; 172 } 173 $1 = static_cast<lldb::StateType>(state_type_value); 174} 175 176/* Typemap definitions to allow SWIG to properly handle char buffer. */ 177 178// typemap for a char buffer 179%typemap(in) (char *dst, size_t dst_len) { 180 if (!PyLong_Check($input)) { 181 PyErr_SetString(PyExc_ValueError, "Expecting an integer"); 182 SWIG_fail; 183 } 184 $2 = PyLong_AsLong($input); 185 if ($2 <= 0) { 186 PyErr_SetString(PyExc_ValueError, "Positive integer expected"); 187 SWIG_fail; 188 } 189 $1 = (char *)malloc($2); 190} 191// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated 192// as char data instead of byte data. 193%typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len); 194 195// Return the char buffer. Discarding any previous return result 196%typemap(argout) (char *dst, size_t dst_len) { 197 Py_XDECREF($result); /* Blow away any previous result */ 198 if (result == 0) { 199 PythonString string(""); 200 $result = string.release(); 201 Py_INCREF($result); 202 } else { 203 llvm::StringRef ref(static_cast<const char *>($1), result); 204 PythonString string(ref); 205 $result = string.release(); 206 } 207 free($1); 208} 209// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated 210// as char data instead of byte data. 211%typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len); 212 213 214// typemap for handling an snprintf-like API like SBThread::GetStopDescription. 215%typemap(in) (char *dst_or_null, size_t dst_len) { 216 if (!PyLong_Check($input)) { 217 PyErr_SetString(PyExc_ValueError, "Expecting an integer"); 218 SWIG_fail; 219 } 220 $2 = PyLong_AsLong($input); 221 if ($2 <= 0) { 222 PyErr_SetString(PyExc_ValueError, "Positive integer expected"); 223 SWIG_fail; 224 } 225 $1 = (char *)malloc($2); 226} 227%typemap(argout) (char *dst_or_null, size_t dst_len) { 228 Py_XDECREF($result); /* Blow away any previous result */ 229 llvm::StringRef ref($1); 230 PythonString string(ref); 231 $result = string.release(); 232 free($1); 233} 234 235 236// typemap for an outgoing buffer 237// See also SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len). 238// Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len). 239%typemap(in) (const char *cstr, uint32_t cstr_len), 240 (const char *src, size_t src_len) { 241 if (PythonString::Check($input)) { 242 PythonString str(PyRefType::Borrowed, $input); 243 $1 = (char *)str.GetString().data(); 244 $2 = str.GetSize(); 245 } else if (PythonByteArray::Check($input)) { 246 PythonByteArray bytearray(PyRefType::Borrowed, $input); 247 $1 = (char *)bytearray.GetBytes().data(); 248 $2 = bytearray.GetSize(); 249 } else if (PythonBytes::Check($input)) { 250 PythonBytes bytes(PyRefType::Borrowed, $input); 251 $1 = (char *)bytes.GetBytes().data(); 252 $2 = bytes.GetSize(); 253 } else { 254 PyErr_SetString(PyExc_ValueError, "Expecting a string"); 255 SWIG_fail; 256 } 257} 258// For SBProcess::WriteMemory, SBTarget::GetInstructions and SBDebugger::DispatchInput. 259%typemap(in) (const void *buf, size_t size), 260 (const void *data, size_t data_len), 261 (const void *buf, uint64_t size) { 262 if (PythonString::Check($input)) { 263 PythonString str(PyRefType::Borrowed, $input); 264 $1 = (void *)str.GetString().data(); 265 $2 = str.GetSize(); 266 } else if (PythonByteArray::Check($input)) { 267 PythonByteArray bytearray(PyRefType::Borrowed, $input); 268 $1 = (void *)bytearray.GetBytes().data(); 269 $2 = bytearray.GetSize(); 270 } else if (PythonBytes::Check($input)) { 271 PythonBytes bytes(PyRefType::Borrowed, $input); 272 $1 = (void *)bytes.GetBytes().data(); 273 $2 = bytes.GetSize(); 274 } else { 275 PyErr_SetString(PyExc_ValueError, "Expecting a buffer"); 276 SWIG_fail; 277 } 278} 279 280// typemap for an incoming buffer 281// See also SBProcess::ReadMemory. 282%typemap(in) (void *buf, size_t size) { 283 if (PyLong_Check($input)) { 284 $2 = PyLong_AsLong($input); 285 } else { 286 PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object"); 287 SWIG_fail; 288 } 289 if ($2 <= 0) { 290 PyErr_SetString(PyExc_ValueError, "Positive integer expected"); 291 SWIG_fail; 292 } 293 $1 = (void *)malloc($2); 294} 295 296// Return the buffer. Discarding any previous return result 297// See also SBProcess::ReadMemory. 298%typemap(argout) (void *buf, size_t size) { 299 Py_XDECREF($result); /* Blow away any previous result */ 300 if (result == 0) { 301 $result = Py_None; 302 Py_INCREF($result); 303 } else { 304 PythonBytes bytes(static_cast<const uint8_t *>($1), result); 305 $result = bytes.release(); 306 } 307 free($1); 308} 309 310%{ 311namespace { 312template <class T> 313T PyLongAsT(PyObject *obj) { 314 static_assert(true, "unsupported type"); 315} 316 317template <> uint64_t PyLongAsT<uint64_t>(PyObject *obj) { 318 return static_cast<uint64_t>(PyLong_AsUnsignedLongLong(obj)); 319} 320 321template <> uint32_t PyLongAsT<uint32_t>(PyObject *obj) { 322 return static_cast<uint32_t>(PyLong_AsUnsignedLong(obj)); 323} 324 325template <> int64_t PyLongAsT<int64_t>(PyObject *obj) { 326 return static_cast<int64_t>(PyLong_AsLongLong(obj)); 327} 328 329template <> int32_t PyLongAsT<int32_t>(PyObject *obj) { 330 return static_cast<int32_t>(PyLong_AsLong(obj)); 331} 332 333template <class T> bool SetNumberFromPyObject(T &number, PyObject *obj) { 334 if (PyLong_Check(obj)) 335 number = PyLongAsT<T>(obj); 336 else 337 return false; 338 339 return true; 340} 341 342template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) { 343 if (PyFloat_Check(obj)) { 344 number = PyFloat_AsDouble(obj); 345 return true; 346 } 347 348 return false; 349} 350 351} // namespace 352%} 353 354// these typemaps allow Python users to pass list objects 355// and have them turn into C++ arrays (this is useful, for instance 356// when creating SBData objects from lists of numbers) 357%typemap(in) (uint64_t* array, size_t array_len), 358 (uint32_t* array, size_t array_len), 359 (int64_t* array, size_t array_len), 360 (int32_t* array, size_t array_len), 361 (double* array, size_t array_len) { 362 /* Check if is a list */ 363 if (PyList_Check($input)) { 364 int size = PyList_Size($input); 365 int i = 0; 366 $2 = size; 367 $1 = ($1_type)malloc(size * sizeof($*1_type)); 368 for (i = 0; i < size; i++) { 369 PyObject *o = PyList_GetItem($input, i); 370 if (!SetNumberFromPyObject($1[i], o)) { 371 PyErr_SetString(PyExc_TypeError, "list must contain numbers"); 372 SWIG_fail; 373 } 374 375 if (PyErr_Occurred()) { 376 SWIG_fail; 377 } 378 } 379 } else if ($input == Py_None) { 380 $1 = NULL; 381 $2 = 0; 382 } else { 383 PyErr_SetString(PyExc_TypeError, "not a list"); 384 SWIG_fail; 385 } 386} 387 388%typemap(freearg) (uint64_t* array, size_t array_len), 389 (uint32_t* array, size_t array_len), 390 (int64_t* array, size_t array_len), 391 (int32_t* array, size_t array_len), 392 (double* array, size_t array_len) { 393 free($1); 394} 395 396// these typemaps wrap SBModule::GetVersion() from requiring a memory buffer 397// to the more Pythonic style where a list is returned and no previous allocation 398// is necessary - this will break if more than 50 versions are ever returned 399%typemap(typecheck) (uint32_t *versions, uint32_t num_versions) { 400 $1 = ($input == Py_None ? 1 : 0); 401} 402 403%typemap(in, numinputs=0) (uint32_t *versions) { 404 $1 = (uint32_t *)malloc(sizeof(uint32_t) * 50); 405} 406 407%typemap(in, numinputs=0) (uint32_t num_versions) { 408 $1 = 50; 409} 410 411%typemap(argout) (uint32_t *versions, uint32_t num_versions) { 412 uint32_t count = result; 413 if (count >= $2) 414 count = $2; 415 PyObject *list = PyList_New(count); 416 for (uint32_t j = 0; j < count; j++) { 417 PyObject *item = PyLong_FromLong($1[j]); 418 int ok = PyList_SetItem(list, j, item); 419 if (ok != 0) { 420 $result = Py_None; 421 break; 422 } 423 } 424 $result = list; 425} 426 427%typemap(freearg) (uint32_t *versions) { 428 free($1); 429} 430 431// For Log::LogOutputCallback 432%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { 433 if (!($input == Py_None || 434 PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { 435 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); 436 SWIG_fail; 437 } 438 439 // FIXME (filcab): We can't currently check if our callback is already 440 // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous 441 // baton) nor can we just remove all traces of a callback, if we want to 442 // revert to a file logging mechanism. 443 444 // Don't lose the callback reference 445 Py_INCREF($input); 446 $1 = LLDBSwigPythonCallPythonLogOutputCallback; 447 $2 = $input; 448} 449 450%typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) { 451 $1 = $input == Py_None; 452 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input)); 453} 454 455// For lldb::SBDebuggerDestroyCallback 456%typemap(in) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { 457 if (!($input == Py_None || 458 PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { 459 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); 460 SWIG_fail; 461 } 462 463 // FIXME (filcab): We can't currently check if our callback is already 464 // LLDBSwigPythonCallPythonSBDebuggerTerminateCallback (to DECREF the previous 465 // baton) nor can we just remove all traces of a callback, if we want to 466 // revert to a file logging mechanism. 467 468 // Don't lose the callback reference 469 Py_INCREF($input); 470 $1 = LLDBSwigPythonCallPythonSBDebuggerTerminateCallback; 471 $2 = $input; 472} 473 474%typemap(typecheck) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { 475 $1 = $input == Py_None; 476 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input)); 477} 478 479// For lldb::SBCommandPrintCallback 480%typemap(in) (lldb::SBCommandPrintCallback callback, void *baton) { 481 if (!($input == Py_None || 482 PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { 483 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); 484 SWIG_fail; 485 } 486 487 // Don't lose the callback reference. 488 Py_INCREF($input); 489 $1 = LLDBSwigPythonCallPythonCommandPrintCallback; 490 $2 = $input; 491} 492 493%typemap(typecheck) (lldb::SBCommandPrintCallback callback, void *baton) { 494 $1 = $input == Py_None; 495 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input)); 496} 497 498%typemap(in) (lldb::CommandOverrideCallback callback, void *baton) { 499 if (!($input == Py_None || 500 PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { 501 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); 502 SWIG_fail; 503 } 504 505 // Don't lose the callback reference. 506 Py_INCREF($input); 507 $1 = LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback; 508 $2 = $input; 509} 510%typemap(typecheck) (lldb::CommandOverrideCallback callback, void *baton) { 511 $1 = $input == Py_None; 512 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input)); 513} 514 515%typemap(in) lldb::FileSP { 516 PythonFile py_file(PyRefType::Borrowed, $input); 517 if (!py_file) { 518 PyErr_SetString(PyExc_TypeError, "not a file"); 519 SWIG_fail; 520 } 521 auto sp = unwrapOrSetPythonException(py_file.ConvertToFile()); 522 if (!sp) 523 SWIG_fail; 524 $1 = sp; 525} 526 527%typemap(in) lldb::FileSP FORCE_IO_METHODS { 528 PythonFile py_file(PyRefType::Borrowed, $input); 529 if (!py_file) { 530 PyErr_SetString(PyExc_TypeError, "not a file"); 531 SWIG_fail; 532 } 533 auto sp = unwrapOrSetPythonException( 534 py_file.ConvertToFileForcingUseOfScriptingIOMethods()); 535 if (!sp) 536 SWIG_fail; 537 $1 = sp; 538} 539 540%typemap(in) lldb::FileSP BORROWED { 541 PythonFile py_file(PyRefType::Borrowed, $input); 542 if (!py_file) { 543 PyErr_SetString(PyExc_TypeError, "not a file"); 544 SWIG_fail; 545 } 546 auto sp = 547 unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true)); 548 if (!sp) 549 SWIG_fail; 550 $1 = sp; 551} 552 553%typemap(in) lldb::FileSP BORROWED_FORCE_IO_METHODS { 554 PythonFile py_file(PyRefType::Borrowed, $input); 555 if (!py_file) { 556 PyErr_SetString(PyExc_TypeError, "not a file"); 557 SWIG_fail; 558 } 559 auto sp = unwrapOrSetPythonException( 560 py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true)); 561 if (!sp) 562 SWIG_fail; 563 $1 = sp; 564} 565 566%typecheck(SWIG_TYPECHECK_POINTER) lldb::FileSP { 567 if (PythonFile::Check($input)) { 568 $1 = 1; 569 } else { 570 PyErr_Clear(); 571 $1 = 0; 572 } 573} 574 575%typemap(out) lldb::FileSP { 576 $result = nullptr; 577 const lldb::FileSP &sp = $1; 578 if (sp) { 579 PythonFile pyfile = unwrapOrSetPythonException(PythonFile::FromFile(*sp)); 580 if (!pyfile.IsValid()) 581 SWIG_fail; 582 $result = pyfile.release(); 583 } 584 if (!$result) { 585 $result = Py_None; 586 Py_INCREF(Py_None); 587 } 588} 589 590%typemap(in) (const char* string, int len) { 591 if ($input == Py_None) { 592 $1 = NULL; 593 $2 = 0; 594 } else if (PythonString::Check($input)) { 595 PythonString py_str(PyRefType::Borrowed, $input); 596 llvm::StringRef str = py_str.GetString(); 597 $1 = const_cast<char *>(str.data()); 598 $2 = str.size(); 599 // In Python 2, if $input is a PyUnicode object then this 600 // will trigger a Unicode -> String conversion, in which 601 // case the `PythonString` will now own the PyString. Thus 602 // if it goes out of scope, the data will be deleted. The 603 // only way to avoid this is to leak the Python object in 604 // that case. Note that if there was no conversion, then 605 // releasing the string will not leak anything, since we 606 // created this as a borrowed reference. 607 py_str.release(); 608 } else { 609 PyErr_SetString(PyExc_TypeError, "not a string-like object"); 610 SWIG_fail; 611 } 612} 613 614// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i, 615// and fixed so they will not crash if PyObject_GetBuffer fails. 616// https://github.com/swig/swig/issues/1640 617// 618// I've also moved the call to PyBuffer_Release to the end of the SWIG wrapper, 619// doing it right away is not legal according to the python buffer protocol. 620 621%define %pybuffer_mutable_binary(TYPEMAP, SIZE) 622%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) { 623 int res; 624 Py_ssize_t size = 0; 625 void *buf = 0; 626 res = PyObject_GetBuffer($input, &view.buffer, PyBUF_WRITABLE); 627 if (res < 0) { 628 PyErr_Clear(); 629 %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); 630 } 631 size = view.buffer.len; 632 buf = view.buffer.buf; 633 $1 = ($1_ltype)buf; 634 $2 = ($2_ltype)(size / sizeof($*1_type)); 635} 636%enddef 637 638%define %pybuffer_binary(TYPEMAP, SIZE) 639%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) { 640 int res; 641 Py_ssize_t size = 0; 642 const void *buf = 0; 643 res = PyObject_GetBuffer($input, &view.buffer, PyBUF_CONTIG_RO); 644 if (res < 0) { 645 PyErr_Clear(); 646 %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); 647 } 648 size = view.buffer.len; 649 buf = view.buffer.buf; 650 $1 = ($1_ltype)buf; 651 $2 = ($2_ltype)(size / sizeof($*1_type)); 652} 653%enddef 654 655%pybuffer_binary(const uint8_t *buf, size_t num_bytes); 656%pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes); 657 658%typemap(in) (const char **symbol_name, uint32_t num_names) { 659 using namespace lldb_private; 660 /* Check if is a list */ 661 if (PythonList::Check($input)) { 662 PythonList list(PyRefType::Borrowed, $input); 663 $2 = list.GetSize(); 664 int i = 0; 665 $1 = (char**)malloc(($2+1)*sizeof(char*)); 666 for (i = 0; i < $2; i++) { 667 PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>(); 668 if (!py_str.IsAllocated()) { 669 PyErr_SetString(PyExc_TypeError,"list must contain strings and blubby"); 670 free($1); 671 return nullptr; 672 } 673 674 $1[i] = const_cast<char*>(py_str.GetString().data()); 675 } 676 $1[i] = 0; 677 } else if ($input == Py_None) { 678 $1 = NULL; 679 } else { 680 PyErr_SetString(PyExc_TypeError,"not a list"); 681 return NULL; 682 } 683} 684 685// For lldb::SBPlatformLocateModuleCallback 686%typemap(in) (lldb::SBPlatformLocateModuleCallback callback, 687 void *callback_baton) { 688 if (!($input == Py_None || 689 PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { 690 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); 691 SWIG_fail; 692 } 693 694 if ($input == Py_None) { 695 $1 = nullptr; 696 $2 = nullptr; 697 } else { 698 PythonCallable callable = Retain<PythonCallable>($input); 699 if (!callable.IsValid()) { 700 PyErr_SetString(PyExc_TypeError, "Need a valid callable object"); 701 SWIG_fail; 702 } 703 704 llvm::Expected<PythonCallable::ArgInfo> arg_info = callable.GetArgInfo(); 705 if (!arg_info) { 706 PyErr_SetString(PyExc_TypeError, 707 ("Could not get arguments: " + 708 llvm::toString(arg_info.takeError())).c_str()); 709 SWIG_fail; 710 } 711 712 if (arg_info.get().max_positional_args != 3) { 713 PyErr_SetString(PyExc_TypeError, "Expected 3 argument callable object"); 714 SWIG_fail; 715 } 716 717 // NOTE: When this is called multiple times, this will leak the Python 718 // callable object as other callbacks, because this does not call Py_DECREF 719 // the object. But it should be almost zero impact since this method is 720 // expected to be called only once. 721 722 // Don't lose the callback reference 723 Py_INCREF($input); 724 725 $1 = LLDBSwigPythonCallLocateModuleCallback; 726 $2 = $input; 727 } 728} 729 730%typemap(typecheck) (lldb::SBPlatformLocateModuleCallback callback, 731 void *callback_baton) { 732 $1 = $input == Py_None; 733 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input)); 734} 735