Changeset 3113 for trunk/lang/python/Rappture/PyRpLibrary.cc
- Timestamp:
- Jul 26, 2012 1:34:19 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lang/python/Rappture/PyRpLibrary.cc
r1527 r3113 11 11 #include <Python.h> 12 12 #include <RpLibrary.h> 13 #include <ctype.h> 14 15 #define TRUE 1 16 #define FALSE 0 17 18 #define RP_OK 0 19 #define RP_ERROR 1 13 20 14 21 #ifndef Py_RETURN_NONE … … 28 35 29 36 static PyObject *ErrorObject; 30 RpLibrary * RpLibraryObject_AsLibrary(PyObject *lib);37 static RpLibrary * RpLibraryObject_AsLibrary(PyObject *lib); 31 38 static PyObject * RpLibraryObject_FromLibrary(RpLibrary *lib); 32 int boolAsInt(const char *inVal, int *outVal);33 int boolIntFromPyObject (PyObject *inPyObj, const char *defaultVal,34 35 int getArgCount (PyObject *args, PyObject *keywds, int *argc);39 static int boolAsInt(const char *inVal, int *outVal); 40 static int boolIntFromPyObject (PyObject *inPyObj, const char *defaultVal, 41 const char *argName, int *boolVal); 42 static int getArgCount(PyObject *args, PyObject *keywds, int *argc); 36 43 37 44 typedef struct { … … 46 53 * correct initialization, use RpLibraryObject_IsValid. 47 54 */ 48 49 int 50 RpLibraryObject_Check(PyObject *o) 51 { 52 int retval = 0; 53 54 if (o != NULL) { 55 if ( (*(o->ob_type->tp_name) == 'R') 56 && (strcmp(o->ob_type->tp_name,"Rappture.library") == 0) ) { 57 retval = 1; 58 } 59 } 60 61 return retval; 55 static int 56 RpLibraryObject_Check(PyObject *objPtr) 57 { 58 if (objPtr == NULL) { 59 return FALSE; 60 } 61 if (strcmp(objPtr->ob_type->tp_name, "Rappture.library") != 0) { 62 return FALSE; 63 } 64 return TRUE; 62 65 } 63 66 … … 70 73 * in by the user is a valid Rappture.library object 71 74 */ 72 73 int 74 RpLibraryObject_IsValid(PyObject *o) 75 {76 int retval = 0; 77 78 if (o != NULL) {79 if (RpLibraryObject_Check(o)) {80 if ( ((RpLibraryObject *)o)->lib != NULL ) { 81 retval = 1;82 }83 } 84 } 85 86 return retval;75 static int 76 RpLibraryObject_IsValid(PyObject *objPtr) 77 { 78 if (objPtr == NULL) { 79 return FALSE; // Passed a NULL pointer. Should 80 // probably segfault. 81 } 82 if (!RpLibraryObject_Check(objPtr)) { 83 return FALSE; // Not a rappture library object type. 84 } 85 if (((RpLibraryObject *)objPtr)->lib == NULL) { 86 return FALSE; // Is a rappture library object, but 87 // doesn't contain a valid library. 88 } 89 return TRUE; 87 90 } 88 91 … … 97 100 } 98 101 else { 99 PyErr_SetString(PyExc_RuntimeError, "trouble creating new RpLibraryObject");100 } 101 102 PyErr_SetString(PyExc_RuntimeError, 103 "trouble creating new RpLibraryObject"); 104 } 102 105 return (PyObject *)self; 103 106 } … … 175 178 char *frompath = (char *)""; 176 179 int argc = 0; 177 int status = 0;178 180 PyObject *fromobj = (PyObject *) self; 179 181 … … 191 193 } 192 194 193 status = getArgCount(args,keywds,&argc); 194 if (status != 0) { 195 if (getArgCount(args,keywds,&argc) != RP_OK) { 195 196 // trouble ensues 196 197 // error message was set in getArgCount() … … 274 275 } 275 276 276 status = getArgCount(args,keywds,&argc); 277 if (status != 0) { 277 if (getArgCount(args,keywds,&argc) != RP_OK) { 278 278 // trouble ensues 279 279 // error message was set in getArgCount() … … 346 346 347 347 PyObject* decode = NULL; 348 int decodeVal = 0;348 int decodeVal = FALSE; 349 349 350 350 PyObject* retVal = NULL; … … 367 367 } 368 368 369 status = getArgCount(args,keywds,&argc); 370 if (status != 0) { 369 if (getArgCount(args,keywds,&argc) != RP_OK) { 371 370 // trouble ensues 372 371 // error message was set in getArgCount() … … 387 386 return NULL; 388 387 } 389 390 status = boolIntFromPyObject(decode,"yes","decode",&decodeVal); 391 if (status != 0) { 388 if (boolIntFromPyObject(decode,"yes", "decode", &decodeVal) != RP_OK) { 392 389 // tested with GetTests.testArgumentsDecodeError() 393 390 return NULL; 394 391 } 395 392 396 if (decodeVal == 1) {393 if (decodeVal) { 397 394 // tested with GetTests.testArgumentsDecode() 398 395 // tested with GetTests.testArgumentsDecodeYes() … … 413 410 retVal = PyString_FromStringAndSize(retValBuf.bytes(),retValBuf.size()); 414 411 } 415 416 412 return (PyObject *)retVal; 417 413 } … … 448 444 }; 449 445 450 status = getArgCount(args,keywds,&argc); 451 if (status != 0) { 446 if (getArgCount(args,keywds,&argc) != RP_OK) { 452 447 // trouble ensues 453 448 // error message was set in getArgCount() … … 459 454 return NULL; 460 455 } 461 462 456 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss", 463 457 kwlist, &path, &as)) { 464 458 /* incorrect input values */ 465 PyErr_SetString(PyExc_TypeError, "parent ([path=\'\'][, as=\'object\'])");466 return NULL;467 }468 459 PyErr_SetString(PyExc_TypeError, 460 "parent ([path=\'\'][, as=\'object\'])"); 461 return NULL; 462 } 469 463 if (self->lib) { 470 464 retlib = self->lib->parent(std::string(path)); 471 } 472 else { 465 } else { 473 466 PyErr_SetString(PyExc_RuntimeError, 474 467 "incorrectly initialized Rappture Library Object"); … … 477 470 478 471 if (retlib != NULL) { 479 if ( (as == NULL) 480 || ((*as == 'o') && (strcmp("object",as) == 0)) 481 ) { 472 if ((as == NULL) || ((*as == 'o') && (strcmp("object",as) == 0))) { 482 473 retVal = RpLibraryObject_FromLibrary(retlib); 483 } 484 else if ((*as == 'c') && (strcmp("component",as) == 0)) { 474 } else if ((*as == 'c') && (strcmp("component",as) == 0)) { 485 475 retVal = PyString_FromString(retlib->nodeComp().c_str()); 486 } 487 else if ((*as == 'i') && (strcmp("id",as) == 0)) { 476 } else if ((*as == 'i') && (strcmp("id",as) == 0)) { 488 477 retVal = PyString_FromString(retlib->nodeId().c_str()); 489 } 490 else if ((*as == 't') && (strcmp("type",as) == 0)) { 478 } else if ((*as == 't') && (strcmp("type",as) == 0)) { 491 479 retVal = PyString_FromString(retlib->nodeType().c_str()); 492 } 493 else if ((*as == 'p') && (strcmp("path",as) == 0)) { 480 } else if ((*as == 'p') && (strcmp("path",as) == 0)) { 494 481 retVal = PyString_FromString(retlib->nodePath().c_str()); 495 } 496 else { 482 } else { 497 483 PyErr_Format(PyExc_ValueError, 498 484 "parent() \'as\' arg must be \'object\' or \'component\' or \'id\' or \'type\' or \'path\'"); 499 485 } 500 486 } 501 502 487 return (PyObject *)retVal; 503 488 } … … 544 529 545 530 PyObject* append = NULL; 546 int appendInt = 0;531 int appendInt = FALSE; 547 532 548 533 char *type = (char *)"string"; 549 534 550 535 PyObject* compress = NULL; 551 int compressInt = 0;536 int compressInt = FALSE; 552 537 553 538 int argc = 0; … … 827 812 * used to create a RpLibrary object from a PyObject 828 813 */ 829 RpLibrary *814 static RpLibrary * 830 815 RpLibraryObject_AsLibrary(PyObject *lib) 831 816 { … … 882 867 */ 883 868 884 int 885 boolAsInt(const char *inVal, int *outVal) 886 { 887 int len = 0; 888 int lcv = 0; 889 int status = 1; 890 char boolVal[7] = "\0\0\0\0\0\0"; 891 892 if ( (inVal == NULL) || (outVal == NULL) ) { 893 PyErr_Format(PyExc_TypeError,"incorrect use of boolAsInt(inVal,outVal)"); 894 return status; 895 } 896 897 len = strlen(inVal); 898 899 if (len > 5) { 869 static int 870 boolAsInt(const char *string, int *resultPtr) 871 { 872 int len, i; 873 char c; 874 875 if ((string == NULL) || (resultPtr == NULL) ) { 876 PyErr_Format(PyExc_TypeError, 877 "incorrect use of boolAsInt(inVal,outVal)"); 878 return RP_ERROR; 879 } 880 c = tolower(string[0]); 881 if ((c == 'y') && (strcasecmp(string, "yes") == 0)) { 882 *resultPtr = TRUE; 883 } else if ((c == 'n') && (strcasecmp(string, "no") == 0)) { 884 *resultPtr = FALSE; 885 } else if ((c == 'o') && (strcasecmp(string, "on") == 0)) { 886 *resultPtr = TRUE; 887 } else if ((c == 'o') && (strcasecmp(string, "off") == 0)) { 888 *resultPtr = FALSE; 889 } else if ((c == 't') && (strcasecmp(string, "true") == 0)) { 890 *resultPtr = TRUE; 891 } else if ((c == 'f') && (strcasecmp(string, "false") == 0)) { 892 *resultPtr = FALSE; 893 } else if ((c == '1') && (strcasecmp(string, "1") == 0)) { 894 *resultPtr = TRUE; 895 } else if ((c == '0') && (strcasecmp(string, "0") == 0)) { 896 *resultPtr = FALSE; 897 } else { 900 898 PyErr_Format(PyExc_ValueError, 901 "unrecognized input: %s: should be one of: \'yes\',\'true\',\'on\',\'1\',1,True,\'no\',\'false\',\'off\',\'0\',0,False",boolVal); 902 return status; 903 } 904 905 for (lcv = 0; lcv < len; lcv++) { 906 boolVal[lcv] = tolower(inVal[lcv]); 907 } 908 909 if ( 910 ((*boolVal == 'y') && (strcmp("yes",boolVal) == 0)) || 911 ((*boolVal == 't') && (strcmp("true",boolVal) == 0)) || 912 ((*boolVal == 'o') && (strcmp("on",boolVal) == 0)) || 913 ((*boolVal == '1') && (strcmp("1",boolVal) == 0)) ) { 914 915 status = 0; 916 *outVal = 1; 917 } 918 else if ( 919 ((*boolVal == 'n') && (strcmp("no",boolVal) == 0)) || 920 ((*boolVal == 'f') && (strcmp("false",boolVal) == 0)) || 921 ((*boolVal == 'o') && (strcmp("off",boolVal) == 0)) || 922 ((*boolVal == '0') && (strcmp("0",boolVal) == 0)) ) { 923 924 status = 0; 925 *outVal = 0; 926 } 927 else { 928 PyErr_Format(PyExc_ValueError, 929 "unrecognized input: %s: should be one of: \'yes\',\'true\',\'on\',\'1\',1,True,\'no\',\'false\',\'off\',\'0\',0,False",boolVal); 930 status = 1; 931 } 932 933 return status; 934 } 935 936 int 937 boolIntFromPyObject ( 938 PyObject *inPyObj, 939 const char *defaultVal, 940 const char *argName, 941 int *boolVal 942 ) 943 { 944 int status = -1; 945 PyObject *inStrObj = NULL; 946 char *inStr = NULL; 947 948 if ( (defaultVal == NULL) || 949 (argName == NULL) || 950 (boolVal == NULL) 951 ) { 899 "unrecognized input: %s: should be one of: \'yes\',\'true\',\'on\',\'1\',1,True,\'no\',\'false\',\'off\',\'0\',0,False", string); 900 return RP_ERROR; 901 } 902 return RP_OK; 903 } 904 905 static int 906 boolIntFromPyObject (PyObject *objPtr, const char *defValue, 907 const char *argName, int *resultPtr) 908 { 909 int value; 910 911 if ((defValue == NULL) || (argName == NULL) || (resultPtr == NULL)) { 952 912 // incorrect use of function 953 913 PyErr_Format(PyExc_ValueError, 954 "boolIntFromPyObject(): defaultVal or argName or boolVal is NULL"); 955 return status; 956 } 957 958 if (inPyObj != NULL) { 959 inStrObj = PyObject_Str(inPyObj); 960 if (inStrObj == NULL) { 961 PyErr_Format(PyExc_TypeError, 962 "bad value: %s: no string representation",argName); 963 return status; 964 } 965 966 inStr = PyString_AsString(inStrObj); 967 if (inStr == NULL) { 914 "boolIntFromPyObject(): defValue or argName or resultPtr is NULL"); 915 return RP_ERROR; 916 } 917 if (objPtr == NULL) { 918 return boolAsInt(defValue, resultPtr); 919 } 920 if (PyBool_Check(objPtr)) { 921 value = PyObject_IsTrue(objPtr); 922 if (value < 0) { 923 PyErr_Format(PyExc_ValueError, 924 "boolIntFromPyObject(): bad boolean object"); 925 return RP_ERROR; 926 } 927 } else if (PyLong_Check(objPtr)) { 928 long l; 929 930 l = PyLong_AsLong(objPtr); 931 value = (l == 0) ? FALSE : TRUE; 932 } else if (PyFloat_Check(objPtr)) { 933 double d; 934 935 d = PyFloat_AS_DOUBLE(objPtr); 936 value = (d == 0.0) ? FALSE : TRUE; 937 } else if (PyString_Check(objPtr)) { 938 const char *string; 939 940 string = PyString_AsString(objPtr); 941 if (string == NULL) { 968 942 PyErr_Format(PyExc_TypeError, 969 943 "bad value: %s: cannot convert to string",argName); 970 return status; 971 } 972 } 973 974 if (inStr != NULL) { 975 status = boolAsInt(inStr,boolVal); 976 } 977 else { 978 status = boolAsInt(defaultVal,boolVal); 979 } 980 981 return status; 982 } 983 984 int getArgCount ( 985 PyObject *args, 986 PyObject *keywds, 987 int *argc 988 ) 989 { 990 int status = 1; 944 return RP_ERROR; 945 } 946 return boolAsInt(string, resultPtr); 947 } 948 *resultPtr = value; 949 return RP_OK; 950 } 951 952 static int 953 getArgCount (PyObject *args, PyObject *keywds, int *argc) 954 { 991 955 int args_cnt = 0; 992 956 int keywds_cnt = 0; … … 996 960 // argc cannot be null 997 961 PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL"); 998 return status; 999 } 1000 962 return RP_ERROR; 963 } 1001 964 if (args != NULL) { 1002 965 if (!PyTuple_Check(args)) { 1003 966 PyErr_Format(PyExc_TypeError, 1004 967 "getArgCount(): \'args\' should be a PyTuple"); 1005 return status;968 return RP_ERROR; 1006 969 } 1007 970 args_cnt = PyTuple_Size(args); 1008 971 } 1009 1010 972 if (keywds != NULL) { 1011 973 if (!PyDict_Check(keywds)) { 1012 974 PyErr_Format(PyExc_TypeError, 1013 975 "getArgCount(): \'keywds\' should be a PyDict"); 1014 return status;976 return RP_ERROR; 1015 977 } 1016 978 keywds_cnt = PyDict_Size(keywds); 1017 979 } 1018 1019 980 *argc = args_cnt + keywds_cnt; 1020 status = 0; 1021 1022 return status; 981 return RP_OK; 1023 982 } 1024 983
Note: See TracChangeset
for help on using the changeset viewer.