Changeset 3127 for trunk/lang/python/Rappture
- Timestamp:
- Aug 1, 2012 10:33:14 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lang/python/Rappture/PyRpLibrary.cc
r3119 r3127 47 47 } RpLibraryObject; 48 48 49 /* 50 * Check to see if this object is a Rappture.library object. 51 * This does not check to see if the object is correctly 52 * initialized. To check for correct object type and 53 * correct initialization, use RpLibraryObject_IsValid. 54 */ 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; 65 } 66 49 50 static int 51 getArgCount(PyObject *args, PyObject *keywds, int *argc) 52 { 53 int args_cnt = 0; 54 int keywds_cnt = 0; 55 56 if (argc == NULL) { 57 // incorrect use of function 58 // argc cannot be null 59 PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL"); 60 return RP_ERROR; 61 } 62 if (args != NULL) { 63 if (!PyTuple_Check(args)) { 64 PyErr_Format(PyExc_TypeError, 65 "getArgCount(): \'args\' should be a PyTuple"); 66 return RP_ERROR; 67 } 68 args_cnt = PyTuple_Size(args); 69 } 70 if (keywds != NULL) { 71 if (!PyDict_Check(keywds)) { 72 PyErr_Format(PyExc_TypeError, 73 "getArgCount(): \'keywds\' should be a PyDict"); 74 return RP_ERROR; 75 } 76 keywds_cnt = PyDict_Size(keywds); 77 } 78 *argc = args_cnt + keywds_cnt; 79 return RP_OK; 80 } 67 81 68 82 /* … … 171 185 } 172 186 173 static int 174 getArgCount (PyObject *args, PyObject *keywds, int *argc) 175 { 176 int args_cnt = 0; 177 int keywds_cnt = 0; 178 179 if (argc == NULL) { 180 // incorrect use of function 181 // argc cannot be null 182 PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL"); 183 return RP_ERROR; 184 } 185 if (args != NULL) { 186 if (!PyTuple_Check(args)) { 187 PyErr_Format(PyExc_TypeError, 188 "getArgCount(): \'args\' should be a PyTuple"); 189 return RP_ERROR; 190 } 191 args_cnt = PyTuple_Size(args); 192 } 193 if (keywds != NULL) { 194 if (!PyDict_Check(keywds)) { 195 PyErr_Format(PyExc_TypeError, 196 "getArgCount(): \'keywds\' should be a PyDict"); 197 return RP_ERROR; 198 } 199 keywds_cnt = PyDict_Size(keywds); 200 } 201 *argc = args_cnt + keywds_cnt; 202 return RP_OK; 187 /* 188 * Check to see if this object is a Rappture.library object. 189 * This does not check to see if the object is correctly 190 * initialized. To check for correct object type and 191 * correct initialization, use RpLibraryObject_IsValid. 192 */ 193 static int 194 RpLibraryObject_Check(PyObject *objPtr) 195 { 196 if (objPtr == NULL) { 197 return FALSE; 198 } 199 if (strcmp(objPtr->ob_type->tp_name, "Rappture.library") != 0) { 200 return FALSE; 201 } 202 return TRUE; 203 203 } 204 204 … … 249 249 PyObject *objPtr; 250 250 251 objPtr = NULL; 251 252 if (!PyArg_ParseTuple(args, "|O", &objPtr)) { 252 253 PyErr_Format(PyExc_TypeError, … … 254 255 return -1; 255 256 } 257 256 258 if (objPtr != NULL) { 257 259 if (PyString_Check(objPtr)) { 258 c onst char *filename;260 char *filename = NULL; 259 261 260 262 filename = PyString_AsString(objPtr); … … 263 265 } 264 266 self->lib = new RpLibrary(std::string(filename)); 265 } else if (RpLibraryObject_IsValid(objPtr)) { 267 } 268 else if (RpLibraryObject_IsValid(objPtr)) { 266 269 self->lib = new RpLibrary( *(RpLibraryObject_AsLibrary(objPtr)) ); 267 } else if (RpLibraryObject_Check(objPtr)) { 270 } 271 else if (RpLibraryObject_Check(objPtr)) { 268 272 self->lib = new RpLibrary(); 269 } else { 273 } 274 else { 270 275 PyErr_Format(PyExc_TypeError,"unrecognized object type"); 271 276 return -1; 272 } 273 } else { 277 } 278 } 279 else { 274 280 self->lib = new RpLibrary(); 275 281 } 276 282 277 return -1;283 return 1; 278 284 } 279 285 … … 408 414 409 415 if (getArgCount(args,keywds,&argc) != RP_OK) { 410 return NULL; 411 } 416 // trouble ensues 417 // error message was set in getArgCount() 418 return NULL; 419 } 420 412 421 if (argc > 2) { 422 // tested with ElementTests.testArguments_TooManyArgs() 413 423 PyErr_Format(PyExc_TypeError, 414 424 "element() takes at most 2 arguments (%i given)", argc); … … 416 426 } 417 427 418 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss", kwlist, &path, &as)) { 428 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss", 429 kwlist, &path, &as)) { 419 430 /* incorrect input values */ 420 431 // tested with ElementTests.testArguments_ArgsWrongType2() … … 447 458 } 448 459 } 460 449 461 return (PyObject *)retVal; 450 462 } … … 626 638 PutProc(RpLibraryObject *self, PyObject *args, PyObject *keywds) 627 639 { 628 char *path; 629 PyObject *compressObjPtr, *appendObjPtr, *valueObjPtr, *strObjPtr; 640 char *path = (char *)""; 641 642 PyObject *valueObjPtr, *compressObjPtr, *appendObjPtr, *strObjPtr; 643 char *id = NULL; 630 644 int appendFlag, compressFlag; 631 char *id = NULL;632 645 char *type = (char *)"string"; 633 646 int argc = 0; … … 642 655 NULL 643 656 }; 644 657 appendFlag = compressFlag = FALSE; 658 strObjPtr = appendObjPtr = valueObjPtr = compressObjPtr = NULL; 645 659 if (self->lib == NULL) { 646 660 PyErr_Format(PyExc_RuntimeError, … … 648 662 return NULL; 649 663 } 664 650 665 if (getArgCount(args,keywds,&argc) != RP_OK) { 651 666 return NULL; … … 653 668 if (argc > 6) { 654 669 PyErr_Format(PyExc_TypeError, 655 "put() takes at most 6 arguments (%i given)", 670 "put() takes at most 6 arguments (%i given)",argc); 656 671 return NULL; 657 672 } 658 673 if (argc < 2) { 659 674 PyErr_Format(PyExc_TypeError, 660 "put() takes at least 2 arguments (%i given)", 675 "put() takes at least 2 arguments (%i given)",argc); 661 676 return NULL; 662 677 } 663 678 if (!PyArg_ParseTupleAndKeywords(args, keywds, "sO|sOsO", kwlist, &path, 664 &valueObjPtr, &id, &appendObjPtr, &type, &compressObjPtr)) { 665 return NULL; 666 } 679 &valueObjPtr, &id, &appendObjPtr, &type, &compressObjPtr)) { 680 return NULL; 681 } 682 667 683 if (valueObjPtr == NULL) { 668 684 PyErr_Format(PyExc_ValueError, "put()'s \'value\' arg is required"); 669 685 return NULL; 670 686 } 687 688 if (PyObjectToBoolean(appendObjPtr, "no"," append", &appendFlag) != RP_OK){ 689 return NULL; 690 } 691 if (PyObjectToBoolean(compressObjPtr, "no", "compress", &compressFlag) 692 != RP_OK) { 693 return NULL; 694 } 695 671 696 strObjPtr = PyObject_Str(valueObjPtr); 672 673 if (PyObjectToBoolean(appendObjPtr, "no", "append", &appendFlag) != RP_OK) {674 return NULL;675 }676 if (PyObjectToBoolean(compressObjPtr, "no", "compress", &compressFlag)677 != RP_OK) {678 return NULL;679 }680 697 if (RpLibraryObject_IsValid(valueObjPtr)) { 681 698 self->lib->put( std::string(path), … … 686 703 687 704 if (PyString_AsStringAndSize(strObjPtr, &string, &length) == -1) { 688 // user passed in an improper string689 // exception raised within fxn690 705 return NULL; 691 706 } 692 if ((type == NULL) || ((*type=='s') && (strcmp("string", type) == 0))) 707 if ((type == NULL) || ((*type=='s') && (strcmp("string", type) == 0))){ 693 708 if (compressFlag == 0) { 694 709 self->lib->put( std::string(path), 695 710 std::string(string), "", appendFlag); 696 711 } else { 697 self->lib->putData( std::string(path), string, length,712 self->lib->putData( std::string(path), string, length, 698 713 appendFlag); 699 714 } 700 } else if ((*type == 'f') && (strcmp("file",type) == 0) 715 } else if ((*type == 'f') && (strcmp("file",type) == 0)) { 701 716 self->lib->putFile( std::string(path), 702 717 std::string(PyString_AsString(strObjPtr)), … … 725 740 XmlProc(RpLibraryObject *self) 726 741 { 727 PyObject *retStr = NULL;728 729 742 if (self->lib == NULL) { 730 743 PyErr_Format(PyExc_RuntimeError, 731 744 "self uninitialized Rappture Library Object"); 732 745 } 733 734 retStr = PyString_FromString(self->lib->xml().c_str()); 735 736 return (PyObject *)retStr; 746 return PyString_FromString(self->lib->xml().c_str()); 737 747 } 738 748 … … 760 770 return NULL; 761 771 } 772 762 773 if (getArgCount(args,keywds,&argc) != RP_OK) { 763 774 // trouble ensues … … 771 782 return NULL; 772 783 } 784 773 785 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|i", kwlist, &status)) { 774 786 // tested with ResultTests.testArguments_InvalidStatusArg() … … 776 788 return NULL; 777 789 } 778 self->lib->put("tool.version.rappture.language", "python"); 790 791 self->lib->put("tool.version.rappture.language","python"); 779 792 self->lib->result(status); 780 793 … … 786 799 {"copy", (PyCFunction)CopyProc, METH_VARARGS|METH_KEYWORDS, CopyProcDoc}, 787 800 788 /* 801 #ifdef notdef 789 802 {"children", (PyCFunction)ChildrenProc, METH_VARARGS|METH_KEYWORDS, 790 803 ChildrenProcDoc}, 791 */ 804 #endif 792 805 793 806 {"element", (PyCFunction)ElementProc, METH_VARARGS|METH_KEYWORDS, 794 807 ElementProcDoc}, 795 808 796 {"get", (PyCFunction)GetProc, METH_VARARGS|METH_KEYWORDS, GetProcDoc}, 797 798 /* 809 {"get", (PyCFunction)GetProc, METH_VARARGS|METH_KEYWORDS, 810 GetProcDoc}, 811 812 #ifdef notdef 799 813 {"parent", (PyCFunction)ParentProc, METH_VARARGS|METH_KEYWORDS, 800 801 */ 814 ParentProcDoc}, 815 #endif 802 816 803 817 {"put", (PyCFunction)PutProc, METH_VARARGS|METH_KEYWORDS, PutProcDoc}, 804 818 805 /* 819 #ifdef notdef 806 820 {"remove", (PyCFunction)RemoveProc, METH_VARARGS|METH_KEYWORDS, 807 821 RemoveProcDoc}, 808 */ 822 #endif 809 823 810 824 {"xml", (PyCFunction)XmlProc, METH_NOARGS, XmlProcDoc}, 811 825 812 {"result", (PyCFunction)ResultProc, METH_VARARGS|METH_KEYWORDS, 826 {"result", (PyCFunction)ResultProc, METH_VARARGS|METH_KEYWORDS, 813 827 ResultProcDoc}, 814 828 815 {NULL, NULL} 829 {NULL, NULL} /* sentinel */ 816 830 }; 817 831 … … 825 839 0, /*tp_itemsize*/ 826 840 /* methods */ 827 (destructor)FreeProc, 841 (destructor)FreeProc, /*tp_dealloc*/ 828 842 0, /*tp_print*/ 829 843 0, /*tp_getattr*/ … … 856 870 0, /*tp_descr_set*/ 857 871 0, /*tp_dictoffset*/ 858 (initproc)InitProc, 872 (initproc)InitProc, /*tp_init*/ 859 873 0, /*tp_alloc*/ 860 NewProc, 874 NewProc, /*tp_new*/ 861 875 0, /*tp_new*/ 862 876 }; … … 890 904 if (self != NULL) { 891 905 self->lib = lib; 892 } 893 else { 906 } else { 894 907 // raise error 895 908 PyErr_SetString(PyExc_RuntimeError, 896 "trouble creating new RpLibraryObject"); 897 } 898 } 899 909 "trouble creating new RpLibraryObject"); 910 } 911 } 900 912 return (PyObject *)self; 901 913 }
Note: See TracChangeset
for help on using the changeset viewer.