- Timestamp:
- Jul 26, 2012, 6:58:17 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lang/python/Rappture/PyRpLibrary.cc
r3115 r3117 37 37 static RpLibrary * RpLibraryObject_AsLibrary(PyObject *lib); 38 38 static PyObject * RpLibraryObject_FromLibrary(RpLibrary *lib); 39 static int boolAsInt(const char *inVal, int *outVal);40 static int boolIntFromPyObject (PyObject *inPyObj, const char *defaultVal,41 const char *argName, int * boolVal);39 static int StringToBoolean(const char *inVal, int *resultPtr); 40 static int PyObjectToBoolean(PyObject *objPtr, const char *defaultVal, 41 const char *argName, int *resultPtr); 42 42 static int getArgCount(PyObject *args, PyObject *keywds, int *argc); 43 43 … … 63 63 } 64 64 return TRUE; 65 } 66 67 68 /* 69 * StringToBoolean -- 70 * 71 * represent a boolean string as an integer. 72 * 73 * outVal set to 1 if the boolean value inVal could 74 * be associated with any of the following strings: 75 * "yes", "on", "true", "1". 76 * 77 * outVal set to 0 if the boolean value inVal could 78 * be associated with any of the following strings: 79 * "no", "off", "false", "0". 80 * 81 * returns a status integer to tell if the operation 82 * was successful (0) of if there was an error (!0) 83 * 84 * note: string comparisons are case insensitive. 85 */ 86 87 static int 88 StringToBoolean(const char *string, int *resultPtr) 89 { 90 char c; 91 92 if ((string == NULL) || (resultPtr == NULL) ) { 93 PyErr_Format(PyExc_TypeError, 94 "incorrect use of StringToBoolean(inVal,outVal)"); 95 return RP_ERROR; 96 } 97 c = tolower(string[0]); 98 if ((c == 'y') && (strcasecmp(string, "yes") == 0)) { 99 *resultPtr = TRUE; 100 } else if ((c == 'n') && (strcasecmp(string, "no") == 0)) { 101 *resultPtr = FALSE; 102 } else if ((c == 'o') && (strcasecmp(string, "on") == 0)) { 103 *resultPtr = TRUE; 104 } else if ((c == 'o') && (strcasecmp(string, "off") == 0)) { 105 *resultPtr = FALSE; 106 } else if ((c == 't') && (strcasecmp(string, "true") == 0)) { 107 *resultPtr = TRUE; 108 } else if ((c == 'f') && (strcasecmp(string, "false") == 0)) { 109 *resultPtr = FALSE; 110 } else if ((c == '1') && (strcasecmp(string, "1") == 0)) { 111 *resultPtr = TRUE; 112 } else if ((c == '0') && (strcasecmp(string, "0") == 0)) { 113 *resultPtr = FALSE; 114 } else { 115 PyErr_Format(PyExc_ValueError, 116 "unrecognized input: %s: should be one of: \'yes\',\'true\',\'on\',\'1\',1,True,\'no\',\'false\',\'off\',\'0\',0,False", string); 117 return RP_ERROR; 118 } 119 return RP_OK; 120 } 121 122 static int 123 PyObjectToBoolean(PyObject *objPtr, const char *defValue, const char *argName, 124 int *resultPtr) 125 { 126 int value; 127 128 value = FALSE; // Suppress compiler warning. 129 if ((defValue == NULL) || (argName == NULL) || (resultPtr == NULL)) { 130 // incorrect use of function 131 PyErr_Format(PyExc_ValueError, 132 "PyObjectToBoolean: defValue or argName or resultPtr is NULL"); 133 return RP_ERROR; 134 } 135 if (objPtr == NULL) { 136 return StringToBoolean(defValue, resultPtr); 137 } 138 if (PyBool_Check(objPtr)) { 139 value = PyObject_IsTrue(objPtr); 140 if (value < 0) { 141 PyErr_Format(PyExc_ValueError, 142 "PyObjectToBoolean: bad boolean object"); 143 return RP_ERROR; 144 } 145 } else if (PyLong_Check(objPtr)) { 146 long l; 147 148 l = PyLong_AsLong(objPtr); 149 value = (l == 0) ? FALSE : TRUE; 150 } else if (PyInt_Check(objPtr)) { 151 long l; 152 153 l = PyInt_AsLong(objPtr); 154 value = (l == 0) ? FALSE : TRUE; 155 } else if (PyFloat_Check(objPtr)) { 156 double d; 157 158 d = PyFloat_AS_DOUBLE(objPtr); 159 value = (d == 0.0) ? FALSE : TRUE; 160 } else if (PyString_Check(objPtr)) { 161 const char *string; 162 163 string = PyString_AsString(objPtr); 164 if (string == NULL) { 165 PyErr_Format(PyExc_TypeError, 166 "bad value: %s: cannot convert to string",argName); 167 return RP_ERROR; 168 } 169 return StringToBoolean(string, resultPtr); 170 } else { 171 PyErr_Format(PyExc_TypeError, 172 "unknown python type for %s", argName); 173 return RP_ERROR; 174 } 175 *resultPtr = value; 176 return RP_OK; 177 } 178 179 static int 180 getArgCount (PyObject *args, PyObject *keywds, int *argc) 181 { 182 int args_cnt = 0; 183 int keywds_cnt = 0; 184 185 if (argc == NULL) { 186 // incorrect use of function 187 // argc cannot be null 188 PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL"); 189 return RP_ERROR; 190 } 191 if (args != NULL) { 192 if (!PyTuple_Check(args)) { 193 PyErr_Format(PyExc_TypeError, 194 "getArgCount(): \'args\' should be a PyTuple"); 195 return RP_ERROR; 196 } 197 args_cnt = PyTuple_Size(args); 198 } 199 if (keywds != NULL) { 200 if (!PyDict_Check(keywds)) { 201 PyErr_Format(PyExc_TypeError, 202 "getArgCount(): \'keywds\' should be a PyDict"); 203 return RP_ERROR; 204 } 205 keywds_cnt = PyDict_Size(keywds); 206 } 207 *argc = args_cnt + keywds_cnt; 208 return RP_OK; 65 209 } 66 210 … … 91 235 92 236 static PyObject * 93 RpLibraryObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)237 NewProc(PyTypeObject *type, PyObject *args, PyObject *kwds) 94 238 { 95 239 RpLibraryObject *self = NULL; … … 107 251 108 252 static int 109 RpLibraryObject_init(RpLibraryObject *self, PyObject *args, PyObject *kwds) 110 { 111 PyObject *inObj = NULL; 112 char *filename = NULL; 113 int retVal = 1; 114 115 if (!PyArg_ParseTuple(args, "|O", &inObj)) { 253 InitProc(RpLibraryObject *self, PyObject *args, PyObject *kwds) 254 { 255 PyObject *objPtr; 256 257 if (!PyArg_ParseTuple(args, "|O", &objPtr)) { 116 258 PyErr_Format(PyExc_TypeError, 117 259 "library() takes at most 1 argument, a file name or a Rappture Library Object"); 118 260 return -1; 119 261 } 120 121 if (inObj != NULL) { 122 if (PyString_Check(inObj)) { 123 filename = PyString_AsString(inObj); 262 if (objPtr != NULL) { 263 if (PyString_Check(objPtr)) { 264 const char *filename; 265 266 filename = PyString_AsString(objPtr); 124 267 if (filename == NULL) { 125 268 PyErr_Format(PyExc_ValueError,"a file name is required"); 126 269 } 127 270 self->lib = new RpLibrary(std::string(filename)); 128 } 129 else if (RpLibraryObject_IsValid(inObj)) { 130 self->lib = new RpLibrary( *(RpLibraryObject_AsLibrary(inObj)) ); 131 } 132 else if (RpLibraryObject_Check(inObj)) { 271 } else if (RpLibraryObject_IsValid(objPtr)) { 272 self->lib = new RpLibrary( *(RpLibraryObject_AsLibrary(objPtr)) ); 273 } else if (RpLibraryObject_Check(objPtr)) { 133 274 self->lib = new RpLibrary(); 134 } 135 else { 275 } else { 136 276 PyErr_Format(PyExc_TypeError,"unrecognized object type"); 137 277 return -1; 138 } 139 } 140 else { 278 } 279 } else { 141 280 self->lib = new RpLibrary(); 142 281 } 143 282 144 return retVal;283 return -1; 145 284 } 146 285 147 286 static void 148 RpLibraryObject_dealloc(RpLibraryObject *self)287 FreeProc(RpLibraryObject *self) 149 288 { 150 289 if (self) { … … 156 295 } 157 296 158 PyDoc_STRVAR( RpLibraryObject_copy_doc,297 PyDoc_STRVAR(CopyProcDoc, 159 298 "copy (topath, frompath [, fromobj=$self]) -> None, copies data\n\ 160 299 \n\ … … 173 312 174 313 static PyObject * 175 RpLibraryObject_copy(RpLibraryObject *self, PyObject *args, PyObject *keywds)314 CopyProc(RpLibraryObject *self, PyObject *args, PyObject *keywds) 176 315 { 177 316 char *topath = (char *)""; … … 235 374 } 236 375 237 PyDoc_STRVAR( RpLibraryObject_element_doc,376 PyDoc_STRVAR(ElementProcDoc, 238 377 "element ([path=\'\'][, as=\'object\']) -> returns string or Rappture Library Object\n\ 239 378 \n\ … … 254 393 255 394 static PyObject * 256 RpLibraryObject_element(RpLibraryObject *self, PyObject *args, PyObject *keywds)395 ElementProc(RpLibraryObject *self, PyObject *args, PyObject *keywds) 257 396 { 258 397 char* path = (char *)""; … … 275 414 276 415 if (getArgCount(args,keywds,&argc) != RP_OK) { 277 // trouble ensues 278 // error message was set in getArgCount() 279 return NULL; 280 } 281 416 return NULL; 417 } 282 418 if (argc > 2) { 283 // tested with ElementTests.testArguments_TooManyArgs()284 419 PyErr_Format(PyExc_TypeError, 285 420 "element() takes at most 2 arguments (%i given)", argc); … … 287 422 } 288 423 289 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss", 290 kwlist, &path, &as)) { 424 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss", kwlist, &path, &as)) { 291 425 /* incorrect input values */ 292 426 // tested with ElementTests.testArguments_ArgsWrongType2() … … 298 432 299 433 if (retlib != NULL) { 300 if ( (as == NULL) 301 || ((*as == 'o') && (strcmp("object",as) == 0)) ) { 434 if ((as == NULL) || ((*as == 'o') && (strcmp("object",as) == 0)) ) { 302 435 // tested with ElementTests.testArguments_PathArg() 303 436 retVal = RpLibraryObject_FromLibrary(retlib); 304 } 305 else if ((*as == 'c') && (strcmp("component",as) == 0)) { 437 } else if ((*as == 'c') && (strcmp("component",as) == 0)) { 306 438 // tested with ElementTests.testArguments_TwoArgs() 307 439 retVal = PyString_FromString(retlib->nodeComp().c_str()); 308 } 309 else if ((*as == 'i') && (strcmp("id",as) == 0)) { 440 } else if ((*as == 'i') && (strcmp("id",as) == 0)) { 310 441 // tested with ElementTests.testArguments_AsId() 311 442 retVal = PyString_FromString(retlib->nodeId().c_str()); 312 } 313 else if ((*as == 't') && (strcmp("type",as) == 0)) { 443 } else if ((*as == 't') && (strcmp("type",as) == 0)) { 314 444 // tested with ElementTests.testArguments_AsKeywordArgs() 315 445 retVal = PyString_FromString(retlib->nodeType().c_str()); 316 } 317 else if ((*as == 'p') && (strcmp("path",as) == 0)) { 446 } else if ((*as == 'p') && (strcmp("path",as) == 0)) { 318 447 // tested with ElementTests.testArguments_TwoKeywordArgs() 319 448 retVal = PyString_FromString(retlib->nodePath().c_str()); 320 } 321 else { 449 } else { 322 450 // tested with ElementTests.testArguments_UnrecognizedAs() 323 451 PyErr_Format(PyExc_ValueError, … … 325 453 } 326 454 } 327 328 455 return (PyObject *)retVal; 329 456 } 330 457 331 458 332 PyDoc_STRVAR( RpLibraryObject_get_doc,459 PyDoc_STRVAR(GetProcDoc, 333 460 "get ([path=\'\'][, decode=\'True\']) -> returns data at \'path\' as string\n\ 334 461 \n\ … … 340 467 341 468 static PyObject * 342 RpLibraryObject_get(RpLibraryObject *self, PyObject *args, PyObject *keywds)469 GetProc(RpLibraryObject *self, PyObject *args, PyObject *keywds) 343 470 { 344 471 char* path = (char *)""; 345 472 346 473 PyObject* decode = NULL; 347 int decode Val = FALSE;474 int decodeFlag; 348 475 349 476 PyObject* retVal = NULL; … … 384 511 return NULL; 385 512 } 386 if ( boolIntFromPyObject(decode,"yes", "decode", &decodeVal) != RP_OK) {513 if (PyObjectToBoolean(decode,"yes", "decode", &decodeFlag) != RP_OK) { 387 514 // tested with GetTests.testArgumentsDecodeError() 388 515 return NULL; 389 516 } 390 391 if (decodeVal) { 392 // tested with GetTests.testArgumentsDecode() 393 // tested with GetTests.testArgumentsDecodeYes() 394 // tested with GetTests.testArgumentsDecodeTrue) 395 // tested with GetTests.testArgumentsDecodeOn() 396 // tested with GetTests.testArgumentsDecode1Str() 397 // tested with GetTests.testArgumentsDecode1Int() 517 if (decodeFlag) { 398 518 retValStr = self->lib->get(std::string(path)); 399 519 retVal = PyString_FromStringAndSize(retValStr.c_str(),retValStr.size()); 400 } 401 else { 402 // tested with GetTests.testArgumentsNoDecodeNo() 403 // tested with GetTests.testArgumentsNoDecodeFalse() 404 // tested with GetTests.testArgumentsNoDecodeOff() 405 // tested with GetTests.testArgumentsNoDecode0Str() 406 // tested with GetTests.testArgumentsNoDecode0Int() 520 } else { 407 521 retValBuf = self->lib->getData(std::string(path)); 408 522 retVal = PyString_FromStringAndSize(retValBuf.bytes(),retValBuf.size()); … … 411 525 } 412 526 413 PyDoc_STRVAR(RpLibraryObject_parent_doc, 527 #ifdef notdef 528 PyDoc_STRVAR(ParentProcDoc, 414 529 "parent ([path=\'\'][, as=\'object\']) -> returns string or Rappture Library Object\n\ 415 530 \n\ … … 427 542 428 543 static PyObject * 429 RpLibraryObject_parent(RpLibraryObject *self, PyObject *args, PyObject *keywds)544 ParentProc(RpLibraryObject *self, PyObject *args, PyObject *keywds) 430 545 { 431 546 char* path = (char *)""; … … 484 599 return (PyObject *)retVal; 485 600 } 486 487 PyDoc_STRVAR(RpLibraryObject_put_doc, 601 #endif 602 603 PyDoc_STRVAR(PutProcDoc, 488 604 "put (path=\'\', value=\'\'[,id=None][,append=False][,type=\'string\'][,compress=False]) -> None\n\ 489 605 \n\ … … 514 630 515 631 static PyObject * 516 RpLibraryObject_put(RpLibraryObject *self, PyObject *args, PyObject *keywds) 517 { 518 char *path = (char *)""; 519 520 PyObject *value = NULL; 521 PyObject *valueStrObj = NULL; 522 char *value_char = NULL; 523 Py_ssize_t value_len = 0; 524 632 PutProc(RpLibraryObject *self, PyObject *args, PyObject *keywds) 633 { 634 char *path; 635 PyObject *compressObjPtr, *appendObjPtr, *valueObjPtr, *strObjPtr; 636 int appendFlag, compressFlag; 525 637 char *id = NULL; 526 527 PyObject* append = NULL;528 int appendInt = FALSE;529 530 638 char *type = (char *)"string"; 531 532 PyObject* compress = NULL;533 int compressInt = FALSE;534 535 639 int argc = 0; 536 int status = 0;537 640 538 641 static char *kwlist[] = { … … 551 654 return NULL; 552 655 } 553 554 status = getArgCount(args,keywds,&argc); 555 if (status != 0) { 556 // trouble ensues 557 // error message was set in getArgCount() 558 return NULL; 559 } 560 656 if (getArgCount(args,keywds,&argc) != RP_OK) { 657 return NULL; 658 } 561 659 if (argc > 6) { 562 // tested with PutTests.testArguments_TooManyArgs() 563 PyErr_Format(PyExc_TypeError, 564 "put() takes at most 6 arguments (%i given)",argc); 565 return NULL; 566 } 567 660 PyErr_Format(PyExc_TypeError, 661 "put() takes at most 6 arguments (%i given)", argc); 662 return NULL; 663 } 568 664 if (argc < 2) { 569 // tested with PutTests.testArguments_NotEnoughArgs() 570 PyErr_Format(PyExc_TypeError, 571 "put() takes at least 2 arguments (%i given)",argc); 572 return NULL; 573 } 574 575 if (!PyArg_ParseTupleAndKeywords(args, keywds, "sO|sOsO", 576 kwlist, &path, &value, &id, &append, &type, &compress)) { 577 // tested with PutTests.testArguments_ArgsWrongType2() 578 PyErr_Format(PyExc_TypeError, "incorrect arguments: put(path=\'\',value=\'\'[,id=None][,append=False][,type=\'string\'][,compress=False])"); 579 return NULL; 580 } 581 582 if (value == NULL) { 665 PyErr_Format(PyExc_TypeError, 666 "put() takes at least 2 arguments (%i given)", argc); 667 return NULL; 668 } 669 if (!PyArg_ParseTupleAndKeywords(args, keywds, "sO|sOsO", kwlist, &path, 670 &valueObjPtr, &id, &appendObjPtr, &type, &compressObjPtr)) { 671 return NULL; 672 } 673 if (valueObjPtr == NULL) { 583 674 PyErr_Format(PyExc_ValueError, "put()'s \'value\' arg is required"); 584 675 return NULL; 585 676 } 586 587 valueStrObj = PyObject_Str(value); 588 589 status = boolIntFromPyObject(append,"no","append",&appendInt); 590 if (status != 0) { 591 // error message set inside of boolIntFromPyObject 592 // tested with PutTests.testArguments_AppendValError() 593 return NULL; 594 } 595 status = boolIntFromPyObject(compress,"no","compress",&compressInt); 596 if (status != 0) { 597 // error message set inside of boolIntFromPyObject 598 // tested with PutTests.testArguments_CompressValError() 599 return NULL; 600 } 601 602 if (RpLibraryObject_IsValid(value)) { 603 // tested with PutTests.testPutObject() 604 self->lib->put( std::string(path), 605 RpLibraryObject_AsLibrary(value), "", appendInt); 606 } 607 else if (valueStrObj != NULL) { 608 609 if (PyString_AsStringAndSize(valueStrObj, &value_char, &value_len) == -1) { 677 strObjPtr = PyObject_Str(valueObjPtr); 678 679 if (PyObjectToBoolean(appendObjPtr, "no", "append", &appendFlag) != RP_OK) { 680 return NULL; 681 } 682 if (PyObjectToBoolean(compressObjPtr, "no", "compress", &compressFlag) 683 != RP_OK) { 684 return NULL; 685 } 686 if (RpLibraryObject_IsValid(valueObjPtr)) { 687 self->lib->put( std::string(path), 688 RpLibraryObject_AsLibrary(valueObjPtr), "", appendFlag); 689 } else if (strObjPtr != NULL) { 690 char *string; 691 Py_ssize_t length; 692 693 if (PyString_AsStringAndSize(strObjPtr, &string, &length) == -1) { 610 694 // user passed in an improper string 611 695 // exception raised within fxn 612 696 return NULL; 613 697 } 614 615 if ( ( type == NULL ) 616 || ( (*type == 's') && (strcmp("string", type) == 0) ) ) { 617 if (compressInt == 0) { 618 // tested with PutTests.testArgumentsCheckAppend() 619 // tested with PutTests.testArgumentsCheckAppendYes() 698 if ((type == NULL) || ((*type=='s') && (strcmp("string", type) == 0))) { 699 if (compressFlag == 0) { 620 700 self->lib->put( std::string(path), 621 std::string(value_char), "", appendInt); 701 std::string(string), "", appendFlag); 702 } else { 703 self->lib->putData(std::string(path), string, length, 704 appendFlag); 622 705 } 623 else { 624 // tested with PutTests.testArgumentsCheckTypeStringCompressYes() 625 self->lib->putData( std::string(path), 626 value_char, value_len, appendInt); 627 } 628 } 629 else if ( (*type == 'f') && (strcmp("file",type) == 0) ) { 630 // tested with PutTests.testArgumentsCheckTypeFile() 631 // tested with PutTests.testArgumentsCheckTypeFileCompressTrue() 632 // tested with PutTests.testArgumentsCheckTypeFileCompressTrueNoDecode() 706 } else if ((*type == 'f') && (strcmp("file",type) == 0) ) { 633 707 self->lib->putFile( std::string(path), 634 std::string(PyString_AsString(valueStrObj)), 635 compressInt, appendInt); 636 } 637 else { 638 // tested with PutTests.testArgumentsCheckTypeError() 708 std::string(PyString_AsString(strObjPtr)), 709 compressFlag, appendFlag); 710 } else { 639 711 PyErr_Format(PyExc_ValueError, 640 712 "\'type\' arg must be \'string\' or \'file\'"); 641 713 return NULL; 642 714 } 643 } 644 else { 715 } else { 645 716 PyErr_Format(PyExc_TypeError, 646 717 "put()'s \'value\' arg must be a string or Rappture Library Object"); 647 718 return NULL; 648 719 } 649 650 720 Py_RETURN_NONE; 651 721 } 652 722 653 PyDoc_STRVAR( RpLibraryObject_xml_doc,723 PyDoc_STRVAR(XmlProcDoc, 654 724 "xml () -> returns xml string\n\ 655 725 \n\ … … 659 729 660 730 static PyObject* 661 RpLibraryObject_xml(RpLibraryObject *self)731 XmlProc(RpLibraryObject *self) 662 732 { 663 733 PyObject *retStr = NULL; … … 673 743 } 674 744 675 PyDoc_STRVAR(R pLibraryObject_result_doc,745 PyDoc_STRVAR(ResultProcDoc, 676 746 "result ([status=0]) -> None, send results back to graphical user interface\n\ 677 747 \n\ … … 681 751 682 752 static PyObject* 683 R pLibraryObject_result(RpLibraryObject *self, PyObject *args, PyObject *keywds)753 ResultProc(RpLibraryObject *self, PyObject *args, PyObject *keywds) 684 754 { 685 755 int argc = 0; … … 696 766 return NULL; 697 767 } 698 699 status = getArgCount(args,keywds,&argc); 700 if (status != 0) { 768 if (getArgCount(args,keywds,&argc) != RP_OK) { 701 769 // trouble ensues 702 770 // error message was set in getArgCount() … … 709 777 return NULL; 710 778 } 711 712 779 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|i", kwlist, &status)) { 713 780 // tested with ResultTests.testArguments_InvalidStatusArg() … … 715 782 return NULL; 716 783 } 717 718 self->lib->put("tool.version.rappture.language","python"); 784 self->lib->put("tool.version.rappture.language", "python"); 719 785 self->lib->result(status); 720 786 … … 724 790 static PyMethodDef RpLibraryObject_methods[] = { 725 791 726 {"copy", (PyCFunction)RpLibraryObject_copy, METH_VARARGS|METH_KEYWORDS, 727 RpLibraryObject_copy_doc}, 792 {"copy", (PyCFunction)CopyProc, METH_VARARGS|METH_KEYWORDS, CopyProcDoc}, 728 793 729 794 /* 730 {"children", (PyCFunction) RpLibraryObject_children, METH_VARARGS|METH_KEYWORDS,731 RpLibraryObject_children_doc},795 {"children", (PyCFunction)ChildrenProc, METH_VARARGS|METH_KEYWORDS, 796 ChildrenProcDoc}, 732 797 */ 733 798 734 {"element", (PyCFunction)RpLibraryObject_element, METH_VARARGS|METH_KEYWORDS, 735 RpLibraryObject_element_doc}, 736 737 {"get", (PyCFunction)RpLibraryObject_get, METH_VARARGS|METH_KEYWORDS, 738 RpLibraryObject_get_doc}, 799 {"element", (PyCFunction)ElementProc, METH_VARARGS|METH_KEYWORDS, 800 ElementProcDoc}, 801 802 {"get", (PyCFunction)GetProc, METH_VARARGS|METH_KEYWORDS, GetProcDoc}, 739 803 740 804 /* 741 {"parent", (PyCFunction) RpLibraryObject_parent, METH_VARARGS|METH_KEYWORDS,742 RpLibraryObject_put_doc},805 {"parent", (PyCFunction)ParentProc, METH_VARARGS|METH_KEYWORDS, 806 ParentProcDoc}, 743 807 */ 744 808 745 {"put", (PyCFunction)RpLibraryObject_put, METH_VARARGS|METH_KEYWORDS, 746 RpLibraryObject_put_doc}, 809 {"put", (PyCFunction)PutProc, METH_VARARGS|METH_KEYWORDS, PutProcDoc}, 747 810 748 811 /* 749 {"remove", (PyCFunction)R pLibraryObject_remove, METH_VARARGS|METH_KEYWORDS,750 R pLibraryObject_remove_doc},812 {"remove", (PyCFunction)RemoveProc, METH_VARARGS|METH_KEYWORDS, 813 RemoveProcDoc}, 751 814 */ 752 815 753 {"xml", (PyCFunction)RpLibraryObject_xml, METH_NOARGS, 754 RpLibraryObject_xml_doc}, 755 756 {"result", (PyCFunction)RpLibraryObject_result, METH_VARARGS|METH_KEYWORDS, 757 RpLibraryObject_result_doc}, 758 759 {NULL, NULL} /* sentinel */ 816 {"xml", (PyCFunction)XmlProc, METH_NOARGS, XmlProcDoc}, 817 818 {"result", (PyCFunction)ResultProc, METH_VARARGS|METH_KEYWORDS, 819 ResultProcDoc}, 820 821 {NULL, NULL} /* sentinel */ 760 822 }; 761 823 … … 769 831 0, /*tp_itemsize*/ 770 832 /* methods */ 771 (destructor) RpLibraryObject_dealloc,/*tp_dealloc*/833 (destructor)FreeProc, /*tp_dealloc*/ 772 834 0, /*tp_print*/ 773 835 0, /*tp_getattr*/ … … 800 862 0, /*tp_descr_set*/ 801 863 0, /*tp_dictoffset*/ 802 (initproc) RpLibraryObject_init,/*tp_init*/864 (initproc)InitProc, /*tp_init*/ 803 865 0, /*tp_alloc*/ 804 RpLibraryObject_new,/*tp_new*/866 NewProc, /*tp_new*/ 805 867 0, /*tp_new*/ 806 868 }; … … 845 907 } 846 908 847 /*848 * int boolAsInt (inVal, outVal)849 *850 * represent a boolean string as an integer.851 *852 * outVal set to 1 if the boolean value inVal could853 * be associated with any of the following strings:854 * "yes", "on", "true", "1".855 *856 * outVal set to 0 if the boolean value inVal could857 * be associated with any of the following strings:858 * "no", "off", "false", "0".859 *860 * returns a status integer to tell if the operation861 * was successful (0) of if there was an error (!0)862 *863 * note: string comparisons are case insensitive.864 */865 866 static int867 boolAsInt(const char *string, int *resultPtr)868 {869 char c;870 871 if ((string == NULL) || (resultPtr == NULL) ) {872 PyErr_Format(PyExc_TypeError,873 "incorrect use of boolAsInt(inVal,outVal)");874 return RP_ERROR;875 }876 c = tolower(string[0]);877 if ((c == 'y') && (strcasecmp(string, "yes") == 0)) {878 *resultPtr = TRUE;879 } else if ((c == 'n') && (strcasecmp(string, "no") == 0)) {880 *resultPtr = FALSE;881 } else if ((c == 'o') && (strcasecmp(string, "on") == 0)) {882 *resultPtr = TRUE;883 } else if ((c == 'o') && (strcasecmp(string, "off") == 0)) {884 *resultPtr = FALSE;885 } else if ((c == 't') && (strcasecmp(string, "true") == 0)) {886 *resultPtr = TRUE;887 } else if ((c == 'f') && (strcasecmp(string, "false") == 0)) {888 *resultPtr = FALSE;889 } else if ((c == '1') && (strcasecmp(string, "1") == 0)) {890 *resultPtr = TRUE;891 } else if ((c == '0') && (strcasecmp(string, "0") == 0)) {892 *resultPtr = FALSE;893 } else {894 PyErr_Format(PyExc_ValueError,895 "unrecognized input: %s: should be one of: \'yes\',\'true\',\'on\',\'1\',1,True,\'no\',\'false\',\'off\',\'0\',0,False", string);896 return RP_ERROR;897 }898 return RP_OK;899 }900 901 static int902 boolIntFromPyObject (PyObject *objPtr, const char *defValue,903 const char *argName, int *resultPtr)904 {905 int value;906 907 value = FALSE; // Suppress compiler warning.908 if ((defValue == NULL) || (argName == NULL) || (resultPtr == NULL)) {909 // incorrect use of function910 PyErr_Format(PyExc_ValueError,911 "boolIntFromPyObject(): defValue or argName or resultPtr is NULL");912 return RP_ERROR;913 }914 if (objPtr == NULL) {915 return boolAsInt(defValue, resultPtr);916 }917 if (PyBool_Check(objPtr)) {918 value = PyObject_IsTrue(objPtr);919 if (value < 0) {920 PyErr_Format(PyExc_ValueError,921 "boolIntFromPyObject(): bad boolean object");922 return RP_ERROR;923 }924 } else if (PyLong_Check(objPtr)) {925 long l;926 927 l = PyLong_AsLong(objPtr);928 value = (l == 0) ? FALSE : TRUE;929 } else if (PyInt_Check(objPtr)) {930 long l;931 932 l = PyInt_AsLong(objPtr);933 value = (l == 0) ? FALSE : TRUE;934 } else if (PyFloat_Check(objPtr)) {935 double d;936 937 d = PyFloat_AS_DOUBLE(objPtr);938 value = (d == 0.0) ? FALSE : TRUE;939 } else if (PyString_Check(objPtr)) {940 const char *string;941 942 string = PyString_AsString(objPtr);943 if (string == NULL) {944 PyErr_Format(PyExc_TypeError,945 "bad value: %s: cannot convert to string",argName);946 return RP_ERROR;947 }948 return boolAsInt(string, resultPtr);949 } else {950 PyErr_Format(PyExc_TypeError,951 "unknown python type for %s", argName);952 return RP_ERROR;953 }954 *resultPtr = value;955 return RP_OK;956 }957 958 static int959 getArgCount (PyObject *args, PyObject *keywds, int *argc)960 {961 int args_cnt = 0;962 int keywds_cnt = 0;963 964 if (argc == NULL) {965 // incorrect use of function966 // argc cannot be null967 PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL");968 return RP_ERROR;969 }970 if (args != NULL) {971 if (!PyTuple_Check(args)) {972 PyErr_Format(PyExc_TypeError,973 "getArgCount(): \'args\' should be a PyTuple");974 return RP_ERROR;975 }976 args_cnt = PyTuple_Size(args);977 }978 if (keywds != NULL) {979 if (!PyDict_Check(keywds)) {980 PyErr_Format(PyExc_TypeError,981 "getArgCount(): \'keywds\' should be a PyDict");982 return RP_ERROR;983 }984 keywds_cnt = PyDict_Size(keywds);985 }986 *argc = args_cnt + keywds_cnt;987 return RP_OK;988 }989 990 909 /* ---------- */ 991 910
Note: See TracChangeset
for help on using the changeset viewer.