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%typemap(in) (lldb::CommandOverrideCallback callback, void *baton) { 480 if (!($input == Py_None || 481 PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { 482 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); 483 SWIG_fail; 484 } 485 486 // Don't lose the callback reference. 487 Py_INCREF($input); 488 $1 = LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback; 489 $2 = $input; 490} 491%typemap(typecheck) (lldb::CommandOverrideCallback callback, void *baton) { 492 $1 = $input == Py_None; 493 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input)); 494} 495 496%typemap(in) lldb::FileSP { 497 PythonFile py_file(PyRefType::Borrowed, $input); 498 if (!py_file) { 499 PyErr_SetString(PyExc_TypeError, "not a file"); 500 SWIG_fail; 501 } 502 auto sp = unwrapOrSetPythonException(py_file.ConvertToFile()); 503 if (!sp) 504 SWIG_fail; 505 $1 = sp; 506} 507 508%typemap(in) lldb::FileSP FORCE_IO_METHODS { 509 PythonFile py_file(PyRefType::Borrowed, $input); 510 if (!py_file) { 511 PyErr_SetString(PyExc_TypeError, "not a file"); 512 SWIG_fail; 513 } 514 auto sp = unwrapOrSetPythonException( 515 py_file.ConvertToFileForcingUseOfScriptingIOMethods()); 516 if (!sp) 517 SWIG_fail; 518 $1 = sp; 519} 520 521%typemap(in) lldb::FileSP BORROWED { 522 PythonFile py_file(PyRefType::Borrowed, $input); 523 if (!py_file) { 524 PyErr_SetString(PyExc_TypeError, "not a file"); 525 SWIG_fail; 526 } 527 auto sp = 528 unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true)); 529 if (!sp) 530 SWIG_fail; 531 $1 = sp; 532} 533 534%typemap(in) lldb::FileSP BORROWED_FORCE_IO_METHODS { 535 PythonFile py_file(PyRefType::Borrowed, $input); 536 if (!py_file) { 537 PyErr_SetString(PyExc_TypeError, "not a file"); 538 SWIG_fail; 539 } 540 auto sp = unwrapOrSetPythonException( 541 py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true)); 542 if (!sp) 543 SWIG_fail; 544 $1 = sp; 545} 546 547%typecheck(SWIG_TYPECHECK_POINTER) lldb::FileSP { 548 if (PythonFile::Check($input)) { 549 $1 = 1; 550 } else { 551 PyErr_Clear(); 552 $1 = 0; 553 } 554} 555 556%typemap(out) lldb::FileSP { 557 $result = nullptr; 558 const lldb::FileSP &sp = $1; 559 if (sp) { 560 PythonFile pyfile = unwrapOrSetPythonException(PythonFile::FromFile(*sp)); 561 if (!pyfile.IsValid()) 562 SWIG_fail; 563 $result = pyfile.release(); 564 } 565 if (!$result) { 566 $result = Py_None; 567 Py_INCREF(Py_None); 568 } 569} 570 571%typemap(in) (const char* string, int len) { 572 if ($input == Py_None) { 573 $1 = NULL; 574 $2 = 0; 575 } else if (PythonString::Check($input)) { 576 PythonString py_str(PyRefType::Borrowed, $input); 577 llvm::StringRef str = py_str.GetString(); 578 $1 = const_cast<char *>(str.data()); 579 $2 = str.size(); 580 // In Python 2, if $input is a PyUnicode object then this 581 // will trigger a Unicode -> String conversion, in which 582 // case the `PythonString` will now own the PyString. Thus 583 // if it goes out of scope, the data will be deleted. The 584 // only way to avoid this is to leak the Python object in 585 // that case. Note that if there was no conversion, then 586 // releasing the string will not leak anything, since we 587 // created this as a borrowed reference. 588 py_str.release(); 589 } else { 590 PyErr_SetString(PyExc_TypeError, "not a string-like object"); 591 SWIG_fail; 592 } 593} 594 595// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i, 596// and fixed so they will not crash if PyObject_GetBuffer fails. 597// https://github.com/swig/swig/issues/1640 598// 599// I've also moved the call to PyBuffer_Release to the end of the SWIG wrapper, 600// doing it right away is not legal according to the python buffer protocol. 601 602%define %pybuffer_mutable_binary(TYPEMAP, SIZE) 603%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) { 604 int res; 605 Py_ssize_t size = 0; 606 void *buf = 0; 607 res = PyObject_GetBuffer($input, &view.buffer, PyBUF_WRITABLE); 608 if (res < 0) { 609 PyErr_Clear(); 610 %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); 611 } 612 size = view.buffer.len; 613 buf = view.buffer.buf; 614 $1 = ($1_ltype)buf; 615 $2 = ($2_ltype)(size / sizeof($*1_type)); 616} 617%enddef 618 619%define %pybuffer_binary(TYPEMAP, SIZE) 620%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) { 621 int res; 622 Py_ssize_t size = 0; 623 const void *buf = 0; 624 res = PyObject_GetBuffer($input, &view.buffer, PyBUF_CONTIG_RO); 625 if (res < 0) { 626 PyErr_Clear(); 627 %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); 628 } 629 size = view.buffer.len; 630 buf = view.buffer.buf; 631 $1 = ($1_ltype)buf; 632 $2 = ($2_ltype)(size / sizeof($*1_type)); 633} 634%enddef 635 636%pybuffer_binary(const uint8_t *buf, size_t num_bytes); 637%pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes); 638 639%typemap(in) (const char **symbol_name, uint32_t num_names) { 640 using namespace lldb_private; 641 /* Check if is a list */ 642 if (PythonList::Check($input)) { 643 PythonList list(PyRefType::Borrowed, $input); 644 $2 = list.GetSize(); 645 int i = 0; 646 $1 = (char**)malloc(($2+1)*sizeof(char*)); 647 for (i = 0; i < $2; i++) { 648 PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>(); 649 if (!py_str.IsAllocated()) { 650 PyErr_SetString(PyExc_TypeError,"list must contain strings and blubby"); 651 free($1); 652 return nullptr; 653 } 654 655 $1[i] = const_cast<char*>(py_str.GetString().data()); 656 } 657 $1[i] = 0; 658 } else if ($input == Py_None) { 659 $1 = NULL; 660 } else { 661 PyErr_SetString(PyExc_TypeError,"not a list"); 662 return NULL; 663 } 664} 665 666// For lldb::SBPlatformLocateModuleCallback 667%typemap(in) (lldb::SBPlatformLocateModuleCallback callback, 668 void *callback_baton) { 669 if (!($input == Py_None || 670 PyCallable_Check(reinterpret_cast<PyObject *>($input)))) { 671 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); 672 SWIG_fail; 673 } 674 675 if ($input == Py_None) { 676 $1 = nullptr; 677 $2 = nullptr; 678 } else { 679 PythonCallable callable = Retain<PythonCallable>($input); 680 if (!callable.IsValid()) { 681 PyErr_SetString(PyExc_TypeError, "Need a valid callable object"); 682 SWIG_fail; 683 } 684 685 llvm::Expected<PythonCallable::ArgInfo> arg_info = callable.GetArgInfo(); 686 if (!arg_info) { 687 PyErr_SetString(PyExc_TypeError, 688 ("Could not get arguments: " + 689 llvm::toString(arg_info.takeError())).c_str()); 690 SWIG_fail; 691 } 692 693 if (arg_info.get().max_positional_args != 3) { 694 PyErr_SetString(PyExc_TypeError, "Expected 3 argument callable object"); 695 SWIG_fail; 696 } 697 698 // NOTE: When this is called multiple times, this will leak the Python 699 // callable object as other callbacks, because this does not call Py_DECREF 700 // the object. But it should be almost zero impact since this method is 701 // expected to be called only once. 702 703 // Don't lose the callback reference 704 Py_INCREF($input); 705 706 $1 = LLDBSwigPythonCallLocateModuleCallback; 707 $2 = $input; 708 } 709} 710 711%typemap(typecheck) (lldb::SBPlatformLocateModuleCallback callback, 712 void *callback_baton) { 713 $1 = $input == Py_None; 714 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input)); 715} 716