1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 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 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 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 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 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 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
|
/*=========================================================================
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.
=========================================================================*/
/*-----------------------------------------------------------------------
The vtkPythonArgs class was created in Oct 2010 by David Gobbi.
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.
-----------------------------------------------------------------------*/
// Keep vtkPythonArgs.h from declaring externs for templates we instantiate
#define vtkPythonArgs_cxx
#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()\
if (PyFloat_Check(o)) \
{ \
PyErr_SetString(PyExc_TypeError, \
"integer argument expected, got float"); \
return false; \
}
inline
bool vtkPythonGetValue(PyObject *o, long &a)
{
VTK_PYTHON_FLOAT_CHECK();
a = PyInt_AsLong(o);
return (a != static_cast<long>(-1) || !PyErr_Occurred());
}
inline
bool vtkPythonGetValue(PyObject *o, unsigned long &a)
{
VTK_PYTHON_FLOAT_CHECK();
a = PyLong_AsUnsignedLong(o);
return (a != static_cast<unsigned long>(-1) || !PyErr_Occurred());
}
template <class T> inline
bool vtkPythonGetLongLongValue(PyObject *o, T &a)
{
VTK_PYTHON_FLOAT_CHECK();
PY_LONG_LONG i = PyLong_AsLongLong(o);
a = static_cast<T>(i);
return (i != static_cast<PY_LONG_LONG>(-1) || !PyErr_Occurred());
}
template <class T> inline
bool vtkPythonGetUnsignedLongLongValue(PyObject *o, T &a)
{
VTK_PYTHON_FLOAT_CHECK();
// PyLong_AsUnsignedLongLong will fail if "o" is not a PyLong
if (PyLong_Check(o))
{
unsigned PY_LONG_LONG i = PyLong_AsUnsignedLongLong(o);
a = static_cast<T>(i);
return (i != static_cast<unsigned PY_LONG_LONG>(-1) || !PyErr_Occurred());
}
unsigned long l = PyLong_AsUnsignedLong(o);
a = static_cast<T>(l);
return (l != static_cast<unsigned long>(-1) || !PyErr_Occurred());
}
template <class T> inline
bool vtkPythonGetStringValue(PyObject *o, T *&a, const char *exctext)
{
if (PyBytes_Check(o))
{
a = PyBytes_AS_STRING(o);
return true;
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(o))
{
#if PY_VERSION_HEX >= 0x03030000
a = const_cast<char*>(PyUnicode_AsUTF8(o));
return true;
#else
PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
if (s)
{
a = PyBytes_AS_STRING(s);
return true;
}
exctext = "(unicode conversion error)";
#endif
}
#endif
PyErr_SetString(PyExc_TypeError, exctext);
return false;
}
inline bool vtkPythonGetStdStringValue(PyObject *o, std::string &a, const char *exctext)
{
if (PyBytes_Check(o))
{
char* val;
Py_ssize_t len;
PyBytes_AsStringAndSize(o, &val, &len);
a = std::string(val, len);
return true;
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(o))
{
#if PY_VERSION_HEX >= 0x03030000
Py_ssize_t len;
const char* val = PyUnicode_AsUTF8AndSize(o, &len);
a = std::string(val, len);
return true;
#else
PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
if (s)
{
char* val;
Py_ssize_t len;
PyBytes_AsStringAndSize(s, &val, &len);
a = std::string(val, len);
return true;
}
exctext = "(unicode conversion error)";
#endif
}
#endif
PyErr_SetString(PyExc_TypeError, exctext);
return false;
}
//--------------------------------------------------------------------
// Overloaded methods, mostly based on the above templates
static bool vtkPythonGetValue(
PyObject *o, const void *&a, Py_buffer *view)
{
void *p = 0;
Py_ssize_t sz = 0;
#ifndef VTK_PY3K
const char *format = 0;
PyBufferProcs *b = Py_TYPE(o)->tp_as_buffer;
#endif
#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
if (PyObject_CheckBuffer(o))
{
// use the new buffer interface
if (PyObject_GetBuffer(o, view, PyBUF_SIMPLE) == -1)
{
return false;
}
p = view->buf;
sz = view->len;
#ifndef VTK_PY3K
format = view->format;
#endif
}
#ifndef VTK_PY3K
else
#endif
#endif
#ifndef VTK_PY3K
// use the old buffer interface
if (b && b->bf_getreadbuffer && b->bf_getsegcount)
{
if (b->bf_getsegcount(o, NULL) == 1)
{
sz = b->bf_getreadbuffer(o, 0, &p);
}
else
{
PyErr_SetString(PyExc_TypeError, "buffer must be single-segment");
return false;
}
}
#endif
#ifdef VTK_PY3K
if (bytes)
#else
if (p && sz >= 0 && sz <= VTK_INT_MAX &&
(format == 0 || format[0] == 'c' || format[0] == 'B'))
#endif
{
// check for pointer mangled as string
int s = static_cast<int>(sz);
a = vtkPythonUtil::UnmanglePointer(
reinterpret_cast<char *>(p), &s, "p_void");
#ifdef VTK_PY3K
Py_DECREF(bytes);
if (s != 0)
{
PyErr_SetString(PyExc_TypeError, "requires a _addr_p_void string");
return false;
}
#else
if (s == -1)
{
// matched _addr_ but not p_void, assume it isn't a swig ptr string:
// use the buffer's pointer as the argument
a = p;
}
#endif
return true;
}
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");
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, void *&a, Py_buffer *buf)
{
// should have an alternate form for non-const "void *" that uses
// writebuffer instead of readbuffer, but that would break existing code
const void *b = NULL;
bool r = vtkPythonGetValue(o, b, buf);
a = const_cast<void *>(b);
return r;
}
inline
bool vtkPythonGetValue(PyObject *o, const char *&a)
{
a = NULL;
return (o == Py_None ||
vtkPythonGetStringValue(o, a, "string or None required"));
}
inline
bool vtkPythonGetValue(PyObject *o, char *&a)
{
a = NULL;
return (o == Py_None ||
vtkPythonGetStringValue(o, a, "string or None required"));
}
inline
bool vtkPythonGetValue(PyObject *o, std::string &a)
{
if (vtkPythonGetStdStringValue(o, a, "string is required"))
{
return true;
}
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, vtkUnicodeString &a)
{
#ifdef Py_USING_UNICODE
PyObject *s = PyUnicode_AsUTF8String(o);
if (s)
{
a = vtkUnicodeString::from_utf8(PyBytes_AS_STRING(s));
Py_DECREF(s);
return true;
}
return false;
#else
a.clear();
PyErr_SetString(PyExc_TypeError, "python built without unicode support");
return false;
#endif
}
inline
bool vtkPythonGetValue(PyObject *o, char &a)
{
static const char exctext[] = "a string of length 1 is required";
const char *b;
if (vtkPythonGetStringValue(o, b, exctext))
{
if (b[0] == '\0' || b[1] == '\0')
{
a = b[0];
return true;
}
PyErr_SetString(PyExc_TypeError, exctext);
}
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, bool &a)
{
int i = PyObject_IsTrue(o);
a = (i != 0);
return (i != -1);
}
inline
bool vtkPythonGetValue(PyObject *o, float &a)
{
a = static_cast<float>(PyFloat_AsDouble(o));
return (a != -1.0f || !PyErr_Occurred());
}
inline
bool vtkPythonGetValue(PyObject *o, double &a)
{
a = PyFloat_AsDouble(o);
return (a != -1.0f || !PyErr_Occurred());
}
inline
bool vtkPythonGetValue(PyObject *o, signed char &a)
{
long i = 0;
if (vtkPythonGetValue(o, i))
{
a = static_cast<signed char>(i);
if (i >= VTK_SIGNED_CHAR_MIN && i <= VTK_SIGNED_CHAR_MAX)
{
return true;
}
PyErr_SetString(PyExc_OverflowError,
"value is out of range for signed char");
}
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, unsigned char &a)
{
long i = 0;
if (vtkPythonGetValue(o, i))
{
a = static_cast<unsigned char>(i);
if (i >= VTK_UNSIGNED_CHAR_MIN && i <= VTK_UNSIGNED_CHAR_MAX)
{
return true;
}
PyErr_SetString(PyExc_OverflowError,
"value is out of range for unsigned char");
}
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, short &a)
{
long i = 0;
if (vtkPythonGetValue(o, i))
{
a = static_cast<short>(i);
if (i >= VTK_SHORT_MIN && i <= VTK_SHORT_MAX)
{
return true;
}
PyErr_SetString(PyExc_OverflowError,
"value is out of range for short");
}
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, unsigned short &a)
{
long i = 0;
if (vtkPythonGetValue(o, i))
{
a = static_cast<unsigned short>(i);
if (i >= VTK_UNSIGNED_SHORT_MIN && i <= VTK_UNSIGNED_SHORT_MAX)
{
return true;
}
PyErr_SetString(PyExc_OverflowError,
"value is out of range for unsigned short");
}
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, int &a)
{
long i = 0;
if (vtkPythonGetValue(o, i))
{
a = static_cast<int>(i);
#if VTK_SIZEOF_INT < VTK_SIZEOF_LONG
if (i >= VTK_INT_MIN && i <= VTK_INT_MAX)
{
return true;
}
PyErr_SetString(PyExc_OverflowError,
"value is out of range for int");
#else
return true;
#endif
}
return false;
}
inline
bool vtkPythonGetValue(PyObject *o, unsigned int &a)
{
#if VTK_SIZEOF_INT < VTK_SIZEOF_LONG
long i = 0;
if (vtkPythonGetValue(o, i))
{
a = static_cast<unsigned int>(i);
if (i >= VTK_UNSIGNED_INT_MIN && i <= VTK_UNSIGNED_INT_MAX)
{
return true;
}
PyErr_SetString(PyExc_OverflowError,
"value is out of range for unsigned int");
}
return false;
#else
unsigned long i = 0;
if (vtkPythonGetValue(o, i))
{
a = static_cast<unsigned int>(i);
return true;
}
return false;
#endif
}
inline
bool vtkPythonGetValue(PyObject *o, long long &a)
{
return vtkPythonGetLongLongValue(o, a);
}
inline
bool vtkPythonGetValue(PyObject *o, unsigned long long &a)
{
return vtkPythonGetUnsignedLongLongValue(o, a);
}
//--------------------------------------------------------------------
// Method for setting a C++ array from a Python sequence.
static
bool vtkPythonSequenceError(PyObject *o, Py_ssize_t n, Py_ssize_t m);
template<class T> inline
bool vtkPythonGetArray(PyObject *o, T *a, int n)
{
if (a)
{
Py_ssize_t m = n;
if (PyTuple_Check(o))
{
m = PyTuple_GET_SIZE(o);
if (m == n)
{
bool r = true;
for (int i = 0; i < n && r; i++)
{
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)
{
bool r = true;
for (int i = 0; i < n && r; i++)
{
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)
{
bool r = true;
for (int i = 0; i < n && r; i++)
{
r = false;
PyObject *s = PySequence_GetItem(o, i);
if (s && vtkPythonGetValue(s, a[i]))
{
Py_DECREF(s);
r = true;
}
}
return r;
}
}
return vtkPythonSequenceError(o, n, m);
}
return true;
}
//--------------------------------------------------------------------
// Method for setting an n-dimensional C++ arrays from a Python sequence.
template<class T>
bool vtkPythonGetNArray(PyObject *o, 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)
{
bool r = true;
if (ndim > 1)
{
for (int i = 0; i < n && r; i++)
{
PyObject *s = PyList_GET_ITEM(o, i);
r = vtkPythonGetNArray(s, a, ndim-1, dims+1);
a += inc;
}
}
else
{
for (int i = 0; i < n && r; i++)
{
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)
{
bool r = true;
for (int i = 0; i < n && r; i++)
{
r = false;
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);
}
return true;
}
//--------------------------------------------------------------------
// Method for setting a python sequence from a C++ array
template<class T> inline
bool vtkPythonSetArray(PyObject *o, const T *a, int n)
{
if (a)
{
Py_ssize_t m = n;
if (PyList_Check(o))
{
m = PyList_GET_SIZE(o);
if (m == n)
{
bool r = true;
for (int i = 0; i < n && r; i++)
{
r = false;
PyObject *s = vtkPythonArgs::BuildValue(a[i]);
if (s)
{
Py_DECREF(PyList_GET_ITEM(o, i));
PyList_SET_ITEM(o, i, s);
r = true;
}
}
return r;
}
}
else if (PySequence_Check(o))
{
m = PySequence_Size(o);
if (m == n)
{
bool r = true;
for (int i = 0; i < n && r; i++)
{
r = false;
PyObject *s = vtkPythonArgs::BuildValue(a[i]);
if (s)
{
r = (PySequence_SetItem(o, i, s) != -1);
Py_DECREF(s);
}
}
return r;
}
}
return vtkPythonSequenceError(o, n, m);
}
return true;
}
//--------------------------------------------------------------------
// Method for setting a python array from an n-dimensional C++ array
template<class T>
bool vtkPythonSetNArray(
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)
{
bool r = true;
if (ndim > 1)
{
for (int i = 0; i < n && r; i++)
{
PyObject *s = PyList_GET_ITEM(o, i);
r = vtkPythonSetNArray(s, a, ndim-1, dims+1);
a += inc;
}
}
else
{
for (int i = 0; i < n && r; i++)
{
r = false;
PyObject *s = vtkPythonArgs::BuildValue(a[i]);
if (s)
{
Py_DECREF(PyList_GET_ITEM(o, i));
PyList_SET_ITEM(o, i, s);
r = true;
}
}
}
return r;
}
}
else if (PySequence_Check(o))
{
m = PySequence_Size(o);
if (m == n)
{
bool r = true;
if (ndim > 1)
{
for (int i = 0; i < n && r; i++)
{
r = false;
PyObject *s = PySequence_GetItem(o, i);
if (s)
{
r = vtkPythonSetNArray(s, a, ndim-1, dims+1);
a += inc;
Py_DECREF(s);
}
}
}
else
{
for (int i = 0; i < n && r; i++)
{
r = false;
PyObject *s = vtkPythonArgs::BuildValue(a[i]);
if (s)
{
r = (PySequence_SetItem(o, i, s) != -1);
Py_DECREF(s);
}
}
}
return r;
}
}
return vtkPythonSequenceError(o, n, m);
}
return true;
}
//--------------------------------------------------------------------
// 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)
VTK_PYTHON_BUILD_TUPLE(long long)
VTK_PYTHON_BUILD_TUPLE(unsigned long long)
//--------------------------------------------------------------------
// If "self" is a class, get real "self" from arg list
PyObject *vtkPythonArgs::GetSelfFromFirstArg(
PyObject *self, PyObject *args)
{
if (PyType_Check(self))
{
PyTypeObject *pytype = (PyTypeObject *)self;
if (PyTuple_GET_SIZE(args) > 0)
{
self = PyTuple_GET_ITEM(args, 0);
if (PyObject_TypeCheck(self, pytype))
{
return self;
}
}
char buf[256];
sprintf(buf, "unbound method requires a %.200s as the first argument",
pytype->tp_name);
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
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;
}
vtkObjectBase *vtkPythonArgs::GetArgAsVTKObject(
const char *classname, bool &valid)
{
PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
vtkObjectBase *r = vtkPythonArgs::GetArgAsVTKObject(o, classname, valid);
if (!valid)
{
this->RefineArgTypeError(this->I - this->M - 1);
}
return r;
}
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)
{
PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
void *r = vtkPythonArgs::GetArgAsSpecialObject(o, classname, p);
if (r == NULL)
{
this->RefineArgTypeError(this->I - this->M - 1);
}
return r;
}
void *vtkPythonArgs::GetArgAsSpecialObject(
PyObject *o, const char *classname, PyObject **p)
{
void *r = vtkPythonUtil::GetPointerFromSpecialObject(o, classname, p);
return r;
}
int vtkPythonArgs::GetArgAsEnum(const char *enumname, bool &valid)
{
PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
int i = vtkPythonArgs::GetArgAsEnum(o, enumname, valid);
if (!valid)
{
this->RefineArgTypeError(this->I - this->M - 1);
}
return i;
}
int vtkPythonArgs::GetArgAsEnum(
PyObject *o, const char *enumname, bool &valid)
{
long i = 0;
PyTypeObject *pytype = vtkPythonUtil::FindEnum(enumname);
if (pytype && PyObject_TypeCheck(o, pytype))
{
i = PyInt_AsLong(o);
valid = true;
}
else
{
std::string errstring = "expected enum ";
errstring += enumname;
errstring += ", got ";
errstring += Py_TYPE(o)->tp_name;
PyErr_SetString(PyExc_TypeError, errstring.c_str());
valid = false;
}
return i;
}
//--------------------------------------------------------------------
// Define the methods for SIP objects
void *vtkPythonArgs::GetArgAsSIPObject(const char *classname, bool &valid)
{
PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
void *r = vtkPythonArgs::GetArgAsSIPObject(o, classname, valid);
if (!valid)
{
this->RefineArgTypeError(this->I - this->M - 1);
}
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);
}
int vtkPythonArgs::GetArgAsSIPEnum(const char *classname, bool &valid)
{
PyObject *o = PyTuple_GET_ITEM(this->Args, this->I++);
int i = vtkPythonArgs::GetArgAsSIPEnum(o, classname, valid);
if (!valid)
{
this->RefineArgTypeError(this->I - this->M - 1);
}
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);
}
//--------------------------------------------------------------------
// 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++); \
if (PyVTKMutableObject_Check(o)) \
{ \
o = PyVTKMutableObject_GetValue(o); \
} \
if (vtkPythonGetValue(o, a)) \
{ \
return true; \
} \
this->RefineArgTypeError(this->I - this->M - 1); \
return false; \
} \
\
bool vtkPythonArgs::GetValue(PyObject *o, T &a) \
{ \
return vtkPythonGetValue(o, a); \
}
VTK_PYTHON_GET_ARG(char *)
VTK_PYTHON_GET_ARG(const char *)
VTK_PYTHON_GET_ARG(std::string)
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)
VTK_PYTHON_GET_ARG(long long)
VTK_PYTHON_GET_ARG(unsigned long long)
//--------------------------------------------------------------------
// 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++); \
if (vtkPythonGetArray(o, a, n)) \
{ \
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)
VTK_PYTHON_GET_ARRAY_ARG(char)
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)
VTK_PYTHON_GET_ARRAY_ARG(long long)
VTK_PYTHON_GET_ARRAY_ARG(unsigned long long)
//--------------------------------------------------------------------
// 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++); \
if (vtkPythonGetNArray(o, a, ndim, dims)) \
{ \
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)
VTK_PYTHON_GET_NARRAY_ARG(char)
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)
VTK_PYTHON_GET_NARRAY_ARG(long long)
VTK_PYTHON_GET_NARRAY_ARG(unsigned long long)
//--------------------------------------------------------------------
// Define the special function pointer GetValue method
bool vtkPythonArgs::GetFunction(PyObject *arg, PyObject *&o)
{
o = arg;
if (o == Py_None || PyCallable_Check(o))
{
return true;
}
PyErr_SetString(PyExc_TypeError, "a callable object is required");
return false;
}
bool vtkPythonArgs::GetFunction(PyObject *&o)
{
PyObject *arg = PyTuple_GET_ITEM(this->Args, this->I++);
return vtkPythonArgs::GetFunction(arg, o);
}
//--------------------------------------------------------------------
// 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 *)
//--------------------------------------------------------------------
// 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; \
}
VTK_PYTHON_SET_ARG(const std::string &)
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(unsigned short)
VTK_PYTHON_SET_ARG(int)
VTK_PYTHON_SET_ARG(unsigned int)
VTK_PYTHON_SET_ARG(long)
VTK_PYTHON_SET_ARG(unsigned long)
VTK_PYTHON_SET_ARG(long long)
VTK_PYTHON_SET_ARG(unsigned long long)
//--------------------------------------------------------------------
// Define all the SetArgValue methods for setting array args
#define VTK_PYTHON_SET_ARRAY_ARG(T) \
bool vtkPythonArgs::SetArray(int i, const T *a, int n) \
{ \
if (this->M + i < this->N) \
{ \
PyObject *o = PyTuple_GET_ITEM(this->Args, this->M + i); \
if (vtkPythonSetArray(o, a, n)) \
{ \
return true; \
} \
this->RefineArgTypeError(i); \
return false; \
} \
return true; \
}
VTK_PYTHON_SET_ARRAY_ARG(bool)
VTK_PYTHON_SET_ARRAY_ARG(float)
VTK_PYTHON_SET_ARRAY_ARG(double)
VTK_PYTHON_SET_ARRAY_ARG(char)
VTK_PYTHON_SET_ARRAY_ARG(signed char)
VTK_PYTHON_SET_ARRAY_ARG(unsigned char)
VTK_PYTHON_SET_ARRAY_ARG(short)
VTK_PYTHON_SET_ARRAY_ARG(unsigned short)
VTK_PYTHON_SET_ARRAY_ARG(int)
VTK_PYTHON_SET_ARRAY_ARG(unsigned int)
VTK_PYTHON_SET_ARRAY_ARG(long)
VTK_PYTHON_SET_ARRAY_ARG(unsigned long)
VTK_PYTHON_SET_ARRAY_ARG(long long)
VTK_PYTHON_SET_ARRAY_ARG(unsigned long long)
//--------------------------------------------------------------------
// Define all the SetArgValue methods for setting multi-dim array args
#define VTK_PYTHON_SET_NARRAY_ARG(T) \
bool vtkPythonArgs::SetNArray( \
int i, const T *a, int ndim, const int *dims) \
{ \
if (this->M + i < this->N) \
{ \
PyObject *o = PyTuple_GET_ITEM(this->Args, this->M + i); \
if (vtkPythonSetNArray(o, a, ndim, dims)) \
{ \
return true; \
} \
this->RefineArgTypeError(i); \
return false; \
} \
return true; \
}
VTK_PYTHON_SET_NARRAY_ARG(bool)
VTK_PYTHON_SET_NARRAY_ARG(float)
VTK_PYTHON_SET_NARRAY_ARG(double)
VTK_PYTHON_SET_NARRAY_ARG(char)
VTK_PYTHON_SET_NARRAY_ARG(signed char)
VTK_PYTHON_SET_NARRAY_ARG(unsigned char)
VTK_PYTHON_SET_NARRAY_ARG(short)
VTK_PYTHON_SET_NARRAY_ARG(unsigned short)
VTK_PYTHON_SET_NARRAY_ARG(int)
VTK_PYTHON_SET_NARRAY_ARG(unsigned int)
VTK_PYTHON_SET_NARRAY_ARG(long)
VTK_PYTHON_SET_NARRAY_ARG(unsigned long)
VTK_PYTHON_SET_NARRAY_ARG(long long)
VTK_PYTHON_SET_NARRAY_ARG(unsigned long long)
//--------------------------------------------------------------------
// Raise an exception about incorrect arg count.
bool vtkPythonArgs::ArgCountError(int m, int n)
{
char text[256];
const char *name = this->MethodName;
int nargs = this->N;
sprintf(text, "%.200s%s takes %s %d argument%s (%d given)",
(name ? name : "function"), (name ? "()" : ""),
((m == n) ? "exactly" : ((nargs < m) ? "at least" : "at most")),
((nargs < m) ? m : n),
((((nargs < m) ? m : n)) == 1 ? "" : "s"),
nargs);
PyErr_SetString(PyExc_TypeError, text);
return false;
}
//--------------------------------------------------------------------
// Static method to write an arg count error.
bool vtkPythonArgs::ArgCountError(int n, const char *name)
{
char text[256];
sprintf(text, "no overloads of %.200s%s take %d argument%s",
(name ? name : "function"), (name ? "()" : ""),
n, (n == 1 ? "" : "s"));
PyErr_SetString(PyExc_TypeError, text);
return false;
}
//--------------------------------------------------------------------
// Raise an exception about pure virtual method call
bool vtkPythonArgs::PureVirtualError()
{
char text[256];
sprintf(text, "pure virtual method %.200s() was called",
this->MethodName);
PyErr_SetString(PyExc_TypeError, text);
return false;
}
//--------------------------------------------------------------------
// Refine an error by saying what argument it is for
bool vtkPythonArgs::RefineArgTypeError(int i)
{
if (PyErr_ExceptionMatches(PyExc_TypeError) ||
PyErr_ExceptionMatches(PyExc_ValueError) ||
PyErr_ExceptionMatches(PyExc_OverflowError))
{
PyObject *exc;
PyObject *val, *newval;
PyObject *frame;
PyErr_Fetch(&exc, &val, &frame);
#ifdef VTK_PY3K
const char *cp = "";
if (val && !PyUnicode_Check(val))
{
Py_DECREF(val);
val = 0;
}
newval = PyUnicode_FromFormat("%s argument %d: %V",
this->MethodName, i+1, val, cp);
#else
const char *cp = "";
if (val && PyString_Check(val))
{
cp = PyString_AsString(val);
}
newval = PyString_FromFormat("%s argument %d: %s",
this->MethodName, i+1, cp);
#endif
Py_XDECREF(val);
PyErr_Restore(exc, newval, frame);
}
return false;
}
//--------------------------------------------------------------------
// Raise a type error for a sequence arg of wrong type or size.
bool vtkPythonSequenceError(PyObject *o, Py_ssize_t n, Py_ssize_t m)
{
char text[80];
if (m == n)
{
sprintf(text, "expected a sequence of %ld value%s, got %s",
(long)n, ((n == 1) ? "" : "s"), Py_TYPE(o)->tp_name);
}
else
{
sprintf(text, "expected a sequence of %ld value%s, got %ld values",
(long)n, ((n == 1) ? "" : "s"), (long)m);
}
PyErr_SetString(PyExc_TypeError, text);
return false;
}
//--------------------------------------------------------------------
// Checking size of array arg.
int vtkPythonArgs::GetArgSize(int i)
{
int size = 0;
if (this->M + i < this->N)
{
PyObject *o = PyTuple_GET_ITEM(this->Args, this->M + i);
if (PySequence_Check(o))
{
size = static_cast<int>(PySequence_Size(o));
}
}
return size;
}
//--------------------------------------------------------------------
// Check if 'm' equals 'n', and report an error for arg i if not.
bool vtkPythonArgs::CheckSizeHint(int i, Py_ssize_t m, Py_ssize_t n)
{
if (this->M + i < this->N)
{
if (m != n)
{
PyObject *o = PyTuple_GET_ITEM(this->Args, this->M + i);
return vtkPythonSequenceError(o, n, m);
}
}
return true;
}
//--------------------------------------------------------------------
// Use stack space for small arrays, heap for large arrays.
template<class T>
vtkPythonArgs::Array<T>::Array(Py_ssize_t n) : Pointer(0)
{
if (n > basicsize)
{
this->Pointer = new T[n];
}
else if (n != 0)
{
this->Pointer = this->Storage;
}
}
// Instantiate the Array class template over all types:
vtkPythonArgsTemplateMacro(
template class VTKWRAPPINGPYTHONCORE_EXPORT vtkPythonArgs::Array
)
|