]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - scripts/Python/python-typemaps.swig
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / scripts / Python / python-typemaps.swig
1 /* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */
2
3 %typemap(in) char ** {
4   using namespace lldb_private;
5   /* Check if is a list  */
6   if (PythonList::Check($input)) {
7     PythonList list(PyRefType::Borrowed, $input);
8     int size = list.GetSize();
9     int i = 0;
10     $1 = (char**)malloc((size+1)*sizeof(char*));
11     for (i = 0; i < size; i++) {
12       PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>();
13       if (!py_str.IsAllocated()) {
14         PyErr_SetString(PyExc_TypeError,"list must contain strings");
15         free($1);
16         return nullptr;
17       }
18
19       $1[i] = const_cast<char*>(py_str.GetString().data());
20     }
21     $1[i] = 0;
22   } else if ($input == Py_None) {
23     $1 =  NULL;
24   } else {
25     PyErr_SetString(PyExc_TypeError,"not a list");
26     return NULL;
27   }
28 }
29
30 %typemap(typecheck) char ** {
31   /* Check if is a list  */
32   $1 = 1;
33   using namespace lldb_private;
34   if (PythonList::Check($input)) {
35     PythonList list(PyRefType::Borrowed, $input);
36     int size = list.GetSize();
37     int i = 0;
38     for (i = 0; i < size; i++) {
39       PythonString s = list.GetItemAtIndex(i).AsType<PythonString>();
40       if (!s.IsAllocated()) { $1 = 0; }
41     }
42   }
43   else
44   {
45     $1 = ( ($input == Py_None) ? 1 : 0);
46   }
47 }
48
49 %typemap(freearg) char** {
50   free((char *) $1);
51 }
52
53 %typemap(out) char** {
54   int len;
55   int i;
56   len = 0;
57   while ($1[len]) len++;
58   using namespace lldb_private;
59   PythonList list(len);
60   for (i = 0; i < len; i++)
61     list.SetItemAtIndex(i, PythonString($1[i]));
62   $result = list.release();
63 }
64
65
66 %typemap(in) lldb::tid_t {
67   using namespace lldb_private;
68   if (PythonInteger::Check($input))
69   {
70     PythonInteger py_int(PyRefType::Borrowed, $input);
71     $1 = static_cast<lldb::tid_t>(py_int.GetInteger());
72   }
73   else
74   {
75     PyErr_SetString(PyExc_ValueError, "Expecting an integer");
76     return nullptr;
77   }
78 }
79
80 /* Typemap definitions to allow SWIG to properly handle char buffer. */
81
82 // typemap for a char buffer
83 // See also SBThread::GetStopDescription.
84 %typemap(in) (char *dst, size_t dst_len) {
85    if (!PyInt_Check($input)) {
86        PyErr_SetString(PyExc_ValueError, "Expecting an integer");
87        return NULL;
88    }
89    $2 = PyInt_AsLong($input);
90    if ($2 <= 0) {
91        PyErr_SetString(PyExc_ValueError, "Positive integer expected");
92        return NULL;
93    }
94    $1 = (char *) malloc($2);
95 }
96 // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
97 // as char data instead of byte data.
98 %typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
99
100 // Return the char buffer.  Discarding any previous return result
101 // See also SBThread::GetStopDescription.
102 %typemap(argout) (char *dst, size_t dst_len) {
103    Py_XDECREF($result);   /* Blow away any previous result */
104    if (result == 0) {
105       $result = Py_None;
106       Py_INCREF($result);
107    } else {
108       llvm::StringRef ref(static_cast<const char*>($1), result);
109       lldb_private::PythonString string(ref);
110       $result = string.release();
111    }
112    free($1);
113 }
114 // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
115 // as char data instead of byte data.
116 %typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
117
118
119 // typemap for an outgoing buffer
120 // See also SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len).
121 %typemap(in) (const char *cstr, uint32_t cstr_len) {
122    using namespace lldb_private;
123    if (PythonString::Check($input)) {
124       PythonString str(PyRefType::Borrowed, $input);
125       $1 = (char*)str.GetString().data();
126       $2 = str.GetSize();
127    }
128    else if(PythonByteArray::Check($input)) {
129       PythonByteArray bytearray(PyRefType::Borrowed, $input);
130       $1 = (char*)bytearray.GetBytes().data();
131       $2 = bytearray.GetSize();
132    }
133    else if (PythonBytes::Check($input)) {
134       PythonBytes bytes(PyRefType::Borrowed, $input);
135       $1 = (char*)bytes.GetBytes().data();
136       $2 = bytes.GetSize();
137    }
138    else {
139       PyErr_SetString(PyExc_ValueError, "Expecting a string");
140       return NULL;
141    }
142 }
143 // Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len).
144 %typemap(in) (const char *src, size_t src_len) {
145    using namespace lldb_private;
146    if (PythonString::Check($input)) {
147       PythonString str(PyRefType::Borrowed, $input);
148       $1 = (char*)str.GetString().data();
149       $2 = str.GetSize();
150    }
151    else if(PythonByteArray::Check($input)) {
152       PythonByteArray bytearray(PyRefType::Borrowed, $input);
153       $1 = (char*)bytearray.GetBytes().data();
154       $2 = bytearray.GetSize();
155    }
156    else if (PythonBytes::Check($input)) {
157       PythonBytes bytes(PyRefType::Borrowed, $input);
158       $1 = (char*)bytes.GetBytes().data();
159       $2 = bytes.GetSize();
160    }
161    else {
162       PyErr_SetString(PyExc_ValueError, "Expecting a string");
163       return NULL;
164    }
165 }
166 // And SBProcess::WriteMemory.
167 %typemap(in) (const void *buf, size_t size) {
168    using namespace lldb_private;
169    if (PythonString::Check($input)) {
170       PythonString str(PyRefType::Borrowed, $input);
171       $1 = (void*)str.GetString().data();
172       $2 = str.GetSize();
173    }
174    else if(PythonByteArray::Check($input)) {
175       PythonByteArray bytearray(PyRefType::Borrowed, $input);
176       $1 = (void*)bytearray.GetBytes().data();
177       $2 = bytearray.GetSize();
178    }
179    else if (PythonBytes::Check($input)) {
180       PythonBytes bytes(PyRefType::Borrowed, $input);
181       $1 = (void*)bytes.GetBytes().data();
182       $2 = bytes.GetSize();
183    }
184    else {
185       PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
186       return NULL;
187    }
188 }
189
190 // For SBDebugger::DispatchInput
191 %typemap(in) (const void *data, size_t data_len) {
192    using namespace lldb_private;
193    if (PythonString::Check($input)) {
194       PythonString str(PyRefType::Borrowed, $input);
195       $1 = (void*)str.GetString().data();
196       $2 = str.GetSize();
197    }
198    else if(PythonByteArray::Check($input)) {
199       PythonByteArray bytearray(PyRefType::Borrowed, $input);
200       $1 = (void*)bytearray.GetBytes().data();
201       $2 = bytearray.GetSize();
202    }
203    else if (PythonBytes::Check($input)) {
204       PythonBytes bytes(PyRefType::Borrowed, $input);
205       $1 = (void*)bytes.GetBytes().data();
206       $2 = bytes.GetSize();
207    }
208    else {
209       PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
210       return NULL;
211    }
212 }
213
214 // typemap for an incoming buffer
215 // See also SBProcess::ReadMemory.
216 %typemap(in) (void *buf, size_t size) {
217    if (PyInt_Check($input)) {
218       $2 = PyInt_AsLong($input);
219    } else if (PyLong_Check($input)) {
220       $2 = PyLong_AsLong($input);
221    } else {
222       PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object");
223       return NULL;
224    }
225    if ($2 <= 0) {
226        PyErr_SetString(PyExc_ValueError, "Positive integer expected");
227        return NULL;
228    }
229    $1 = (void *) malloc($2);
230 }
231
232 // Return the buffer.  Discarding any previous return result
233 // See also SBProcess::ReadMemory.
234 %typemap(argout) (void *buf, size_t size) {
235    Py_XDECREF($result);   /* Blow away any previous result */
236    if (result == 0) {
237       $result = Py_None;
238       Py_INCREF($result);
239    } else {
240       lldb_private::PythonBytes bytes(static_cast<const uint8_t*>($1), result);
241       $result = bytes.release();
242    }
243    free($1);
244 }
245
246 // these typemaps allow Python users to pass list objects
247 // and have them turn into C++ arrays (this is useful, for instance
248 // when creating SBData objects from lists of numbers)
249 %typemap(in) (uint64_t* array, size_t array_len) {
250   /* Check if is a list  */
251   if (PyList_Check($input)) {
252     int size = PyList_Size($input);
253     int i = 0;
254     $2 = size;
255     $1 = (uint64_t*) malloc(size * sizeof(uint64_t));
256     for (i = 0; i < size; i++) {
257       PyObject *o = PyList_GetItem($input,i);
258       if (PyInt_Check(o)) {
259         $1[i] = PyInt_AsLong(o);
260       }
261       else if (PyLong_Check(o)) {
262         $1[i] = PyLong_AsUnsignedLongLong(o);
263       }
264       else {
265         PyErr_SetString(PyExc_TypeError,"list must contain numbers");
266         free($1);
267         return NULL;
268       }
269
270       if (PyErr_Occurred()) {
271         free($1);
272         return NULL;
273       }
274     }
275   } else if ($input == Py_None) {
276     $1 =  NULL;
277     $2 = 0;
278   } else {
279     PyErr_SetString(PyExc_TypeError,"not a list");
280     return NULL;
281   }
282 }
283
284 %typemap(freearg) (uint64_t* array, size_t array_len) {
285   free($1);
286 }
287
288 %typemap(in) (uint32_t* array, size_t array_len) {
289   /* Check if is a list  */
290   if (PyList_Check($input)) {
291     int size = PyList_Size($input);
292     int i = 0;
293     $2 = size;
294     $1 = (uint32_t*) malloc(size * sizeof(uint32_t));
295     for (i = 0; i < size; i++) {
296       PyObject *o = PyList_GetItem($input,i);
297       if (PyInt_Check(o)) {
298         $1[i] = PyInt_AsLong(o);
299       }
300       else if (PyLong_Check(o)) {
301         $1[i] = PyLong_AsUnsignedLong(o);
302       }
303       else {
304         PyErr_SetString(PyExc_TypeError,"list must contain numbers");
305         free($1);
306         return NULL;
307       }
308
309       if (PyErr_Occurred()) {
310         free($1);
311         return NULL;
312       }
313     }
314   } else if ($input == Py_None) {
315     $1 =  NULL;
316     $2 = 0;
317   } else {
318     PyErr_SetString(PyExc_TypeError,"not a list");
319     return NULL;
320   }
321 }
322
323 %typemap(freearg) (uint32_t* array, size_t array_len) {
324   free($1);
325 }
326
327 %typemap(in) (int64_t* array, size_t array_len) {
328   /* Check if is a list  */
329   if (PyList_Check($input)) {
330     int size = PyList_Size($input);
331     int i = 0;
332     $2 = size;
333     $1 = (int64_t*) malloc(size * sizeof(int64_t));
334     for (i = 0; i < size; i++) {
335       PyObject *o = PyList_GetItem($input,i);
336       if (PyInt_Check(o)) {
337         $1[i] = PyInt_AsLong(o);
338       }
339       else if (PyLong_Check(o)) {
340         $1[i] = PyLong_AsLongLong(o);
341       }
342       else {
343         PyErr_SetString(PyExc_TypeError,"list must contain numbers");
344         free($1);
345         return NULL;
346       }
347
348       if (PyErr_Occurred()) {
349         free($1);
350         return NULL;
351       }
352     }
353   } else if ($input == Py_None) {
354     $1 =  NULL;
355     $2 = 0;
356   } else {
357     PyErr_SetString(PyExc_TypeError,"not a list");
358     return NULL;
359   }
360 }
361
362 %typemap(freearg) (int64_t* array, size_t array_len) {
363   free($1);
364 }
365
366 %typemap(in) (int32_t* array, size_t array_len) {
367   /* Check if is a list  */
368   if (PyList_Check($input)) {
369     int size = PyList_Size($input);
370     int i = 0;
371     $2 = size;
372     $1 = (int32_t*) malloc(size * sizeof(int32_t));
373     for (i = 0; i < size; i++) {
374       PyObject *o = PyList_GetItem($input,i);
375       if (PyInt_Check(o)) {
376         $1[i] = PyInt_AsLong(o);
377       }
378       else if (PyLong_Check(o)) {
379         $1[i] = PyLong_AsLong(o);
380       }
381       else {
382         PyErr_SetString(PyExc_TypeError,"list must contain numbers");
383         free($1);
384         return NULL;
385       }
386
387       if (PyErr_Occurred()) {
388         free($1);
389         return NULL;
390       }
391     }
392   } else if ($input == Py_None) {
393     $1 =  NULL;
394     $2 = 0;
395   } else {
396     PyErr_SetString(PyExc_TypeError,"not a list");
397     return NULL;
398   }
399 }
400
401 %typemap(freearg) (int32_t* array, size_t array_len) {
402   free($1);
403 }
404
405 %typemap(in) (double* array, size_t array_len) {
406   /* Check if is a list  */
407   if (PyList_Check($input)) {
408     int size = PyList_Size($input);
409     int i = 0;
410     $2 = size;
411     $1 = (double*) malloc(size * sizeof(double));
412     for (i = 0; i < size; i++) {
413       PyObject *o = PyList_GetItem($input,i);
414       if (PyFloat_Check(o)) {
415         $1[i] = PyFloat_AsDouble(o);
416       }
417       else {
418         PyErr_SetString(PyExc_TypeError,"list must contain floating-point numbers");
419         free($1);
420         return NULL;
421       }
422     }
423   } else if ($input == Py_None) {
424     $1 =  NULL;
425     $2 = 0;
426   } else {
427     PyErr_SetString(PyExc_TypeError,"not a list");
428     return NULL;
429   }
430 }
431
432 %typemap(freearg) (double* array, size_t array_len) {
433   free($1);
434 }
435
436 // these typemaps wrap SBModule::GetVersion() from requiring a memory buffer
437 // to the more Pythonic style where a list is returned and no previous allocation
438 // is necessary - this will break if more than 50 versions are ever returned
439 %typemap(typecheck) (uint32_t *versions, uint32_t num_versions) {
440     $1 = ($input == Py_None ? 1 : 0);
441 }
442
443 %typemap(in, numinputs=0) (uint32_t *versions) {
444     $1 = (uint32_t*)malloc(sizeof(uint32_t) * 50);
445 }
446
447 %typemap(in, numinputs=0) (uint32_t num_versions) {
448     $1 = 50;
449 }
450
451 %typemap(argout) (uint32_t *versions, uint32_t num_versions) {
452     uint32_t count = result;
453     if (count >= $2)
454         count = $2;
455     PyObject* list = PyList_New(count);
456     for (uint32_t j = 0; j < count; j++)
457     {
458         if ($1[j] < UINT32_MAX)
459         {
460             PyObject* item = PyInt_FromLong($1[j]);
461             int ok = PyList_SetItem(list,j,item);
462             if (ok != 0)
463             {
464                 $result = Py_None;
465                 break;
466             }
467         }
468         else
469             break;
470     }
471     $result = list;
472 }
473
474 %typemap(freearg) (uint32_t *versions) {
475     free($1);
476 }
477
478
479 // For Log::LogOutputCallback
480 %typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
481   if (!($input == Py_None || PyCallable_Check(reinterpret_cast<PyObject*>($input)))) {
482     PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
483     return NULL;
484   }
485
486   // FIXME (filcab): We can't currently check if our callback is already
487   // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous
488   // baton) nor can we just remove all traces of a callback, if we want to
489   // revert to a file logging mechanism.
490
491   // Don't lose the callback reference
492   Py_INCREF($input);
493   $1 = LLDBSwigPythonCallPythonLogOutputCallback;
494   $2 = $input;
495 }
496
497 %typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) {
498   $1 = $input == Py_None;
499   $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject*>($input));
500 }
501
502 %typemap(in) FILE * {
503    using namespace lldb_private;
504    if ($input == Py_None)
505       $1 = nullptr;
506    else if (!lldb_private::PythonFile::Check($input)) {
507       int fd = PyObject_AsFileDescriptor($input);
508       PythonObject py_input(PyRefType::Borrowed, $input);
509       PythonString py_mode = py_input.GetAttributeValue("mode").AsType<PythonString>();
510
511       if (-1 != fd && py_mode.IsValid()) {
512          FILE *f;
513          if ((f = fdopen(fd, py_mode.GetString().str().c_str())))
514             $1 = f;
515          else
516             PyErr_SetString(PyExc_TypeError, strerror(errno));
517       } else {
518          PyErr_SetString(PyExc_TypeError,"not a file-like object");
519          return nullptr;
520       }
521    }
522    else
523    {
524       PythonFile py_file(PyRefType::Borrowed, $input);
525       File file;
526       if (!py_file.GetUnderlyingFile(file))
527          return nullptr;
528
529       $1 = file.GetStream();
530       if ($1)
531          file.Clear();
532     }
533 }
534
535 %typemap(out) FILE * {
536    char mode[4] = {0};
537 %#ifdef __APPLE__
538    int i = 0;
539    if ($1)
540    {
541        short flags = $1->_flags;
542
543        if (flags & __SRD)
544           mode[i++] = 'r';
545        else if (flags & __SWR)
546           mode[i++] = 'w';
547        else // if (flags & __SRW)
548           mode[i++] = 'a';
549     }
550 %#endif
551    using namespace lldb_private;
552    File file($1, false);
553    PythonFile py_file(file, mode);
554    $result = py_file.release();
555    if (!$result)
556    {
557        $result = Py_None;
558        Py_INCREF(Py_None);
559    }
560 }
561
562 %typemap(in) (const char* string, int len) {
563     using namespace lldb_private;
564     if ($input == Py_None)
565     {
566         $1 = NULL;
567         $2 = 0;
568     }
569     else if (PythonString::Check($input))
570     {
571         PythonString py_str(PyRefType::Borrowed, $input);
572         llvm::StringRef str = py_str.GetString();
573         $1 = const_cast<char*>(str.data());
574         $2 = str.size();
575         // In Python 2, if $input is a PyUnicode object then this
576         // will trigger a Unicode -> String conversion, in which
577         // case the `PythonString` will now own the PyString.  Thus
578         // if it goes out of scope, the data will be deleted.  The
579         // only way to avoid this is to leak the Python object in
580         // that case.  Note that if there was no conversion, then
581         // releasing the string will not leak anything, since we
582         // created this as a borrowed reference.
583         py_str.release();
584     }
585     else
586     {
587         PyErr_SetString(PyExc_TypeError,"not a string-like object");
588         return NULL;
589     }
590 }