vtkPythonArgs.cxx 33.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPythonArgs.cxx

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

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

=========================================================================*/
/*-----------------------------------------------------------------------
16
The vtkPythonArgs class was created in Oct 2010 by David Gobbi.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

This class provides methods for reading an argument tuple from Python
and converting it to types that can be used by VTK.  It is meant to be
more efficient and flexible that the original PyArg_ParseTuple() code,
resulting in wrapper code that is faster and more compact.
-----------------------------------------------------------------------*/

#include "vtkPythonArgs.h"
#include "vtkPythonUtil.h"

#include "vtkObjectBase.h"

//--------------------------------------------------------------------
// Extract various C++ types from python objects.  The rules are
// identical to PyArg_ParseTuple except that range checking is done
// on unsigned values.

// Macro to mimic a check done in PyArg_ParseTuple
#define VTK_PYTHON_FLOAT_CHECK()\
36
  if (PyFloat_Check(o)) \
37
    { \
38
    PyErr_SetString(PyExc_TypeError, \
39
                      "integer argument expected, got float"); \
40
    return false; \
41
42
    }

43
44
inline
bool vtkPythonGetValue(PyObject *o, long &a)
45
46
47
{
  VTK_PYTHON_FLOAT_CHECK();

48
  a = PyInt_AsLong(o);
49
  return (a != static_cast<long>(-1) || !PyErr_Occurred());
50
51
}

52
53
inline
bool vtkPythonGetValue(PyObject *o, unsigned long &a)
54
55
56
{
  VTK_PYTHON_FLOAT_CHECK();

57
  a = PyLong_AsUnsignedLong(o);
58
  return (a != static_cast<unsigned long>(-1) || !PyErr_Occurred());
59
60
61
}

template <class T> inline
62
bool vtkPythonGetLongLongValue(PyObject *o, T &a)
63
64
65
66
67
{
  VTK_PYTHON_FLOAT_CHECK();

  PY_LONG_LONG i = PyLong_AsLongLong(o);
  a = static_cast<T>(i);
68
  return (i != static_cast<PY_LONG_LONG>(-1) || !PyErr_Occurred());
69
70
71
}

template <class T> inline
72
bool vtkPythonGetUnsignedLongLongValue(PyObject *o, T &a)
73
74
75
{
  VTK_PYTHON_FLOAT_CHECK();

76
77
  // PyLong_AsUnsignedLongLong will fail if "o" is not a PyLong
  if (PyLong_Check(o))
78
    {
79
80
81
    unsigned PY_LONG_LONG i = PyLong_AsUnsignedLongLong(o);
    a = static_cast<T>(i);
    return (i != static_cast<unsigned PY_LONG_LONG>(-1) || !PyErr_Occurred());
82
    }
83
84
85
86

  unsigned long l = PyLong_AsUnsignedLong(o);
  a = static_cast<T>(l);
  return (l != static_cast<unsigned long>(-1) || !PyErr_Occurred());
87
88
89
90
}


template <class T> inline
91
bool vtkPythonGetStringValue(PyObject *o, T *&a, const char *exctext)
92
{
93
  if (PyBytes_Check(o))
94
    {
95
    a = PyBytes_AS_STRING(o);
96
    return true;
97
98
99
100
    }
#ifdef Py_USING_UNICODE
  else if (PyUnicode_Check(o))
    {
101
#if PY_VERSION_HEX >= 0x03030000
102
103
104
    a = PyUnicode_AsUTF8(o);
    return true;
#else
105
106
107
    PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
    if (s)
      {
108
      a = PyBytes_AS_STRING(s);
109
      return true;
110
      }
111
112

    exctext = "(unicode conversion error)";
113
#endif
114
115
    }
#endif
116
117
118

  PyErr_SetString(PyExc_TypeError, exctext);
  return false;
119
120
}

121
122
inline bool vtkPythonGetStdStringValue(PyObject *o, std::string &a, const char *exctext)
{
123
  if (PyBytes_Check(o))
124
125
126
    {
    char* val;
    Py_ssize_t len;
127
    PyBytes_AsStringAndSize(o, &val, &len);
128
129
130
131
132
133
    a = std::string(val, len);
    return true;
    }
#ifdef Py_USING_UNICODE
  else if (PyUnicode_Check(o))
    {
134
#if PY_VERSION_HEX >= 0x03030000
135
136
137
138
139
    Py_ssize_t len;
    const char* val = PyUnicode_AsUTF8AndSize(o, &len);
    a = std::string(val, len);
    return true;
#else
140
141
142
143
144
    PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
    if (s)
      {
      char* val;
      Py_ssize_t len;
145
      PyBytes_AsStringAndSize(s, &val, &len);
146
147
148
149
150
      a = std::string(val, len);
      return true;
      }

    exctext = "(unicode conversion error)";
151
#endif
152
153
154
155
156
157
158
    }
#endif

  PyErr_SetString(PyExc_TypeError, exctext);
  return false;
}

159
160
161
//--------------------------------------------------------------------
// Overloaded methods, mostly based on the above templates

162
163
static bool vtkPythonGetValue(
  PyObject *o, const void *&a, Py_buffer *view)
164
{
165
166
  void *p = 0;
  Py_ssize_t sz = 0;
167
168
#ifndef VTK_PY3K
  const char *format = 0;
169
  PyBufferProcs *b = Py_TYPE(o)->tp_as_buffer;
170
#endif
171

172
173
174
175
176
177
178
179
180
181
182
183
#if PY_VERSION_HEX < 0x02060000
  (void)view;
#else
#ifdef VTK_PY3K
  PyObject *bytes = NULL;
  if (PyUnicode_Check(o))
    {
    bytes = PyUnicode_AsUTF8String(o);
    PyBytes_AsStringAndSize(bytes, reinterpret_cast<char **>(&p), &sz);
    }
  else
#endif
184
185
  if (PyObject_CheckBuffer(o))
    {
186
    // use the new buffer interface
187
188
189
190
191
192
    if (PyObject_GetBuffer(o, view, PyBUF_SIMPLE) == -1)
      {
      return false;
      }
    p = view->buf;
    sz = view->len;
193
#ifndef VTK_PY3K
194
    format = view->format;
195
#endif
196
    }
197
#ifndef VTK_PY3K
198
199
  else
#endif
200
201
#endif
#ifndef VTK_PY3K
202
  // use the old buffer interface
203
204
205
206
  if (b && b->bf_getreadbuffer && b->bf_getsegcount)
    {
    if (b->bf_getsegcount(o, NULL) == 1)
      {
207
208
209
210
211
212
213
214
      sz = b->bf_getreadbuffer(o, 0, &p);
      }
    else
      {
      PyErr_SetString(PyExc_TypeError, "buffer must be single-segment");
      return false;
      }
    }
215
#endif
216

217
218
219
#ifdef VTK_PY3K
  if (bytes)
#else
220
221
  if (p && sz >= 0 && sz <= VTK_INT_MAX &&
      (format == 0 || format[0] == 'c' || format[0] == 'B'))
222
#endif
223
224
225
226
227
    {
    // check for pointer mangled as string
    int s = static_cast<int>(sz);
    a = vtkPythonUtil::UnmanglePointer(
      reinterpret_cast<char *>(p), &s, "p_void");
228
229
#ifdef VTK_PY3K
    Py_DECREF(bytes);
230
    if (s != 0)
231
      {
232
      PyErr_SetString(PyExc_TypeError, "requires a _addr_p_void string");
233
234
      return false;
      }
235
236
#else
    if (s == -1)
237
      {
238
239
240
      // matched _addr_ but not p_void, assume it isn't a swig ptr string:
      // use the buffer's pointer as the argument
      a = p;
241
      }
242
243
#endif
    return true;
244
    }
245
246
247
248
249
250
251
252
253
  else if (p && sz >= 0)
    {
    // directly use the pointer to the buffer contents
    a = p;
    return true;
    }

  PyErr_SetString(PyExc_TypeError,
    "object does not have a readable buffer");
254
  return false;
255
256
257
}

inline
258
bool vtkPythonGetValue(PyObject *o, void *&a, Py_buffer *buf)
259
260
261
{
  // should have an alternate form for non-const "void *" that uses
  // writebuffer instead of readbuffer, but that would break existing code
262
  const void *b = NULL;
263
  bool r = vtkPythonGetValue(o, b, buf);
264
265
266
267
268
269
  a = const_cast<void *>(b);
  return r;
}


inline
270
bool vtkPythonGetValue(PyObject *o, const char *&a)
271
272
273
{
  a = NULL;

274
275
  return (o == Py_None ||
          vtkPythonGetStringValue(o, a, "string or None required"));
276
277
278
}

inline
279
bool vtkPythonGetValue(PyObject *o, char *&a)
280
281
282
{
  a = NULL;

283
284
  return (o == Py_None ||
          vtkPythonGetStringValue(o, a, "string or None required"));
285
286
287
}

inline
288
bool vtkPythonGetValue(PyObject *o, std::string &a)
289
{
290
  if (vtkPythonGetStdStringValue(o, a, "string is required"))
291
    {
292
    return true;
293
    }
294
  return false;
295
296
297
}

inline
298
bool vtkPythonGetValue(PyObject *o, vtkUnicodeString &a)
299
300
301
302
303
{
#ifdef Py_USING_UNICODE
  PyObject *s = PyUnicode_AsUTF8String(o);
  if (s)
    {
304
    a = vtkUnicodeString::from_utf8(PyBytes_AS_STRING(s));
305
    Py_DECREF(s);
306
    return true;
307
    }
308
  return false;
309
310
311
#else
  a.clear();
  PyErr_SetString(PyExc_TypeError, "python built without unicode support");
312
  return false;
313
314
315
316
#endif
}

inline
317
bool vtkPythonGetValue(PyObject *o, char &a)
318
{
319
  static const char exctext[] = "a string of length 1 is required";
320
  const char *b;
321
  if (vtkPythonGetStringValue(o, b, exctext))
322
    {
323
324
325
326
327
328
    if (b[0] == '\0' || b[1] == '\0')
      {
      a = b[0];
      return true;
      }
    PyErr_SetString(PyExc_TypeError, exctext);
329
    }
330
  return false;
331
332
333
}

inline
334
bool vtkPythonGetValue(PyObject *o, bool &a)
335
336
337
{
  int i = PyObject_IsTrue(o);
  a = (i != 0);
338
  return (i != -1);
339
340
341
}

inline
342
bool vtkPythonGetValue(PyObject *o, float &a)
343
{
344
  a = static_cast<float>(PyFloat_AsDouble(o));
345
  return (a != -1.0f || !PyErr_Occurred());
346
347
348
}

inline
349
bool vtkPythonGetValue(PyObject *o, double &a)
350
{
351
  a = PyFloat_AsDouble(o);
352
  return (a != -1.0f || !PyErr_Occurred());
353
354
355
}

inline
356
bool vtkPythonGetValue(PyObject *o, signed char &a)
357
358
{
  long i = 0;
359
  if (vtkPythonGetValue(o, i))
360
    {
361
362
    a = static_cast<signed char>(i);
    if (i >= VTK_SIGNED_CHAR_MIN && i <= VTK_SIGNED_CHAR_MAX)
363
      {
364
      return true;
365
      }
366
367
    PyErr_SetString(PyExc_OverflowError,
                    "value is out of range for signed char");
368
    }
369
  return false;
370
371
372
}

inline
373
bool vtkPythonGetValue(PyObject *o, unsigned char &a)
374
375
{
  long i = 0;
376
  if (vtkPythonGetValue(o, i))
377
    {
378
379
    a = static_cast<unsigned char>(i);
    if (i >= VTK_UNSIGNED_CHAR_MIN && i <= VTK_UNSIGNED_CHAR_MAX)
380
      {
381
      return true;
382
      }
383
384
    PyErr_SetString(PyExc_OverflowError,
                    "value is out of range for unsigned char");
385
    }
386
  return false;
387
388
389
}

inline
390
bool vtkPythonGetValue(PyObject *o, short &a)
391
392
{
  long i = 0;
393
  if (vtkPythonGetValue(o, i))
394
    {
395
396
    a = static_cast<short>(i);
    if (i >= VTK_SHORT_MIN && i <= VTK_SHORT_MAX)
397
      {
398
      return true;
399
      }
400
401
    PyErr_SetString(PyExc_OverflowError,
                    "value is out of range for short");
402
    }
403
  return false;
404
405
406
}

inline
407
bool vtkPythonGetValue(PyObject *o, unsigned short &a)
408
409
{
  long i = 0;
410
  if (vtkPythonGetValue(o, i))
411
    {
412
413
    a = static_cast<unsigned short>(i);
    if (i >= VTK_UNSIGNED_SHORT_MIN && i <= VTK_UNSIGNED_SHORT_MAX)
414
      {
415
      return true;
416
      }
417
418
    PyErr_SetString(PyExc_OverflowError,
                    "value is out of range for unsigned short");
419
    }
420
  return false;
421
422
}

423

424
inline
425
bool vtkPythonGetValue(PyObject *o, int &a)
426
427
{
  long i = 0;
428
  if (vtkPythonGetValue(o, i))
429
    {
430
431
432
    a = static_cast<int>(i);
#if VTK_SIZEOF_INT < VTK_SIZEOF_LONG
    if (i >= VTK_INT_MIN && i <= VTK_INT_MAX)
433
      {
434
      return true;
435
      }
436
437
438
439
    PyErr_SetString(PyExc_OverflowError,
                    "value is out of range for int");
#else
    return true;
440
#endif
441
442
    }
  return false;
443
444
445
}

inline
446
bool vtkPythonGetValue(PyObject *o, unsigned int &a)
447
448
449
{
#if VTK_SIZEOF_INT < VTK_SIZEOF_LONG
  long i = 0;
450
  if (vtkPythonGetValue(o, i))
451
    {
452
453
    a = static_cast<unsigned int>(i);
    if (i >= VTK_UNSIGNED_INT_MIN && i <= VTK_UNSIGNED_INT_MAX)
454
      {
455
      return true;
456
      }
457
458
    PyErr_SetString(PyExc_OverflowError,
                    "value is out of range for unsigned int");
459
    }
460
  return false;
461
462
#else
  unsigned long i = 0;
463
  if (vtkPythonGetValue(o, i))
464
465
466
467
468
    {
    a = static_cast<unsigned int>(i);
    return true;
    }
  return false;
469
470
471
472
473
#endif
}

#ifdef VTK_TYPE_USE_LONG_LONG
inline
474
bool vtkPythonGetValue(PyObject *o, long long &a)
475
{
476
  return vtkPythonGetLongLongValue(o, a);
477
478
479
}

inline
480
bool vtkPythonGetValue(PyObject *o, unsigned long long &a)
481
{
482
  return vtkPythonGetUnsignedLongLongValue(o, a);
483
484
485
486
487
}
#endif

#ifdef VTK_TYPE_USE___INT64
inline
488
bool vtkPythonGetValue(PyObject *o, __int64 &a)
489
{
490
  return vtkPythonGetLongLongValue(o, a);
491
492
493
}

inline
494
bool vtkPythonGetValue(PyObject *o, unsigned __int64 &a)
495
{
496
  return vtkPythonGetUnsignedLongLongValue(o, i);
497
498
499
500
501
502
503
504
}
#endif


//--------------------------------------------------------------------
// Method for setting a C++ array from a Python sequence.

static
505
bool vtkPythonSequenceError(PyObject *o, Py_ssize_t n, Py_ssize_t m);
506
507

template<class T> inline
508
bool vtkPythonGetArray(PyObject *o, T *a, int n)
509
510
511
512
513
514
515
516
517
518
{
  if (a)
    {
    Py_ssize_t m = n;

    if (PyTuple_Check(o))
      {
      m = PyTuple_GET_SIZE(o);
      if (m == n)
        {
519
520
        bool r = true;
        for (int i = 0; i < n && r; i++)
521
522
523
524
525
526
527
528
529
530
531
532
          {
          PyObject *s = PyTuple_GET_ITEM(o, i);
          r = vtkPythonGetValue(s, a[i]);
          }
        return r;
        }
      }
    else if (PyList_Check(o))
      {
      m = PyList_GET_SIZE(o);
      if (m == n)
        {
533
534
        bool r = true;
        for (int i = 0; i < n && r; i++)
535
536
537
538
539
540
541
542
543
544
545
546
          {
          PyObject *s = PyList_GET_ITEM(o, i);
          r = vtkPythonGetValue(s, a[i]);
          }
        return r;
        }
      }
    else if (PySequence_Check(o))
      {
      m = PySequence_Size(o);
      if (m == n)
        {
547
        bool r = true;
548
        for (int i = 0; i < n && r; i++)
549
          {
550
          r = false;
551
          PyObject *s = PySequence_GetItem(o, i);
552
          if (s && vtkPythonGetValue(s, a[i]))
553
554
            {
            Py_DECREF(s);
555
            r = true;
556
557
558
559
560
561
562
563
564
            }
          }
        return r;
        }
      }

    return vtkPythonSequenceError(o, n, m);
    }

565
  return true;
566
567
568
569
570
571
}

//--------------------------------------------------------------------
// Method for setting an n-dimensional C++ arrays from a Python sequence.

template<class T>
572
bool vtkPythonGetNArray(PyObject *o, T *a, int ndim, const int *dims)
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
{
  if (a)
    {
    int inc = 1;
    for (int j = 1; j < ndim; j++)
      {
      inc *= dims[j];
      }

    int n = dims[0];
    Py_ssize_t m = n;

    if (PyList_Check(o))
      {
      m = PyList_GET_SIZE(o);
      if (m == n)
        {
590
        bool r = true;
591
592
        if (ndim > 1)
          {
593
          for (int i = 0; i < n && r; i++)
594
595
596
597
598
599
600
601
            {
            PyObject *s = PyList_GET_ITEM(o, i);
            r = vtkPythonGetNArray(s, a, ndim-1, dims+1);
            a += inc;
            }
          }
        else
          {
602
          for (int i = 0; i < n && r; i++)
603
604
605
606
607
608
609
610
611
612
613
614
615
            {
            PyObject *s = PyList_GET_ITEM(o, i);
            r = vtkPythonGetValue(s, a[i]);
            }
          }
        return r;
        }
      }
    else if (PySequence_Check(o))
      {
      m = PySequence_Size(o);
      if (m == n)
        {
616
617
        bool r = true;
        for (int i = 0; i < n && r; i++)
618
          {
619
          r = false;
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
          PyObject *s = PySequence_GetItem(o, i);
          if (s)
            {
            if (ndim > 1)
              {
              r = vtkPythonGetNArray(s, a, ndim-1, dims+1);
              a += inc;
              }
            else
              {
              r = vtkPythonGetValue(s, a[i]);
              }
            Py_DECREF(s);
            }
          }
        return r;
        }
      }

    return vtkPythonSequenceError(o, n, m);
    }

642
  return true;
643
644
645
646
647
648
}

//--------------------------------------------------------------------
// Method for setting a python sequence from a C++ array

template<class T> inline
649
bool vtkPythonSetArray(PyObject *o, const T *a, int n)
650
651
652
653
654
655
656
657
658
659
{
  if (a)
    {
    Py_ssize_t m = n;

    if (PyList_Check(o))
      {
      m = PyList_GET_SIZE(o);
      if (m == n)
        {
660
661
        bool r = true;
        for (int i = 0; i < n && r; i++)
662
          {
663
          r = false;
664
665
666
          PyObject *s = vtkPythonArgs::BuildValue(a[i]);
          if (s)
            {
667
            Py_DECREF(PyList_GET_ITEM(o, i));
668
            PyList_SET_ITEM(o, i, s);
669
            r = true;
670
671
672
673
674
675
676
677
678
679
            }
          }
        return r;
        }
      }
    else if (PySequence_Check(o))
      {
      m = PySequence_Size(o);
      if (m == n)
        {
680
681
        bool r = true;
        for (int i = 0; i < n && r; i++)
682
          {
683
          r = false;
684
685
686
          PyObject *s = vtkPythonArgs::BuildValue(a[i]);
          if (s)
            {
687
            r = (PySequence_SetItem(o, i, s) != -1);
688
689
690
691
692
693
694
695
696
697
            Py_DECREF(s);
            }
          }
        return r;
        }
      }

    return vtkPythonSequenceError(o, n, m);
    }

698
  return true;
699
700
701
702
703
704
}

//--------------------------------------------------------------------
// Method for setting a python array from an n-dimensional C++ array

template<class T>
705
bool vtkPythonSetNArray(
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
  PyObject *o, const T *a, int ndim, const int *dims)
{
  if (a)
    {
    int inc = 1;
    for (int j = 1; j < ndim; j++)
      {
      inc *= dims[j];
      }

    int n = dims[0];
    Py_ssize_t m = n;

    if (PyList_Check(o))
      {
      m = PyList_GET_SIZE(o);
      if (m == n)
        {
724
        bool r = true;
725
726
        if (ndim > 1)
          {
727
          for (int i = 0; i < n && r; i++)
728
729
730
731
732
733
734
735
            {
            PyObject *s = PyList_GET_ITEM(o, i);
            r = vtkPythonSetNArray(s, a, ndim-1, dims+1);
            a += inc;
            }
          }
        else
          {
736
          for (int i = 0; i < n && r; i++)
737
            {
738
            r = false;
739
740
741
            PyObject *s = vtkPythonArgs::BuildValue(a[i]);
            if (s)
              {
742
              Py_DECREF(PyList_GET_ITEM(o, i));
743
              PyList_SET_ITEM(o, i, s);
744
              r = true;
745
746
747
748
749
750
751
752
753
754
755
              }
            }
          }
        return r;
        }
      }
    else if (PySequence_Check(o))
      {
      m = PySequence_Size(o);
      if (m == n)
        {
756
        bool r = true;
757
758
        if (ndim > 1)
          {
759
          for (int i = 0; i < n && r; i++)
760
            {
761
            r = false;
762
763
764
765
766
767
768
769
770
771
772
            PyObject *s = PySequence_GetItem(o, i);
            if (s)
              {
              r = vtkPythonSetNArray(s, a, ndim-1, dims+1);
              a += inc;
              Py_DECREF(s);
              }
            }
          }
        else
          {
773
          for (int i = 0; i < n && r; i++)
774
            {
775
            r = false;
776
777
778
            PyObject *s = vtkPythonArgs::BuildValue(a[i]);
            if (s)
              {
779
              r = (PySequence_SetItem(o, i, s) != -1);
780
781
782
783
784
785
786
787
788
789
790
              Py_DECREF(s);
              }
            }
          }
        return r;
        }
      }

    return vtkPythonSequenceError(o, n, m);
    }

791
  return true;
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
}

//--------------------------------------------------------------------
// Define all the "BuildValue" array methods defined in the class.

template<class T> inline
PyObject *vtkPythonBuildTuple(const T *a, int n)
{
  if (a)
    {
    PyObject *t = PyTuple_New(n);
    for (int i = 0; i < n; i++)
      {
      PyObject *o = vtkPythonArgs::BuildValue(a[i]);
      PyTuple_SET_ITEM(t, i, o);
      }
    return t;
    }

  Py_INCREF(Py_None);
  return Py_None;
}

#define VTK_PYTHON_BUILD_TUPLE(T) \
PyObject *vtkPythonArgs::BuildTuple(const T *a, int n) \
{ \
  return vtkPythonBuildTuple(a, n); \
}

VTK_PYTHON_BUILD_TUPLE(bool)
VTK_PYTHON_BUILD_TUPLE(float)
VTK_PYTHON_BUILD_TUPLE(double)
VTK_PYTHON_BUILD_TUPLE(signed char)
VTK_PYTHON_BUILD_TUPLE(unsigned char)
VTK_PYTHON_BUILD_TUPLE(short)
VTK_PYTHON_BUILD_TUPLE(unsigned short)
VTK_PYTHON_BUILD_TUPLE(int)
VTK_PYTHON_BUILD_TUPLE(unsigned int)
VTK_PYTHON_BUILD_TUPLE(long)
VTK_PYTHON_BUILD_TUPLE(unsigned long)
#ifdef VTK_TYPE_USE_LONG_LONG
VTK_PYTHON_BUILD_TUPLE(long long)
VTK_PYTHON_BUILD_TUPLE(unsigned long long)
#endif
#ifdef VTK_TYPE_USE___INT64
VTK_PYTHON_BUILD_TUPLE(__int64)
VTK_PYTHON_BUILD_TUPLE(unsigned __int64)
#endif

//--------------------------------------------------------------------
// If "self" is a class, get real "self" from arg list
843
PyObject *vtkPythonArgs::GetSelfFromFirstArg(
844
845
  PyObject *self, PyObject *args)
{
846
  if (PyType_Check(self))
847
    {
848
    PyTypeObject *pytype = (PyTypeObject *)self;
849
850
851
    if (PyTuple_GET_SIZE(args) > 0)
      {
      self = PyTuple_GET_ITEM(args, 0);
852
      if (PyObject_TypeCheck(self, pytype))
853
        {
854
        return self;
855
856
857
858
859
        }
      }

    char buf[256];
    sprintf(buf, "unbound method requires a %.200s as the first argument",
860
            pytype->tp_name);
861
862
863
864
865
866
867
868
869
870
871
    PyErr_SetString(PyExc_TypeError, buf);
    return NULL;
    }

  PyErr_SetString(PyExc_TypeError, "unbound method requires a vtkobject");
  return NULL;
}

//--------------------------------------------------------------------
// Define the GetArg methods for getting objects

872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
PyObject *vtkPythonArgs::GetArgAsPythonObject(
  bool &valid)
{
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
  valid = true;
  return o;
}

PyObject *vtkPythonArgs::GetArgAsPythonObject(
  PyObject *o, bool &valid)
{
  valid = true;
  return o;
}

887
888
889
890
vtkObjectBase *vtkPythonArgs::GetArgAsVTKObject(
  const char *classname, bool &valid)
{
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
891
892
  vtkObjectBase *r = vtkPythonArgs::GetArgAsVTKObject(o, classname, valid);
  if (!valid)
893
    {
894
    this->RefineArgTypeError(this->I - this->M - 1);
895
896
897
898
    }
  return r;
}

899
900
901
902
903
904
905
906
907
908
vtkObjectBase *vtkPythonArgs::GetArgAsVTKObject(
  PyObject *o, const char *classname, bool &valid)
{
  vtkObjectBase *r = vtkPythonUtil::GetPointerFromObject(o, classname);
  valid = (r || o == Py_None);
  return r;
}

void *vtkPythonArgs::GetArgAsSpecialObject(
  const char *classname, PyObject **p)
909
910
{
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
911
912
  void *r = vtkPythonArgs::GetArgAsSpecialObject(o, classname, p);
  if (r == NULL)
913
    {
914
    this->RefineArgTypeError(this->I - this->M - 1);
915
916
917
918
    }
  return r;
}

919
920
921
922
923
924
925
void *vtkPythonArgs::GetArgAsSpecialObject(
  PyObject *o, const char *classname, PyObject **p)
{
  void *r = vtkPythonUtil::GetPointerFromSpecialObject(o, classname, p);
  return r;
}

926
int vtkPythonArgs::GetArgAsEnum(const char *enumname, bool &valid)
927
928
{
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
929
  int i = vtkPythonArgs::GetArgAsEnum(o, enumname, valid);
930
931
932
933
934
935
  if (!valid)
    {
    this->RefineArgTypeError(this->I - this->M - 1);
    }
  return i;
}
936

937
int vtkPythonArgs::GetArgAsEnum(
938
  PyObject *o, const char *enumname, bool &valid)
939
{
940
  long i = 0;
941
942
  PyTypeObject *pytype = vtkPythonUtil::FindEnum(enumname);
  if (pytype && PyObject_TypeCheck(o, pytype))
943
944
945
946
947
948
949
    {
    i = PyInt_AsLong(o);
    valid = true;
    }
  else
    {
    std::string errstring = "expected enum ";
950
    errstring += enumname;
951
    errstring += ", got ";
952
    errstring += Py_TYPE(o)->tp_name;
953
954
955
956
    PyErr_SetString(PyExc_TypeError, errstring.c_str());
    valid = false;
    }
  return i;
957
958
959
960
961
962
}


//--------------------------------------------------------------------
// Define the methods for SIP objects

963
void *vtkPythonArgs::GetArgAsSIPObject(const char *classname, bool &valid)
964
965
{
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
966
967
  void *r = vtkPythonArgs::GetArgAsSIPObject(o, classname, valid);
  if (!valid)
968
    {
969
    this->RefineArgTypeError(this->I - this->M - 1);
970
    }
971
972
973
974
975
976
977
978
979
  return r;
}

void *vtkPythonArgs::GetArgAsSIPObject(
  PyObject *o, const char *classname, bool &valid)
{
  void *r = vtkPythonUtil::SIPGetPointerFromObject(o, classname);
  valid = (r || !PyErr_Occurred());
  return (valid ? r : NULL);
980
981
}

982
int vtkPythonArgs::GetArgAsSIPEnum(const char *classname, bool &valid)
983
984
{
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
985
986
  int i = vtkPythonArgs::GetArgAsSIPEnum(o, classname, valid);
  if (!valid)
987
    {
988
    this->RefineArgTypeError(this->I - this->M - 1);
989
    }
990
991
992
993
994
995
996
997
998
999
  return i;
}

int vtkPythonArgs::GetArgAsSIPEnum(
  PyObject *o, const char *classname, bool &valid)
{
  int i = 0;
  valid = (vtkPythonUtil::SIPGetPointerFromObject(o, classname) &&
           vtkPythonGetValue(o, i));
  return (valid ? i : 0);
1000
1001
1002
1003
1004
1005
1006
1007
1008
}

//--------------------------------------------------------------------
// Define all the "GetValue" methods in the class.

#define VTK_PYTHON_GET_ARG(T) \
bool vtkPythonArgs::GetValue(T &a) \
{ \
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++); \
1009
1010
1011
1012
  if (PyVTKMutableObject_Check(o)) \
    { \
    o = PyVTKMutableObject_GetValue(o); \
    } \
1013
  if (vtkPythonGetValue(o, a)) \
1014
1015
1016
1017
1018
    { \
    return true; \
    } \
  this->RefineArgTypeError(this->I - this->M - 1); \
  return false; \
1019
1020
1021
1022
1023
} \
 \
bool vtkPythonArgs::GetValue(PyObject *o, T &a) \
{ \
  return vtkPythonGetValue(o, a); \
1024
1025
1026
1027
}

VTK_PYTHON_GET_ARG(char *)
VTK_PYTHON_GET_ARG(const char *)
1028
VTK_PYTHON_GET_ARG(std::string)
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
VTK_PYTHON_GET_ARG(vtkUnicodeString)
VTK_PYTHON_GET_ARG(char)
VTK_PYTHON_GET_ARG(bool)
VTK_PYTHON_GET_ARG(float)
VTK_PYTHON_GET_ARG(double)
VTK_PYTHON_GET_ARG(signed char)
VTK_PYTHON_GET_ARG(unsigned char)
VTK_PYTHON_GET_ARG(short)
VTK_PYTHON_GET_ARG(unsigned short)
VTK_PYTHON_GET_ARG(int)
VTK_PYTHON_GET_ARG(unsigned int)
VTK_PYTHON_GET_ARG(long)
VTK_PYTHON_GET_ARG(unsigned long)
#ifdef VTK_TYPE_USE_LONG_LONG
VTK_PYTHON_GET_ARG(long long)
VTK_PYTHON_GET_ARG(unsigned long long)
#endif
#ifdef VTK_TYPE_USE___INT64
VTK_PYTHON_GET_ARG(__int64)
VTK_PYTHON_GET_ARG(unsigned __int64)
#endif

//--------------------------------------------------------------------
// Define all the GetArray methods in the class.

#define VTK_PYTHON_GET_ARRAY_ARG(T) \
bool vtkPythonArgs::GetArray(T *a, int n) \
{ \
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++); \
1058
  if (vtkPythonGetArray(o, a, n)) \
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
    { \
    return true; \
    } \
  this->RefineArgTypeError(this->I - this->M - 1); \
  return false; \
}

VTK_PYTHON_GET_ARRAY_ARG(bool)
VTK_PYTHON_GET_ARRAY_ARG(float)
VTK_PYTHON_GET_ARRAY_ARG(double)
1069
VTK_PYTHON_GET_ARRAY_ARG(char)
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
VTK_PYTHON_GET_ARRAY_ARG(signed char)
VTK_PYTHON_GET_ARRAY_ARG(unsigned char)
VTK_PYTHON_GET_ARRAY_ARG(short)
VTK_PYTHON_GET_ARRAY_ARG(unsigned short)
VTK_PYTHON_GET_ARRAY_ARG(int)
VTK_PYTHON_GET_ARRAY_ARG(unsigned int)
VTK_PYTHON_GET_ARRAY_ARG(long)
VTK_PYTHON_GET_ARRAY_ARG(unsigned long)
#ifdef VTK_TYPE_USE_LONG_LONG
VTK_PYTHON_GET_ARRAY_ARG(long long)
VTK_PYTHON_GET_ARRAY_ARG(unsigned long long)
#endif
#ifdef VTK_TYPE_USE___INT64
VTK_PYTHON_GET_ARRAY_ARG(__int64)
VTK_PYTHON_GET_ARRAY_ARG(unsigned __int64)
#endif

//--------------------------------------------------------------------
// Define all the GetNArray methods in the class.

#define VTK_PYTHON_GET_NARRAY_ARG(T) \
bool vtkPythonArgs::GetNArray(T *a, int ndim, const int *dims) \
{ \
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++); \
1094
  if (vtkPythonGetNArray(o, a, ndim, dims)) \
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
    { \
    return true; \
    } \
  this->RefineArgTypeError(this->I - this->M - 1); \
  return false; \
}

VTK_PYTHON_GET_NARRAY_ARG(bool)
VTK_PYTHON_GET_NARRAY_ARG(float)
VTK_PYTHON_GET_NARRAY_ARG(double)
1105
VTK_PYTHON_GET_NARRAY_ARG(char)
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
VTK_PYTHON_GET_NARRAY_ARG(signed char)
VTK_PYTHON_GET_NARRAY_ARG(unsigned char)
VTK_PYTHON_GET_NARRAY_ARG(short)
VTK_PYTHON_GET_NARRAY_ARG(unsigned short)
VTK_PYTHON_GET_NARRAY_ARG(int)
VTK_PYTHON_GET_NARRAY_ARG(unsigned int)
VTK_PYTHON_GET_NARRAY_ARG(long)
VTK_PYTHON_GET_NARRAY_ARG(unsigned long)
#ifdef VTK_TYPE_USE_LONG_LONG
VTK_PYTHON_GET_NARRAY_ARG(long long)
VTK_PYTHON_GET_NARRAY_ARG(unsigned long long)
#endif
#ifdef VTK_TYPE_USE___INT64
VTK_PYTHON_GET_NARRAY_ARG(__int64)
VTK_PYTHON_GET_NARRAY_ARG(unsigned __int64)
#endif

//--------------------------------------------------------------------
1124
// Define the special function pointer GetValue method
1125

1126
bool vtkPythonArgs::GetFunction(PyObject *arg, PyObject *&o)
1127
{
1128
  o = arg;
1129
1130
1131
1132
1133
1134
1135
1136
  if (o == Py_None || PyCallable_Check(o))
    {
    return true;
    }
  PyErr_SetString(PyExc_TypeError, "a callable object is required");
  return false;
}

1137
1138
1139
1140
1141
1142
bool vtkPythonArgs::GetFunction(PyObject *&o)
{
  PyObject *arg = PyTuple_GET_ITEM(this->Args, this->I++);
  return vtkPythonArgs::GetFunction(arg, o);
}

1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
//--------------------------------------------------------------------
// Define the void pointer GetValue method

#define VTK_PYTHON_GET_BUFFER(T) \
bool vtkPythonArgs::GetBuffer(T &a, Py_buffer *buf) \
{ \
  PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++); \
  if (vtkPythonGetValue(o, a, buf)) \
    { \
    return true; \
    } \
  this->RefineArgTypeError(this->I - this->M - 1); \
  return false; \
} \
 \
bool vtkPythonArgs::GetBuffer(PyObject *o, T &a, Py_buffer *buf) \
{ \
  return vtkPythonGetValue(o, a, buf); \
}

VTK_PYTHON_GET_BUFFER(void *)
VTK_PYTHON_GET_BUFFER(const void *)

1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
//--------------------------------------------------------------------
// Define all the SetArgValue methods for setting reference args

#define VTK_PYTHON_SET_ARG(T) \
bool vtkPythonArgs::SetArgValue(int i, T a) \
{ \
  if (this->M + i < this->N) \
    { \
    PyObject *m = PyTuple_GET_ITEM(this->Args, this->M + i); \
    PyObject *o = vtkPythonArgs::BuildValue(a); \
    int r = PyVTKMutableObject_SetValue(m, o); \
    if (r == 0) \
      { \
      return true; \
      } \
    this->RefineArgTypeError(i); \
    return false; \
    } \
  return true; \
}

1187
VTK_PYTHON_SET_ARG(const std::string &)
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
VTK_PYTHON_SET_ARG(const vtkUnicodeString &)
VTK_PYTHON_SET_ARG(char)
VTK_PYTHON_SET_ARG(bool)
VTK_PYTHON_SET_ARG(float)
VTK_PYTHON_SET_ARG(double)
VTK_PYTHON_SET_ARG(signed char)
VTK_PYTHON_SET_ARG(unsigned char)
VTK_PYTHON_SET_ARG(short)
VTK_PYTHON_SET_ARG(</