vtkWrapPython.c 89 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkWrapPython.c

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
13
14
15

=========================================================================*/

16
#include "vtkPython.h"
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include <ctype.h>
21
#include "vtkParse.h"
22
#include "vtkConfigure.h"
23

24
25
26
/* -------------------------------------------------------------------- */
/* the main entry method, called by vtkParse.y */
void vtkParseOutput(FILE *fp, FileInfo *data);
27

28

29
30
/* -------------------------------------------------------------------- */
/* prototypes for the methods used by the python wrappers */
31

32
33
34
/* generate the class docstring and write it to "fp" */
static void vtkWrapPython_ClassDoc(
  FILE *fp, FileInfo *data);
35

36
37
/* print out all methods and the method table */
static void vtkWrapPython_GenerateMethods(
38
  FILE *fp, FileInfo *data, int class_has_new, int do_constructors);
39

40
41
42
/* make a temporary variable for an arg value or a return value */
static void vtkWrapPython_MakeTempVariable(
  FILE *fp, FunctionInfo *currentFunction, int i);
43

44
/* print the code to build python return value from a method */
45
46
static void vtkWrapPython_ReturnValue(
  FILE *fp, FunctionInfo *currentFunction);
47

48
49
50
/* print the code to return a hinted value from a method */
static void vtkWrapPython_ReturnHintedValue(
  FILE *fp, FunctionInfo *currentFunction);
51
52


53
54
/* -------------------------------------------------------------------- */
/* prototypes for utility methods */
55

56
57
/* check whether a method is wrappable */
static int vtkWrapPython_MethodCheck(
58
  FileInfo *data, FunctionInfo *currentFunction, int contructor);
59

60
61
62
/* create a format string for PyArg_ParseTuple */
static char *vtkWrapPython_FormatString(
  FunctionInfo *currentFunction);
63

64
65
66
67
/* weed out methods that will never be called */
static void vtkWrapPython_RemovePreceededMethods(
  FunctionInfo *wrappedFunctions[], int numberWrapped, int fnum);

68
69
/* create a string for checking arguments against available signatures */
static char *vtkWrapPython_ArgCheckString(
70
  int isvtkobjmethod, FunctionInfo *currentFunction);
71

72
73
74
/* replace the original method signature with a python-ized signature */
static void vtkWrapPython_Signature(
  FunctionInfo *currentFunction);
75

76
77
78
/* quote a string for inclusion in a C string literal */
static const char *vtkWrapPython_QuoteString(
  const char *comment, int maxlen);
79
80


81
82
83
84
85
86
87
88
89
/* -------------------------------------------------------------------- */
/* Use the hints in the hints file to get the tuple size to use when
 * returning for a pointer-type return value.  The Python return value
 * is created with Py_BuildValue() with the appropriate format string,
 * e.g. Py_BuildValue((char *)"fff", temp20[0], temp20[1], temp20[2]))
 * for a trio of floats.  The type cast is needed because python 2.0
 * Py_BuildValue does not use const.  The "20" is MAX_ARGS, which is
 * used to signify the return value.  If a null pointer is encountered,
 * then Py_BuildValue((char *)"") is used to create a None object. */
90

91
92
93
94
95
static void vtkWrapPython_ReturnHintedValue(
  FILE *fp, FunctionInfo *currentFunction)
{
  int  i;
  const char *c = 0;
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  /* Get the char code for the return type */
  switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
    {
    /* Basic types */
    case VTK_PARSE_FLOAT_PTR: { c = "f"; break;}
    case VTK_PARSE_DOUBLE_PTR: { c = "d"; break;}
    case VTK_PARSE_INT_PTR: { c = "i"; break; }
    case VTK_PARSE_SHORT_PTR: { c = "i"; break; }
    case VTK_PARSE_UNSIGNED_SHORT_PTR: { c = "i"; break; }
    case VTK_PARSE_SIGNED_CHAR_PTR: { c = "i"; break; }
    case VTK_PARSE_UNSIGNED_CHAR_PTR: { c = "i"; break; }
    case VTK_PARSE_LONG_PTR: { c = "l"; break; }

    /* Bool was "int" until Python 2.3 */
    case VTK_PARSE_BOOL_PTR: { c = "i"; break; }

    /* The vtkIdType depends on configuration */
114
#ifdef VTK_USE_64BIT_IDS
115
#ifdef PY_LONG_LONG
116
    case VTK_PARSE_ID_TYPE_PTR: { c = "L"; break; }
117
#else
118
    case VTK_PARSE_ID_TYPE_PTR: { c = "l"; break; }
119
120
#endif
#else
121
    case VTK_PARSE_ID_TYPE_PTR: { c = "i"; break; }
122
#endif
123

124
    /* The 64-bit types require PY_LONG_LONG */
125
#ifdef PY_LONG_LONG
126
127
    case VTK_PARSE_LONG_LONG_PTR: { c = "L"; break; }
    case VTK_PARSE___INT64_PTR: { c = "L"; break; }
128
#else
129
130
    case VTK_PARSE_LONG_LONG_PTR: { c = "l"; break; }
    case VTK_PARSE___INT64_PTR: { c = "l"; break; }
131
#endif
132

133
    /* These should be added with appropriate compile-time checks */
134
135
    case VTK_PARSE_UNSIGNED_INT_PTR:
    case VTK_PARSE_UNSIGNED_LONG_PTR:
136
137
    case VTK_PARSE_UNSIGNED___INT64_PTR:
    case VTK_PARSE_UNSIGNED_LONG_LONG_PTR:
138
139
      break;
    }
140

141
142
143
144
145
146
  if (c)
    {
    /* Check to make sure the pointer is not NULL */
    fprintf(fp,
            "    if(temp%i)\n"
            "      {\n"
147
            "      result = Py_BuildValue((char*)\"",
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
            MAX_ARGS);

    for (i = 0; i < currentFunction->HintSize; i++)
      {
      fprintf(fp, "%s", c);
      }
    fprintf(fp, "\"");

    for (i = 0; i < currentFunction->HintSize; i++)
      {
      fprintf(fp, ",temp%i[%d]",MAX_ARGS,i);
      }
    fprintf(fp, ");\n"
            "      }\n"
            "    else\n");
    }

  /* If the pointer was NULL, then build a None and return it */
166
167
  fprintf(fp,
          "      {\n"
168
          "      result = Py_BuildValue((char*)\"\");\n"
169
          "      }\n");
170

171
172
173
174
  return;
}


175
176
177
178
179
180
181
/* -------------------------------------------------------------------- */
/* This method produces a temporary variable of the required type:
 * "i" is the argument id, to keep the various temps unique, and
 * if "i" == MAX_ARGS, then declare as return type instead of arg type */

static void vtkWrapPython_MakeTempVariable(
  FILE *fp, FunctionInfo *currentFunction, int i)
182
{
183
184
185
186
187
188
189
190
191
192
193
194
  int aType = currentFunction->ReturnType;
  char *Id = currentFunction->ReturnClass;
  int aCount = 0;

  if (i < MAX_ARGS)
    {
    aType = currentFunction->ArgTypes[i];
    Id = currentFunction->ArgClasses[i];
    aCount = currentFunction->ArgCounts[i];
    }

  /* handle the function pointer type */
195
  if (aType == VTK_PARSE_FUNCTION)
196
    {
197
198
199
    fprintf(fp,
            "  PyObject *temp%i;\n",
            i);
200
201
    return;
    }
202
203

  /* do nothing for void, unless it is "void *" */
204
205
  if (((aType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VOID) &&
      ((aType & VTK_PARSE_INDIRECT) == 0))
206
207
208
209
    {
    return;
    }

210
  /* for const * return types, prepend with const */
211
  if ((i == MAX_ARGS) && ((aType & VTK_PARSE_CONST) != 0))
212
213
214
215
216
217
218
    {
    fprintf(fp,"  const ");
    }
  else
    {
    fprintf(fp,"  ");
    }
219
220

  /* for unsigned, prepend with "unsigned" */
221
  if ((aType & VTK_PARSE_UNSIGNED) != 0)
222
223
224
    {
    fprintf(fp,"unsigned ");
    }
225
226

  /* print the type itself */
227
  switch ((aType & VTK_PARSE_BASE_TYPE) & ~VTK_PARSE_UNSIGNED)
228
    {
229
    case VTK_PARSE_FLOAT:       fprintf(fp,"float "); break;
230
    case VTK_PARSE_DOUBLE:      fprintf(fp,"double "); break;
231
232
233
234
235
    case VTK_PARSE_INT:         fprintf(fp,"int "); break;
    case VTK_PARSE_SHORT:       fprintf(fp,"short "); break;
    case VTK_PARSE_LONG:        fprintf(fp,"long "); break;
    case VTK_PARSE_VOID:        fprintf(fp,"void "); break;
    case VTK_PARSE_CHAR:        fprintf(fp,"char "); break;
236
237
238
239
240
241
    case VTK_PARSE_VTK_OBJECT:  fprintf(fp,"%s ",Id); break;
    case VTK_PARSE_ID_TYPE:     fprintf(fp,"vtkIdType "); break;
    case VTK_PARSE_LONG_LONG:   fprintf(fp,"long long "); break;
    case VTK_PARSE___INT64:     fprintf(fp,"__int64 "); break;
    case VTK_PARSE_SIGNED_CHAR: fprintf(fp,"signed char "); break;
    case VTK_PARSE_BOOL:        fprintf(fp,"bool "); break;
242
243
    case VTK_PARSE_STRING:      fprintf(fp,"vtkStdString "); break;
    case VTK_PARSE_UNICODE_STRING: fprintf(fp,"vtkUnicodeString "); break;
244
    case VTK_PARSE_UNKNOWN:     return;
245
    }
246
247

  /* then print the decorators for ref and pointer, but not for arrays */
248
  switch (aType & VTK_PARSE_INDIRECT)
249
    {
250
    case VTK_PARSE_REF:
251
      fprintf(fp, "*"); /* refs are converted to pointers */
252
      break;
253
    case VTK_PARSE_POINTER:
254
255
256
257
      if ((i == MAX_ARGS) ||
          ((aType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT) ||
          ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_CHAR_PTR) ||
          ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VOID_PTR))
Ken Martin's avatar
Ken Martin committed
258
        {
259
        fprintf(fp, "*");
Ken Martin's avatar
Ken Martin committed
260
        }
261
      break;
262
263
264
265
266
267
268
269
    case VTK_PARSE_POINTER_REF:
      fprintf(fp, "*&");
      break;
    case VTK_PARSE_POINTER_POINTER:
      fprintf(fp, "**");
      break;
    default:
      break;
270
    }
271

272
273
274
275
  /* handle non-vtkObjectBase object arguments as pointers */
  if ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VTK_OBJECT &&
      i != MAX_ARGS)
    {
276
    fprintf(fp, "*");
277
278
    }

279
  /* the variable name */
280
  fprintf(fp,"temp%i",i);
281

282
  /* print the array decorators */
283
  if (((aType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER) &&
284
285
286
287
      (i != MAX_ARGS) &&
      ((aType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_VTK_OBJECT) &&
      ((aType & VTK_PARSE_UNQUALIFIED_TYPE) != VTK_PARSE_CHAR_PTR) &&
      ((aType & VTK_PARSE_UNQUALIFIED_TYPE) != VTK_PARSE_VOID_PTR))
288
289
290
291
    {
    fprintf(fp,"[%i]",aCount);
    }

292
  /* finish off with a semicolon */
293
  fprintf(fp,";\n");
294
295

  /* for "void *", add another temp to hold the size of the argument */
296
297
  if (((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VOID_PTR) &&
      (i != MAX_ARGS))
298
    {
299
    fprintf(fp,
300
            "  int size%d;\n",
301
            i);
302
    }
303

304
  /* for VTK_OBJECT arguments, a PyObject temp is also needed */
305
  if ((i != MAX_ARGS) &&
306
      ((aType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT))
307
    {
308
    fprintf(fp,
309
            "  PyObject *tempH%d = 0;\n",
310
            i);
311
    }
312
313

  /* ditto for bool */
314
315
  if ((i != MAX_ARGS) &&
      ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_BOOL))
316
317
318
319
320
    {
    fprintf(fp,
            "  PyObject *tempB%d = 0;\n",
            i);
    }
321
322
323

  /* ditto for string */
  if ((i != MAX_ARGS) &&
324
       ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_STRING))
325
326
    {
    fprintf(fp,
327
328
329
330
331
332
333
334
335
336
            "  const char *tempC%d = 0;\n",
            i);
    }

  /* ditto for unicode */
  if ((i != MAX_ARGS) &&
      ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_UNICODE_STRING))
    {
    fprintf(fp,
            "  PyObject *tempU%d = 0;\n"
337
            "  PyObject *tempS%d = 0;\n",
338
339
340
341
342
343
344
345
346
            i, i);
    }

  /* A temporary mini-string for character return value conversion */
  if ((i == MAX_ARGS) &&
      ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_CHAR))
    {
    fprintf(fp,
            "  char tempA%d[2];\n",
347
348
            i);
    }
349
350
}

351
352
353
354
355
356
357

/* -------------------------------------------------------------------- */
/* Convert values into python object and return them within python,
 * using the static var "currentFunction" as the current function */

static void vtkWrapPython_ReturnValue(
  FILE *fp, FunctionInfo *currentFunction)
358
{
359
  /* for void, just return "None" */
360
361
  if (((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VOID)
      && ((currentFunction->ReturnType & VTK_PARSE_INDIRECT) == 0))
362
    {
363
364
    fprintf(fp,
            "    Py_INCREF(Py_None);\n"
365
            "    result = Py_None;\n");
366
367
    return;
    }
368

369
  /* for other types, handle as required */
370
  switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
371
    {
372
373

    /* convert "char *" to a python string, by copying */
374
    case VTK_PARSE_CHAR_PTR:
375
      {
376
      fprintf(fp,
377
378
              "    if (temp%i == NULL)\n"
              "      {\n"
379
              "      Py_INCREF(Py_None);\n"
380
              "      result = Py_None;\n"
381
382
383
              "      }\n"
              "    else\n"
              "      {\n"
384
              "      result = PyString_FromString(temp%i);\n"
385
              "      }\n",
386
              MAX_ARGS, MAX_ARGS);
387
388
389
390
      break;
      }

    /* convert VTK objects to Python objects */
391
    case VTK_PARSE_VTK_OBJECT_PTR:
392
      {
393
      fprintf(fp,
394
              "    result = vtkPythonUtil::GetObjectFromPointer((vtkObjectBase *)temp%i);\n",
Ken Martin's avatar
Ken Martin committed
395
              MAX_ARGS);
396
397
      break;
      }
398

399
    /* convert special objects to Python objects */
400
    case VTK_PARSE_VTK_OBJECT_REF:
401
402
      {
      fprintf(fp,
403
              "    result = vtkPythonUtil::GetSpecialObjectFromPointer(temp%i, \"%s\");\n",
404
405
406
407
408
              MAX_ARGS, currentFunction->ReturnClass);
      break;
      }

    /* convert special objects to Python objects */
409
410
    case VTK_PARSE_VTK_OBJECT:
      {
411
      fprintf(fp,
412
              "    result = vtkPythonUtil::GetSpecialObjectFromPointer(&temp%i, \"%s\");\n",
413
              MAX_ARGS, currentFunction->ReturnClass);
414
415
416
417
      break;
      }

    /* handle functions returning tuples via the hints file */
418
419
420
421
422
423
424
425
426
427
    case VTK_PARSE_FLOAT_PTR:
    case VTK_PARSE_DOUBLE_PTR:
    case VTK_PARSE_ID_TYPE_PTR:
    case VTK_PARSE_LONG_LONG_PTR:
    case VTK_PARSE___INT64_PTR:
    case VTK_PARSE_SIGNED_CHAR_PTR:
    case VTK_PARSE_INT_PTR:
    case VTK_PARSE_SHORT_PTR:
    case VTK_PARSE_LONG_PTR:
    case VTK_PARSE_BOOL_PTR:
428
      vtkWrapPython_ReturnHintedValue(fp, currentFunction);
429
      break;
430
431
432

    /* convert void pointers to None (if NULL) or to a python string,
     * where the string refers to rather than copies the contents */
433
    case VTK_PARSE_VOID_PTR:
434
      {
435
436
437
438
      fprintf(fp,
              "    if (temp%i == NULL)\n"
              "      {\n"
              "      Py_INCREF(Py_None);\n"
439
              "      result = Py_None;\n"
440
441
442
              "      }\n"
              "    else\n"
              "      {\n"
443
              "      result = PyString_FromString(vtkPythonUtil::ManglePointer(temp%i,\"void_p\"));\n"
444
445
              "      }\n",
              MAX_ARGS, MAX_ARGS);
446
447
      break;
      }
448
449

    /* handle all basic types by simple conversion */
450
451
    case VTK_PARSE_FLOAT:
    case VTK_PARSE_DOUBLE:
452
      {
453
      fprintf(fp,
454
              "    result = PyFloat_FromDouble(temp%i);\n",
455
              MAX_ARGS);
456
457
      break;
      }
458
459
460
461
462
463
464
    case VTK_PARSE_UNSIGNED_CHAR:
    case VTK_PARSE_UNSIGNED_INT:
    case VTK_PARSE_UNSIGNED_SHORT:
    case VTK_PARSE_INT:
    case VTK_PARSE_SHORT:
    case VTK_PARSE_LONG:
    case VTK_PARSE_SIGNED_CHAR:
465
      {
466
      fprintf(fp,
467
              "    result = PyInt_FromLong(temp%i);\n",
468
              MAX_ARGS);
469
      break;
470
      }
471
472
473

    /* PyBool_FromLong was introduced in Python 2.3,
     * but PyInt_FromLong is a good substitute */
474
    case VTK_PARSE_BOOL:
475
      {
476
477
      fprintf(fp,
              "#if PY_VERSION_HEX >= 0x02030000\n"
478
              "    result = PyBool_FromLong(temp%i);\n"
479
              "#else\n"
480
              "    result = PyInt_FromLong((long)temp%i);\n"
481
482
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
483
484
      break;
      }
485
486

    /* PyLong_FromUnsignedLong() is new to Python 2.2 */
487
    case VTK_PARSE_UNSIGNED_LONG:
488
      {
489
      fprintf(fp,
490
              "#if (PY_VERSION_HEX >= 0x02020000)\n"
491
              "    result = PyLong_FromUnsignedLong(temp%i);\n"
492
              "#else\n"
493
              "    result = PyInt_FromLong((long)temp%i);\n"
494
495
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
496
497
      break;
      }
498
499

    /* Support for vtkIdType depends on config and capabilities */
500
#if defined(VTK_USE_64BIT_IDS) && defined(PY_LONG_LONG) && (VTK_SIZEOF_LONG != VTK_SIZEOF_ID_TYPE)
501
    case VTK_PARSE_ID_TYPE:
502
      {
503
      fprintf(fp,
504
              "    result = PyLong_FromLongLong(temp%i);\n",
505
              MAX_ARGS);
506
507
      break;
      }
508
    case VTK_PARSE_UNSIGNED_ID_TYPE:
509
      {
510
      fprintf(fp,
511
              "    result = PyLong_FromUnsignedLongLong(temp%i);\n",
512
513
514
515
              MAX_ARGS);
      break;
      }
#else
516
    case VTK_PARSE_ID_TYPE:
517
      {
518
      fprintf(fp,
519
              "    result = PyInt_FromLong((long)temp%i);\n",
520
              MAX_ARGS);
521
522
      break;
      }
523
    case VTK_PARSE_UNSIGNED_ID_TYPE:
524
      {
525
      fprintf(fp,
526
              "#if (PY_VERSION_HEX >= 0x02020000)\n"
527
              "    result = PyLong_FromUnsignedLong((unsigned long)temp%i);\n"
528
              "#else\n"
529
              "    result = PyInt_FromLong((long)temp%i);\n"
530
531
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
532
533
      break;
      }
534
#endif
535
536

    /* support for "long long" depends on config and capabilities */
537
538
#if defined(VTK_SIZEOF_LONG_LONG)
# if defined(PY_LONG_LONG) && (VTK_SIZEOF_LONG != VTK_SIZEOF_LONG_LONG)
539
    case VTK_PARSE_LONG_LONG:
540
      {
541
      fprintf(fp,
542
              "    result = PyLong_FromLongLong(temp%i);\n",
543
              MAX_ARGS);
544
545
      break;
      }
546
    case VTK_PARSE_UNSIGNED_LONG_LONG:
547
      {
548
      fprintf(fp,
549
              "    result = PyLong_FromUnsignedLongLong(temp%i);\n",
550
551
552
553
              MAX_ARGS);
      break;
      }
# else
554
    case VTK_PARSE_LONG_LONG:
555
      {
556
      fprintf(fp,
557
              "    result = PyLong_FromLong(temp%i);\n",
558
              MAX_ARGS);
559
560
      break;
      }
561
    case VTK_PARSE_UNSIGNED_LONG_LONG:
562
      {
563
      fprintf(fp,
564
              "    result = PyLong_FromUnsignedLong(temp%i);\n",
565
566
567
568
569
              MAX_ARGS);
      break;
      }
# endif
#endif
570
571

    /* support for "__int64" depends on config and capabilities */
572
573
#if defined(VTK_SIZEOF___INT64)
# if defined(PY_LONG_LONG) && (VTK_SIZEOF_LONG != VTK_SIZEOF___INT64)
574
    case VTK_PARSE___INT64:
575
      {
576
      fprintf(fp,
577
              "    result = PyLong_FromLongLong(temp%i);\n",
578
              MAX_ARGS);
579
580
      break;
      }
581
    case VTK_PARSE_UNSIGNED___INT64:
582
      {
583
      fprintf(fp,
584
              "    result = PyLong_FromUnsignedLongLong(temp%i);\n",
585
586
587
588
              MAX_ARGS);
      break;
      }
# else
589
    case VTK_PARSE___INT64:
590
      {
591
      fprintf(fp,
592
              "    result = PyLong_FromLong(temp%i);\n",
593
              MAX_ARGS);
594
595
      break;
      }
596
    case VTK_PARSE_UNSIGNED___INT64:
597
      {
598
      fprintf(fp,
599
              "#if (PY_VERSION_HEX >= 0x02020000)\n"
600
              "    result = PyLong_FromUnsignedLong((unsigned long)temp%i);\n"
601
              "#else\n"
602
              "    result = PyInt_FromLong((long)temp%i);\n"
603
604
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
605
606
607
      break;
      }
# endif
608
#endif
609
610

    /* return a char as a string of unit length */
611
    case VTK_PARSE_CHAR:
612
      {
613
      fprintf(fp,
614
615
616
617
              "    tempA%i[0] = temp%i;\n"
              "    tempA%i[1] = \'\\0\';\n"
              "    result = PyString_FromStringAndSize(tempA%i,1);\n",
              MAX_ARGS, MAX_ARGS, MAX_ARGS, MAX_ARGS);
618
619
      break;
      }
620
621
622
623
624
625
626

    /* return a string */
    case VTK_PARSE_STRING:
      {
      fprintf(fp,
              "    result = PyString_FromString(temp%i);\n",
              MAX_ARGS);
627
628
629
      break;
      }

630
631
632
    /* return a vtkUnicodeString, using utf8 intermediate because python
     * can be configured for either 32-bit or 16-bit unicode and it's
     * tricky to test both, so utf8 is a safe alternative */
633
634
635
636
637
638
639
640
641
    case VTK_PARSE_UNICODE_STRING:
      {
      fprintf(fp,
              "      {\n"
              "      const char *s = temp%i.utf8_str();\n"
              "      result = PyUnicode_DecodeUTF8(s, strlen(s), \"strict\");\n"
              "      }\n",
              MAX_ARGS);
      break;
642
      }
643
644
645
    }
}

646
647
648
649

/* -------------------------------------------------------------------- */
/* Create a format string for PyArg_ParseTuple(), see the python
 * documentation for PyArg_ParseTuple() for more information.
650
651
652
 * Briefly, "O" is for objects and "d", "f", "i" etc are basic types.
 *
 * If any new format characters are added here, they must also be
653
 * added to vtkPythonUtil::CheckArg() in vtkPythonUtil.cxx
654
 */
655
656

static char *vtkWrapPython_FormatString(FunctionInfo *currentFunction)
657
658
659
660
661
{
  static char result[1024];
  int currPos = 0;
  int argtype;
  int i, j;
662

663
  if (currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION)
664
    {
665
    result[currPos++] = 'O';
666
667
668
    result[currPos] = '\0';
    return result;
    }
669

670
671
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
672
    argtype = (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
673
674
675

    switch (argtype)
      {
676
677
      case VTK_PARSE_FLOAT_PTR:
        result[currPos++] = '(';
678
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
679
          {
680
          result[currPos++] = 'f';
Ken Martin's avatar
Ken Martin committed
681
          }
682
        result[currPos++] = ')';
Ken Martin's avatar
Ken Martin committed
683
        break;
684
685
      case VTK_PARSE_DOUBLE_PTR:
        result[currPos++] = '(';
686
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
687
          {
688
          result[currPos++] = 'd';
Ken Martin's avatar
Ken Martin committed
689
          }
690
        result[currPos++] = ')';
Ken Martin's avatar
Ken Martin committed
691
        break;
692
      case VTK_PARSE_BOOL_PTR: /* there is no char for "bool" */
693
694
      case VTK_PARSE_INT_PTR:
        result[currPos++] = '(';
695
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
696
          {
697
          result[currPos++] = 'i';
Ken Martin's avatar
Ken Martin committed
698
          }
699
        result[currPos++] = ')';
Ken Martin's avatar
Ken Martin committed
700
        break;
701
702
      case VTK_PARSE_ID_TYPE_PTR:
        result[currPos++] = '(';
703
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
704
705
          {
#ifdef VTK_USE_64BIT_IDS
706
#ifdef PY_LONG_LONG
707
          result[currPos++] = 'L';
708
#else
709
          result[currPos++] = 'l';
710
711
#endif
#else
712
          result[currPos++] = 'i';
713
714
#endif
          }
715
        result[currPos++] = ')';
716
        break;
717
718
719
      case VTK_PARSE_LONG_LONG_PTR:
      case VTK_PARSE___INT64_PTR:
        result[currPos++] = '(';
720
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
721
722
          {
#ifdef PY_LONG_LONG
723
          result[currPos++] = 'L';
724
#else
725
          result[currPos++] = 'l';
726
727
#endif
          }
728
729
730
731
        result[currPos++] = ')';
        break;
      case VTK_PARSE_VTK_OBJECT_REF:
      case VTK_PARSE_VTK_OBJECT_PTR:
732
      case VTK_PARSE_VTK_OBJECT:
733
734
735
736
        result[currPos++] = 'O';
        break;
      case VTK_PARSE_CHAR_PTR:
        result[currPos++] = 'z';
737
        break;
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
      case VTK_PARSE_VOID_PTR:
        result[currPos++] = 's';
        result[currPos++] = '#';
        break;
      case VTK_PARSE_FLOAT:
        result[currPos++] = 'f';
        break;
      case VTK_PARSE_DOUBLE:
        result[currPos++] = 'd';
        break;
      case VTK_PARSE_UNSIGNED_INT:
      case VTK_PARSE_INT:
        result[currPos++] = 'i';
        break;
      case VTK_PARSE_UNSIGNED_SHORT:
      case VTK_PARSE_SHORT:
        result[currPos++] = 'h';
        break;
      case VTK_PARSE_UNSIGNED_LONG:
      case VTK_PARSE_LONG:
        result[currPos++] = 'l';
        break;
      case VTK_PARSE_UNSIGNED_ID_TYPE:
      case VTK_PARSE_ID_TYPE:
762
#ifdef VTK_USE_64BIT_IDS
763
#ifdef PY_LONG_LONG
764
        result[currPos++] = 'L';
765
#else
766
        result[currPos++] = 'l';
767
768
#endif
#else
769
        result[currPos++] = 'i';
770
#endif
771
        break;
772
#ifdef PY_LONG_LONG
773
774
775
776
777
778
      case VTK_PARSE_UNSIGNED_LONG_LONG:
      case VTK_PARSE_UNSIGNED___INT64:
      case VTK_PARSE_LONG_LONG:
      case VTK_PARSE___INT64:
        result[currPos++] = 'L';
        break;
779
#else
780
781
782
783
784
785
      case VTK_PARSE_UNSIGNED_LONG_LONG:
      case VTK_PARSE_UNSIGNED___INT64:
      case VTK_PARSE_LONG_LONG:
      case VTK_PARSE___INT64:
        result[currPos++] = 'l';
        break;
786
#endif
787
      case VTK_PARSE_SIGNED_CHAR:
788
        result[currPos++] = 'b';
789
790
791
792
793
794
795
796
        break;
      case VTK_PARSE_CHAR:
        result[currPos++] = 'c';
        break;
      case VTK_PARSE_UNSIGNED_CHAR:
        result[currPos++] = 'b';
        break;
      case VTK_PARSE_BOOL:
797
        result[currPos++] = 'O';
798
        break;
799
      case VTK_PARSE_STRING:
800
        result[currPos++] = 's';
801
802
803
804
        break;
      case VTK_PARSE_UNICODE_STRING:
        result[currPos++] = 'O';
        break;
805
806
807
808
809
810
811
      }
    }

  result[currPos] = '\0';
  return result;
}

812
813
/* -------------------------------------------------------------------- */
/* Create a string to describe the signature of a method.
814
815
816
817
818
 * If isvtkobject is set the string will start with an ampersand.
 * Following the optional space will be a ParseTuple format string,
 * followed by the names of any VTK classes required.  The optional
 * ampersand indicates that methods like vtkClass.Method(self, arg1,...)
 * are possible, and the ampersand is a placeholder for "self". */
819
820

static char *vtkWrapPython_ArgCheckString(
821
  int isvtkobjmethod, FunctionInfo *currentFunction)
822
823
824
825
826
827
{
  static char result[1024];
  int currPos = 0;
  int argtype;
  int i;

828
829
830
831
832
  if (isvtkobjmethod)
    {
    result[currPos++] = '@';
    }

833
834
835
836
837
838
839
840
841
842
843
844
845
  strcpy(&result[currPos], vtkWrapPython_FormatString(currentFunction));
  currPos = strlen(result);

  if (currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION)
    {
    strcpy(&result[currPos], " func");
    return result;
    }

  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
    argtype = (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);

846
847
848
849
850
851
    if (argtype == VTK_PARSE_BOOL)
      {
      strcpy(&result[currPos], " bool");
      currPos += 5;
      }

852
853
854
855
856
857
    if (argtype == VTK_PARSE_UNICODE_STRING)
      {
      strcpy(&result[currPos], " unicode");
      currPos += 8;
      }

858
859
860
861
862
    if (argtype == VTK_PARSE_VTK_OBJECT_REF ||
        argtype == VTK_PARSE_VTK_OBJECT_PTR ||
        argtype == VTK_PARSE_VTK_OBJECT)
      {
      result[currPos++] = ' ';
863
864
865
866
867
868
869
870
      if (argtype == VTK_PARSE_VTK_OBJECT_REF)
        {
        result[currPos++] = '&';
        }
      else if (argtype == VTK_PARSE_VTK_OBJECT_PTR)
        {
        result[currPos++] = '*';
        }
871
872
873
874
875
876
877
878
      strcpy(&result[currPos], currentFunction->ArgClasses[i]);
      currPos += strlen(currentFunction->ArgClasses[i]);
      }
    }

  return result;
}

879
880
881
882
/* -------------------------------------------------------------------- */
/* The method signatures are for the python docstrings. */

static void vtkWrapPython_AddToSignature(char *sig, const char *add, int *i)
883
{
884
885
886
887
888
889
890
891
892
893
894
895
  int j = 0;
  char *cp = &sig[*i];
  for (; add[j] != '\0'; j++)
    {
    // stop at the semicolon, there's often garbage after it
    if ((cp[j] = add[j]) == ';')
      {
      cp[++j] = '\0';
      break;
      }
    }
  *i += j;
896
897
}

898
899
900
901
902
/* -------------------------------------------------------------------- */
/* Create a signature for the python version of a method, and
 * write it directly to currentFunction->Signature */

static void vtkWrapPython_Signature(FunctionInfo *currentFunction)
903
904
905
906
907
908
909
{
  static char result[1024];
  int currPos = 0;
  int argtype;
  int i, j;

  /* print out the name of the method */
910
911
  vtkWrapPython_AddToSignature(result,"V.",&currPos);
  vtkWrapPython_AddToSignature(result,currentFunction->Name,&currPos);
912
913

  /* print the arg list */
914
  vtkWrapPython_AddToSignature(result,"(",&currPos);
915

916
917
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
918
    if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
919
      {
920
      vtkWrapPython_AddToSignature(result,"function",&currPos);
921
      }
922

923
    argtype = (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
924
925
926

    if (i != 0)
      {
927
      vtkWrapPython_AddToSignature(result,", ",&currPos);
928
929
930
931
      }

    switch (argtype)
      {
932
933
      case VTK_PARSE_FLOAT_PTR:
      case VTK_PARSE_DOUBLE_PTR:
934
        vtkWrapPython_AddToSignature(result,"(",&currPos);
935
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
936
937
938
          {
          if (j != 0)
            {
939
            vtkWrapPython_AddToSignature(result,", ",&currPos);
Ken Martin's avatar
Ken Martin committed
940
            }
941
          vtkWrapPython_AddToSignature(result,"float",&currPos);
Ken Martin's avatar
Ken Martin committed
942
          }
943
        vtkWrapPython_AddToSignature(result,")",&currPos);
Ken Martin's avatar
Ken Martin committed
944
        break;
945
      case VTK_PARSE_INT_PTR:
946
        vtkWrapPython_AddToSignature(result,"(",&currPos);
947
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
948
949
950
          {
          if (j != 0)
            {
951
            vtkWrapPython_AddToSignature(result,", ",&currPos);
Ken Martin's avatar
Ken Martin committed
952
            }
David Gobbi's avatar