vtkWrapPython.c 91.7 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
38
39
/* print out headers for any special types used by methods */
static void vtkWrapPython_GenerateSpecialHeaders(
  FILE *fp, FileInfo *data);

40
41
/* print out all methods and the method table */
static void vtkWrapPython_GenerateMethods(
42
  FILE *fp, FileInfo *data, int class_has_new, int do_constructors);
43

44
45
46
/* make a temporary variable for an arg value or a return value */
static void vtkWrapPython_MakeTempVariable(
  FILE *fp, FunctionInfo *currentFunction, int i);
47

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

52
53
54
/* print the code to return a hinted value from a method */
static void vtkWrapPython_ReturnHintedValue(
  FILE *fp, FunctionInfo *currentFunction);
55
56


57
58
/* -------------------------------------------------------------------- */
/* prototypes for utility methods */
59

60
61
/* check whether a method is wrappable */
static int vtkWrapPython_MethodCheck(
62
63
64
65
66
67
68
69
70
  FunctionInfo *currentFunction);

/* is the method a constructor of the class */
static int vtkWrapPython_IsConstructor(
  FileInfo *data, FunctionInfo *currentFunction);

/* is the method a destructor of the class */
static int vtkWrapPython_IsDestructor(
  FileInfo *data, FunctionInfo *currentFunction);
71

72
73
74
/* create a format string for PyArg_ParseTuple */
static char *vtkWrapPython_FormatString(
  FunctionInfo *currentFunction);
75

76
77
78
79
/* weed out methods that will never be called */
static void vtkWrapPython_RemovePreceededMethods(
  FunctionInfo *wrappedFunctions[], int numberWrapped, int fnum);

80
81
/* create a string for checking arguments against available signatures */
static char *vtkWrapPython_ArgCheckString(
82
  int isvtkobjmethod, FunctionInfo *currentFunction);
83

84
85
86
/* replace the original method signature with a python-ized signature */
static void vtkWrapPython_Signature(
  FunctionInfo *currentFunction);
87

88
89
90
/* quote a string for inclusion in a C string literal */
static const char *vtkWrapPython_QuoteString(
  const char *comment, int maxlen);
91
92


93
94
95
96
97
98
99
100
101
/* -------------------------------------------------------------------- */
/* 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. */
102

103
104
105
106
107
static void vtkWrapPython_ReturnHintedValue(
  FILE *fp, FunctionInfo *currentFunction)
{
  int  i;
  const char *c = 0;
108

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  /* 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 */
126
#ifdef VTK_USE_64BIT_IDS
127
#ifdef PY_LONG_LONG
128
    case VTK_PARSE_ID_TYPE_PTR: { c = "L"; break; }
129
#else
130
    case VTK_PARSE_ID_TYPE_PTR: { c = "l"; break; }
131
132
#endif
#else
133
    case VTK_PARSE_ID_TYPE_PTR: { c = "i"; break; }
134
#endif
135

136
    /* The 64-bit types require PY_LONG_LONG */
137
#ifdef PY_LONG_LONG
138
139
    case VTK_PARSE_LONG_LONG_PTR: { c = "L"; break; }
    case VTK_PARSE___INT64_PTR: { c = "L"; break; }
140
#else
141
142
    case VTK_PARSE_LONG_LONG_PTR: { c = "l"; break; }
    case VTK_PARSE___INT64_PTR: { c = "l"; break; }
143
#endif
144

145
    /* These should be added with appropriate compile-time checks */
146
147
    case VTK_PARSE_UNSIGNED_INT_PTR:
    case VTK_PARSE_UNSIGNED_LONG_PTR:
148
149
    case VTK_PARSE_UNSIGNED___INT64_PTR:
    case VTK_PARSE_UNSIGNED_LONG_LONG_PTR:
150
151
      break;
    }
152

153
154
155
156
157
158
  if (c)
    {
    /* Check to make sure the pointer is not NULL */
    fprintf(fp,
            "    if(temp%i)\n"
            "      {\n"
159
            "      result = Py_BuildValue((char*)\"",
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
            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 */
178
179
  fprintf(fp,
          "      {\n"
180
          "      result = Py_BuildValue((char*)\"\");\n"
181
          "      }\n");
182

183
184
185
186
  return;
}


187
188
189
190
191
192
193
/* -------------------------------------------------------------------- */
/* 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)
194
{
195
196
197
198
199
200
201
202
203
204
205
206
  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 */
207
  if (aType == VTK_PARSE_FUNCTION)
208
    {
209
210
211
    fprintf(fp,
            "  PyObject *temp%i;\n",
            i);
212
213
    return;
    }
214
215

  /* do nothing for void, unless it is "void *" */
216
217
  if (((aType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VOID) &&
      ((aType & VTK_PARSE_INDIRECT) == 0))
218
219
220
221
    {
    return;
    }

222
  /* for const * return types, prepend with const */
223
224
  if ((i == MAX_ARGS) && ((aType & VTK_PARSE_CONST) != 0) &&
      ((aType & VTK_PARSE_INDIRECT) != 0))
225
226
227
228
229
230
231
    {
    fprintf(fp,"  const ");
    }
  else
    {
    fprintf(fp,"  ");
    }
232
233

  /* for unsigned, prepend with "unsigned" */
234
  if ((aType & VTK_PARSE_UNSIGNED) != 0)
235
236
237
    {
    fprintf(fp,"unsigned ");
    }
238
239

  /* print the type itself */
240
  switch ((aType & VTK_PARSE_BASE_TYPE) & ~VTK_PARSE_UNSIGNED)
241
    {
242
    case VTK_PARSE_FLOAT:       fprintf(fp,"float "); break;
243
    case VTK_PARSE_DOUBLE:      fprintf(fp,"double "); break;
244
245
246
247
248
    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;
249
250
251
252
253
254
    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;
255
256
    case VTK_PARSE_STRING:      fprintf(fp,"vtkStdString "); break;
    case VTK_PARSE_UNICODE_STRING: fprintf(fp,"vtkUnicodeString "); break;
257
    case VTK_PARSE_UNKNOWN:     return;
258
    }
259
260

  /* then print the decorators for ref and pointer, but not for arrays */
261
  switch (aType & VTK_PARSE_INDIRECT)
262
    {
263
    case VTK_PARSE_REF:
264
      fprintf(fp, "*"); /* refs are converted to pointers */
265
      break;
266
    case VTK_PARSE_POINTER:
267
268
269
270
      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
271
        {
272
        fprintf(fp, "*");
Ken Martin's avatar
Ken Martin committed
273
        }
274
      break;
275
276
277
278
279
280
281
282
    case VTK_PARSE_POINTER_REF:
      fprintf(fp, "*&");
      break;
    case VTK_PARSE_POINTER_POINTER:
      fprintf(fp, "**");
      break;
    default:
      break;
283
    }
284

285
286
287
288
  /* handle non-vtkObjectBase object arguments as pointers */
  if ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VTK_OBJECT &&
      i != MAX_ARGS)
    {
289
    fprintf(fp, "*");
290
291
    }

292
  /* the variable name */
293
  fprintf(fp,"temp%i",i);
294

295
  /* print the array decorators */
296
  if (((aType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER) &&
297
298
299
300
      (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))
301
302
303
304
    {
    fprintf(fp,"[%i]",aCount);
    }

305
  /* finish off with a semicolon */
306
307
308
309
310
311
312
313
  if (i == MAX_ARGS)
    {
    fprintf(fp, "; // return value\n");
    }
  else
    {
    fprintf(fp, "; // arg %d\n", i);
    }
314
315

  /* for "void *", add another temp to hold the size of the argument */
316
317
  if (((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VOID_PTR) &&
      (i != MAX_ARGS))
318
    {
319
    fprintf(fp,
320
            "  int size%d;\n",
321
            i);
322
    }
323

324
  /* for VTK_OBJECT arguments, a PyObject temp is also needed */
325
  if ((i != MAX_ARGS) &&
326
      ((aType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT))
327
    {
328
    fprintf(fp,
329
            "  PyObject *tempH%d = 0;\n",
330
            i);
331
    }
332
333

  /* ditto for bool */
334
335
  if ((i != MAX_ARGS) &&
      ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_BOOL))
336
337
    {
    fprintf(fp,
338
339
340
            "  PyObject *tempB%d = 0;\n"
            "  int tempI%d;\n",
            i, i);
341
    }
342
343
344

  /* ditto for string */
  if ((i != MAX_ARGS) &&
345
       ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_STRING))
346
347
    {
    fprintf(fp,
348
349
350
351
352
353
354
355
356
357
            "  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"
358
            "  PyObject *tempS%d = 0;\n",
359
360
361
362
363
364
365
366
367
            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",
368
369
            i);
    }
370
371
}

372
373
374
375
376
377
378

/* -------------------------------------------------------------------- */
/* 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)
379
{
380
  /* for void, just return "None" */
381
382
  if (((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VOID)
      && ((currentFunction->ReturnType & VTK_PARSE_INDIRECT) == 0))
383
    {
384
385
    fprintf(fp,
            "    Py_INCREF(Py_None);\n"
386
            "    result = Py_None;\n");
387
388
    return;
    }
389

390
  /* for other types, handle as required */
391
  switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
392
    {
393
394

    /* convert "char *" to a python string, by copying */
395
    case VTK_PARSE_CHAR_PTR:
396
      {
397
      fprintf(fp,
398
399
              "    if (temp%i == NULL)\n"
              "      {\n"
400
              "      Py_INCREF(Py_None);\n"
401
              "      result = Py_None;\n"
402
403
404
              "      }\n"
              "    else\n"
              "      {\n"
405
              "      result = PyString_FromString(temp%i);\n"
406
              "      }\n",
407
              MAX_ARGS, MAX_ARGS);
408
409
410
411
      break;
      }

    /* convert VTK objects to Python objects */
412
    case VTK_PARSE_VTK_OBJECT_PTR:
413
      {
414
      fprintf(fp,
415
              "    result = vtkPythonUtil::GetObjectFromPointer((vtkObjectBase *)temp%i);\n",
Ken Martin's avatar
Ken Martin committed
416
              MAX_ARGS);
417
418
      break;
      }
419

420
    /* convert special objects to Python objects */
421
    case VTK_PARSE_VTK_OBJECT_REF:
422
423
      {
      fprintf(fp,
424
              "    result = vtkPythonUtil::GetSpecialObjectFromPointer(temp%i, \"%s\");\n",
425
426
427
428
429
              MAX_ARGS, currentFunction->ReturnClass);
      break;
      }

    /* convert special objects to Python objects */
430
431
    case VTK_PARSE_VTK_OBJECT:
      {
432
      fprintf(fp,
433
              "    result = vtkPythonUtil::GetSpecialObjectFromPointer(&temp%i, \"%s\");\n",
434
              MAX_ARGS, currentFunction->ReturnClass);
435
436
437
438
      break;
      }

    /* handle functions returning tuples via the hints file */
439
440
441
442
443
444
445
446
447
448
    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:
449
      vtkWrapPython_ReturnHintedValue(fp, currentFunction);
450
      break;
451
452
453

    /* convert void pointers to None (if NULL) or to a python string,
     * where the string refers to rather than copies the contents */
454
    case VTK_PARSE_VOID_PTR:
455
      {
456
457
458
459
      fprintf(fp,
              "    if (temp%i == NULL)\n"
              "      {\n"
              "      Py_INCREF(Py_None);\n"
460
              "      result = Py_None;\n"
461
462
463
              "      }\n"
              "    else\n"
              "      {\n"
464
              "      result = PyString_FromString(vtkPythonUtil::ManglePointer(temp%i,\"void_p\"));\n"
465
466
              "      }\n",
              MAX_ARGS, MAX_ARGS);
467
468
      break;
      }
469
470

    /* handle all basic types by simple conversion */
471
472
    case VTK_PARSE_FLOAT:
    case VTK_PARSE_DOUBLE:
473
      {
474
      fprintf(fp,
475
              "    result = PyFloat_FromDouble(temp%i);\n",
476
              MAX_ARGS);
477
478
      break;
      }
479
480
481
482
483
484
485
    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:
486
      {
487
      fprintf(fp,
488
              "    result = PyInt_FromLong(temp%i);\n",
489
              MAX_ARGS);
490
      break;
491
      }
492
493
494

    /* PyBool_FromLong was introduced in Python 2.3,
     * but PyInt_FromLong is a good substitute */
495
    case VTK_PARSE_BOOL:
496
      {
497
498
      fprintf(fp,
              "#if PY_VERSION_HEX >= 0x02030000\n"
499
              "    result = PyBool_FromLong((long)temp%i);\n"
500
              "#else\n"
501
              "    result = PyInt_FromLong((long)temp%i);\n"
502
503
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
504
505
      break;
      }
506
507

    /* PyLong_FromUnsignedLong() is new to Python 2.2 */
508
    case VTK_PARSE_UNSIGNED_LONG:
509
      {
510
      fprintf(fp,
511
              "#if (PY_VERSION_HEX >= 0x02020000)\n"
512
              "    result = PyLong_FromUnsignedLong(temp%i);\n"
513
              "#else\n"
514
              "    result = PyInt_FromLong((long)temp%i);\n"
515
516
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
517
518
      break;
      }
519
520

    /* Support for vtkIdType depends on config and capabilities */
521
#if defined(VTK_USE_64BIT_IDS) && defined(PY_LONG_LONG) && (VTK_SIZEOF_LONG != VTK_SIZEOF_ID_TYPE)
522
    case VTK_PARSE_ID_TYPE:
523
      {
524
      fprintf(fp,
525
              "    result = PyLong_FromLongLong(temp%i);\n",
526
              MAX_ARGS);
527
528
      break;
      }
529
    case VTK_PARSE_UNSIGNED_ID_TYPE:
530
      {
531
      fprintf(fp,
532
              "    result = PyLong_FromUnsignedLongLong(temp%i);\n",
533
534
535
536
              MAX_ARGS);
      break;
      }
#else
537
    case VTK_PARSE_ID_TYPE:
538
      {
539
      fprintf(fp,
540
              "    result = PyInt_FromLong((long)temp%i);\n",
541
              MAX_ARGS);
542
543
      break;
      }
544
    case VTK_PARSE_UNSIGNED_ID_TYPE:
545
      {
546
      fprintf(fp,
547
              "#if (PY_VERSION_HEX >= 0x02020000)\n"
548
              "    result = PyLong_FromUnsignedLong((unsigned long)temp%i);\n"
549
              "#else\n"
550
              "    result = PyInt_FromLong((long)temp%i);\n"
551
552
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
553
554
      break;
      }
555
#endif
556
557

    /* support for "long long" depends on config and capabilities */
558
#if defined(VTK_TYPE_USE_LONG_LONG)
559
# if defined(PY_LONG_LONG) && (VTK_SIZEOF_LONG != VTK_SIZEOF_LONG_LONG)
560
    case VTK_PARSE_LONG_LONG:
561
      {
562
      fprintf(fp,
563
              "    result = PyLong_FromLongLong(temp%i);\n",
564
              MAX_ARGS);
565
566
      break;
      }
567
    case VTK_PARSE_UNSIGNED_LONG_LONG:
568
      {
569
      fprintf(fp,
570
              "    result = PyLong_FromUnsignedLongLong(temp%i);\n",
571
572
573
574
              MAX_ARGS);
      break;
      }
# else
575
    case VTK_PARSE_LONG_LONG:
576
      {
577
      fprintf(fp,
578
              "    result = PyLong_FromLong(temp%i);\n",
579
              MAX_ARGS);
580
581
      break;
      }
582
    case VTK_PARSE_UNSIGNED_LONG_LONG:
583
      {
584
      fprintf(fp,
585
              "    result = PyLong_FromUnsignedLong(temp%i);\n",
586
587
588
589
590
              MAX_ARGS);
      break;
      }
# endif
#endif
591
592

    /* support for "__int64" depends on config and capabilities */
593
#if defined(VTK_TYPE_USE___INT64)
594
# if defined(PY_LONG_LONG) && (VTK_SIZEOF_LONG != VTK_SIZEOF___INT64)
595
    case VTK_PARSE___INT64:
596
      {
597
      fprintf(fp,
598
              "    result = PyLong_FromLongLong(temp%i);\n",
599
              MAX_ARGS);
600
601
      break;
      }
602
    case VTK_PARSE_UNSIGNED___INT64:
603
      {
604
      fprintf(fp,
605
              "    result = PyLong_FromUnsignedLongLong(temp%i);\n",
606
607
608
609
              MAX_ARGS);
      break;
      }
# else
610
    case VTK_PARSE___INT64:
611
      {
612
      fprintf(fp,
613
              "    result = PyLong_FromLong(temp%i);\n",
614
              MAX_ARGS);
615
616
      break;
      }
617
    case VTK_PARSE_UNSIGNED___INT64:
618
      {
619
      fprintf(fp,
620
              "#if (PY_VERSION_HEX >= 0x02020000)\n"
621
              "    result = PyLong_FromUnsignedLong((unsigned long)temp%i);\n"
622
              "#else\n"
623
              "    result = PyInt_FromLong((long)temp%i);\n"
624
625
              "#endif\n",
              MAX_ARGS, MAX_ARGS);
626
627
628
      break;
      }
# endif
629
#endif
630
631

    /* return a char as a string of unit length */
632
    case VTK_PARSE_CHAR:
633
      {
634
      fprintf(fp,
635
636
637
638
              "    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);
639
640
      break;
      }
641
642
643
644
645
646
647

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

651
652
653
    /* 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 */
654
655
656
657
658
659
660
661
662
    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;
663
      }
664
665
666
    }
}

667
668
669
670

/* -------------------------------------------------------------------- */
/* Create a format string for PyArg_ParseTuple(), see the python
 * documentation for PyArg_ParseTuple() for more information.
671
672
673
 * 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
674
 * added to vtkPythonUtil::CheckArg() in vtkPythonUtil.cxx
675
 */
676
677

static char *vtkWrapPython_FormatString(FunctionInfo *currentFunction)
678
679
680
681
682
{
  static char result[1024];
  int currPos = 0;
  int argtype;
  int i, j;
683

684
  if (currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION)
685
    {
686
    result[currPos++] = 'O';
687
688
689
    result[currPos] = '\0';
    return result;
    }
690

691
692
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
693
    argtype = (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
694
695
696

    switch (argtype)
      {
697
698
      case VTK_PARSE_FLOAT_PTR:
        result[currPos++] = '(';
699
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
700
          {
701
          result[currPos++] = 'f';
Ken Martin's avatar
Ken Martin committed
702
          }
703
        result[currPos++] = ')';
Ken Martin's avatar
Ken Martin committed
704
        break;
705
706
      case VTK_PARSE_DOUBLE_PTR:
        result[currPos++] = '(';
707
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
708
          {
709
          result[currPos++] = 'd';
Ken Martin's avatar
Ken Martin committed
710
          }
711
        result[currPos++] = ')';
Ken Martin's avatar
Ken Martin committed
712
        break;
713
      case VTK_PARSE_BOOL_PTR: /* there is no char for "bool" */
714
715
      case VTK_PARSE_INT_PTR:
        result[currPos++] = '(';
716
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
717
          {
718
          result[currPos++] = 'i';
Ken Martin's avatar
Ken Martin committed
719
          }
720
        result[currPos++] = ')';
Ken Martin's avatar
Ken Martin committed
721
        break;
722
723
      case VTK_PARSE_ID_TYPE_PTR:
        result[currPos++] = '(';
724
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
725
726
          {
#ifdef VTK_USE_64BIT_IDS
727
#ifdef PY_LONG_LONG
728
          result[currPos++] = 'L';
729
#else
730
          result[currPos++] = 'l';
731
732
#endif
#else
733
          result[currPos++] = 'i';
734
735
#endif
          }
736
        result[currPos++] = ')';
737
        break;
738
739
740
      case VTK_PARSE_LONG_LONG_PTR:
      case VTK_PARSE___INT64_PTR:
        result[currPos++] = '(';
741
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
742
743
          {
#ifdef PY_LONG_LONG
744
          result[currPos++] = 'L';
745
#else
746
          result[currPos++] = 'l';
747
748
#endif
          }
749
750
751
752
        result[currPos++] = ')';
        break;
      case VTK_PARSE_VTK_OBJECT_REF:
      case VTK_PARSE_VTK_OBJECT_PTR:
753
      case VTK_PARSE_VTK_OBJECT:
754
755
756
757
        result[currPos++] = 'O';
        break;
      case VTK_PARSE_CHAR_PTR:
        result[currPos++] = 'z';
758
        break;
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
      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:
783
#ifdef VTK_USE_64BIT_IDS
784
#ifdef PY_LONG_LONG
785
        result[currPos++] = 'L';
786
#else
787
        result[currPos++] = 'l';
788
789
#endif
#else
790
        result[currPos++] = 'i';
791
#endif
792
        break;
793
#ifdef PY_LONG_LONG
794
795
796
797
798
799
      case VTK_PARSE_UNSIGNED_LONG_LONG:
      case VTK_PARSE_UNSIGNED___INT64:
      case VTK_PARSE_LONG_LONG:
      case VTK_PARSE___INT64:
        result[currPos++] = 'L';
        break;
800
#else
801
802
803
804
805
806
      case VTK_PARSE_UNSIGNED_LONG_LONG:
      case VTK_PARSE_UNSIGNED___INT64:
      case VTK_PARSE_LONG_LONG:
      case VTK_PARSE___INT64:
        result[currPos++] = 'l';
        break;
807
#endif
808
      case VTK_PARSE_SIGNED_CHAR:
809
        result[currPos++] = 'b';
810
811
812
813
814
815
816
817
        break;
      case VTK_PARSE_CHAR:
        result[currPos++] = 'c';
        break;
      case VTK_PARSE_UNSIGNED_CHAR:
        result[currPos++] = 'b';
        break;
      case VTK_PARSE_BOOL:
818
        result[currPos++] = 'O';
819
        break;
820
      case VTK_PARSE_STRING:
821
        result[currPos++] = 's';
822
823
824
825
        break;
      case VTK_PARSE_UNICODE_STRING:
        result[currPos++] = 'O';
        break;
826
827
828
829
830
831
832
      }
    }

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

833
834
/* -------------------------------------------------------------------- */
/* Create a string to describe the signature of a method.
835
836
837
838
839
 * 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". */
840
841

static char *vtkWrapPython_ArgCheckString(
842
  int isvtkobjmethod, FunctionInfo *currentFunction)
843
844
845
846
847
848
{
  static char result[1024];
  int currPos = 0;
  int argtype;
  int i;

849
850
851
852
853
  if (isvtkobjmethod)
    {
    result[currPos++] = '@';
    }

854
855
856
857
858
859
860
861
862
863
864
865
866
  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);

867
868
869
870
871
872
    if (argtype == VTK_PARSE_BOOL)
      {
      strcpy(&result[currPos], " bool");
      currPos += 5;
      }

873
874
875
876
877
878
    if (argtype == VTK_PARSE_UNICODE_STRING)
      {
      strcpy(&result[currPos], " unicode");
      currPos += 8;
      }

879
880
881
882
883
    if (argtype == VTK_PARSE_VTK_OBJECT_REF ||
        argtype == VTK_PARSE_VTK_OBJECT_PTR ||
        argtype == VTK_PARSE_VTK_OBJECT)
      {
      result[currPos++] = ' ';
884
885
886
887
888
889
890
891
      if (argtype == VTK_PARSE_VTK_OBJECT_REF)
        {
        result[currPos++] = '&';
        }
      else if (argtype == VTK_PARSE_VTK_OBJECT_PTR)
        {
        result[currPos++] = '*';
        }
892
893
894
895
896
897
898
899
      strcpy(&result[currPos], currentFunction->ArgClasses[i]);
      currPos += strlen(currentFunction->ArgClasses[i]);
      }
    }

  return result;
}

900
901
902
903
/* -------------------------------------------------------------------- */
/* The method signatures are for the python docstrings. */

static void vtkWrapPython_AddToSignature(char *sig, const char *add, int *i)
904
{
905
906
907
908
909
910
911
912
913
914
915
916
  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;
917
918
}

919
920
921
922
923
/* -------------------------------------------------------------------- */
/* Create a signature for the python version of a method, and
 * write it directly to currentFunction->Signature */

static void vtkWrapPython_Signature(FunctionInfo *currentFunction)
924
925
926
927
928
929
930
{
  static char result[1024];
  int currPos = 0;
  int argtype;
  int i, j;

  /* print out the name of the method */
931
932
  vtkWrapPython_AddToSignature(result,"V.",&currPos);
  vtkWrapPython_AddToSignature(result,currentFunction->Name,&currPos);
933
934

  /* print the arg list */
935
  vtkWrapPython_AddToSignature(result,"(",&currPos);
936

937
938
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
939
    if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
940
      {
941
      vtkWrapPython_AddToSignature(result,"function",&currPos);
942
      }
943

944
    argtype = (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
945
946
947

    if (i != 0)
      {
948
      vtkWrapPython_AddToSignature(result,", ",&currPos);
949
950
951
952
      }

    switch (argtype)
      {
953
954
      case VTK_PARSE_FLOAT_PTR:
      case VTK_PARSE_DOUBLE_PTR:
955
        vtkWrapPython_AddToSignature(result,"(",&currPos);
956
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
Ken Martin's avatar
Ken Martin committed
957
958
959
          {
          if (j != 0)
            {
960
            vtkWrapPython_AddToSignature(result,", ",&currPos);
Ken Martin's avatar
Ken Martin committed
961
            }
962
          vtkWrapPython_AddToSignature(result,"float",&currPos);
Ken Martin's avatar
Ken Martin committed
963
          }
964
        vtkWrapPython_AddToSignature(result,")",&currPos);
Ken Martin's avatar
Ken Martin committed
965
        break;
David Gobbi's avatar