Changeset 3117 for trunk/lang


Ignore:
Timestamp:
Jul 26, 2012, 6:58:17 PM (12 years ago)
Author:
gah
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lang/python/Rappture/PyRpLibrary.cc

    r3115 r3117  
    3737static RpLibrary * RpLibraryObject_AsLibrary(PyObject *lib);
    3838static 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);
     39static int StringToBoolean(const char *inVal, int *resultPtr);
     40static int PyObjectToBoolean(PyObject *objPtr, const char *defaultVal,
     41        const char *argName, int *resultPtr);
    4242static int getArgCount(PyObject *args, PyObject *keywds, int *argc);
    4343
     
    6363    }
    6464    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
     87static int
     88StringToBoolean(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
     122static int
     123PyObjectToBoolean(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
     179static int
     180getArgCount (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;
    65209}
    66210
     
    91235
    92236static PyObject *
    93 RpLibraryObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     237NewProc(PyTypeObject *type, PyObject *args, PyObject *kwds)
    94238{
    95239    RpLibraryObject *self = NULL;
     
    107251
    108252static 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)) {
     253InitProc(RpLibraryObject *self, PyObject *args, PyObject *kwds)
     254{
     255    PyObject *objPtr;
     256
     257    if (!PyArg_ParseTuple(args, "|O", &objPtr)) {
    116258        PyErr_Format(PyExc_TypeError,
    117259            "library() takes at most 1 argument, a file name or a Rappture Library Object");
    118260        return -1;
    119261    }
    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);
    124267            if (filename == NULL) {
    125268                PyErr_Format(PyExc_ValueError,"a file name is required");
    126269            }
    127270            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)) {
    133274            self->lib = new RpLibrary();
    134         }
    135         else {
     275        } else {
    136276            PyErr_Format(PyExc_TypeError,"unrecognized object type");
    137277            return -1;
    138         }
    139     }
    140     else {
     278        }
     279    } else {
    141280        self->lib = new RpLibrary();
    142281    }
    143282
    144     return retVal;
     283    return -1;
    145284}
    146285
    147286static void
    148 RpLibraryObject_dealloc(RpLibraryObject *self)
     287FreeProc(RpLibraryObject *self)
    149288{
    150289    if (self) {
     
    156295}
    157296
    158 PyDoc_STRVAR(RpLibraryObject_copy_doc,
     297PyDoc_STRVAR(CopyProcDoc,
    159298"copy (topath, frompath [, fromobj=$self]) -> None, copies data\n\
    160299\n\
     
    173312
    174313static PyObject *
    175 RpLibraryObject_copy(RpLibraryObject *self, PyObject *args, PyObject *keywds)
     314CopyProc(RpLibraryObject *self, PyObject *args, PyObject *keywds)
    176315{
    177316    char *topath = (char *)"";
     
    235374}
    236375
    237 PyDoc_STRVAR(RpLibraryObject_element_doc,
     376PyDoc_STRVAR(ElementProcDoc,
    238377"element ([path=\'\'][, as=\'object\']) -> returns string or Rappture Library Object\n\
    239378\n\
     
    254393
    255394static PyObject *
    256 RpLibraryObject_element(RpLibraryObject *self, PyObject *args, PyObject *keywds)
     395ElementProc(RpLibraryObject *self, PyObject *args, PyObject *keywds)
    257396{
    258397    char* path = (char *)"";
     
    275414
    276415    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    }
    282418    if (argc > 2) {
    283         // tested with ElementTests.testArguments_TooManyArgs()
    284419        PyErr_Format(PyExc_TypeError,
    285420            "element() takes at most 2 arguments (%i given)", argc);
     
    287422    }
    288423
    289     if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss",
    290                 kwlist, &path, &as)) {
     424    if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss", kwlist, &path, &as)) {
    291425        /* incorrect input values */
    292426        // tested with ElementTests.testArguments_ArgsWrongType2()
     
    298432
    299433    if (retlib != NULL) {
    300         if (   (as == NULL)
    301             || ((*as == 'o') && (strcmp("object",as) == 0)) ) {
     434        if ((as == NULL) || ((*as == 'o') && (strcmp("object",as) == 0)) ) {
    302435            // tested with ElementTests.testArguments_PathArg()
    303436            retVal = RpLibraryObject_FromLibrary(retlib);
    304         }
    305         else if ((*as == 'c') && (strcmp("component",as) == 0)) {
     437        } else if ((*as == 'c') && (strcmp("component",as) == 0)) {
    306438            // tested with ElementTests.testArguments_TwoArgs()
    307439            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)) {
    310441            // tested with ElementTests.testArguments_AsId()
    311442            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)) {
    314444            // tested with ElementTests.testArguments_AsKeywordArgs()
    315445            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)) {
    318447            // tested with ElementTests.testArguments_TwoKeywordArgs()
    319448            retVal = PyString_FromString(retlib->nodePath().c_str());
    320         }
    321         else {
     449        } else {
    322450            // tested with ElementTests.testArguments_UnrecognizedAs()
    323451            PyErr_Format(PyExc_ValueError,
     
    325453        }
    326454    }
    327 
    328455    return (PyObject *)retVal;
    329456}
    330457
    331458
    332 PyDoc_STRVAR(RpLibraryObject_get_doc,
     459PyDoc_STRVAR(GetProcDoc,
    333460"get ([path=\'\'][, decode=\'True\']) -> returns data at \'path\' as string\n\
    334461\n\
     
    340467
    341468static PyObject *
    342 RpLibraryObject_get(RpLibraryObject *self, PyObject *args, PyObject *keywds)
     469GetProc(RpLibraryObject *self, PyObject *args, PyObject *keywds)
    343470{
    344471    char* path = (char *)"";
    345472
    346473    PyObject* decode = NULL;
    347     int decodeVal = FALSE;
     474    int decodeFlag;
    348475
    349476    PyObject* retVal = NULL;
     
    384511        return NULL;
    385512    }
    386     if (boolIntFromPyObject(decode,"yes", "decode", &decodeVal) != RP_OK) {
     513    if (PyObjectToBoolean(decode,"yes", "decode", &decodeFlag) != RP_OK) {
    387514        // tested with GetTests.testArgumentsDecodeError()
    388515        return NULL;
    389516    }
    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) {
    398518        retValStr = self->lib->get(std::string(path));
    399519        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 {
    407521        retValBuf = self->lib->getData(std::string(path));
    408522        retVal = PyString_FromStringAndSize(retValBuf.bytes(),retValBuf.size());
     
    411525}
    412526
    413 PyDoc_STRVAR(RpLibraryObject_parent_doc,
     527#ifdef notdef
     528PyDoc_STRVAR(ParentProcDoc,
    414529"parent ([path=\'\'][, as=\'object\']) -> returns string or Rappture Library Object\n\
    415530\n\
     
    427542
    428543static PyObject *
    429 RpLibraryObject_parent(RpLibraryObject *self, PyObject *args, PyObject *keywds)
     544ParentProc(RpLibraryObject *self, PyObject *args, PyObject *keywds)
    430545{
    431546    char* path = (char *)"";
     
    484599    return (PyObject *)retVal;
    485600}
    486 
    487 PyDoc_STRVAR(RpLibraryObject_put_doc,
     601#endif
     602
     603PyDoc_STRVAR(PutProcDoc,
    488604"put (path=\'\', value=\'\'[,id=None][,append=False][,type=\'string\'][,compress=False]) -> None\n\
    489605\n\
     
    514630
    515631static 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 
     632PutProc(RpLibraryObject *self, PyObject *args, PyObject *keywds)
     633{
     634    char *path;
     635    PyObject *compressObjPtr, *appendObjPtr, *valueObjPtr, *strObjPtr;
     636    int appendFlag, compressFlag;
    525637    char *id = NULL;
    526 
    527     PyObject* append = NULL;
    528     int appendInt = FALSE;
    529 
    530638    char *type = (char *)"string";
    531 
    532     PyObject* compress = NULL;
    533     int compressInt = FALSE;
    534 
    535639    int argc = 0;
    536     int status = 0;
    537640
    538641    static char *kwlist[] = {
     
    551654        return NULL;
    552655    }
    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    }
    561659    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    }
    568664    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) {
    583674        PyErr_Format(PyExc_ValueError, "put()'s \'value\' arg is required");
    584675        return NULL;
    585676    }
    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) {
    610694            // user passed in an improper string
    611695            // exception raised within fxn
    612696            return NULL;
    613697        }
    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) {
    620700                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);
    622705            }
    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) ) {
    633707            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 {
    639711            PyErr_Format(PyExc_ValueError,
    640712                "\'type\' arg must be \'string\' or \'file\'");
    641713            return NULL;
    642714        }
    643     }
    644     else {
     715    } else {
    645716        PyErr_Format(PyExc_TypeError,
    646717            "put()'s \'value\' arg must be a string or Rappture Library Object");
    647718        return NULL;
    648719    }
    649 
    650720    Py_RETURN_NONE;
    651721}
    652722
    653 PyDoc_STRVAR(RpLibraryObject_xml_doc,
     723PyDoc_STRVAR(XmlProcDoc,
    654724"xml () -> returns xml string\n\
    655725\n\
     
    659729
    660730static PyObject*
    661 RpLibraryObject_xml(RpLibraryObject *self)
     731XmlProc(RpLibraryObject *self)
    662732{
    663733    PyObject *retStr = NULL;
     
    673743}
    674744
    675 PyDoc_STRVAR(RpLibraryObject_result_doc,
     745PyDoc_STRVAR(ResultProcDoc,
    676746"result ([status=0]) -> None, send results back to graphical user interface\n\
    677747\n\
     
    681751
    682752static PyObject*
    683 RpLibraryObject_result(RpLibraryObject *self, PyObject *args, PyObject *keywds)
     753ResultProc(RpLibraryObject *self, PyObject *args, PyObject *keywds)
    684754{
    685755    int argc = 0;
     
    696766        return NULL;
    697767    }
    698 
    699     status = getArgCount(args,keywds,&argc);
    700     if (status != 0) {
     768    if (getArgCount(args,keywds,&argc) != RP_OK) {
    701769        // trouble ensues
    702770        // error message was set in getArgCount()
     
    709777        return NULL;
    710778    }
    711 
    712779    if (!PyArg_ParseTupleAndKeywords(args, keywds, "|i", kwlist, &status)) {
    713780        // tested with ResultTests.testArguments_InvalidStatusArg()
     
    715782        return NULL;
    716783    }
    717 
    718     self->lib->put("tool.version.rappture.language","python");
     784    self->lib->put("tool.version.rappture.language", "python");
    719785    self->lib->result(status);
    720786
     
    724790static PyMethodDef RpLibraryObject_methods[] = {
    725791
    726     {"copy", (PyCFunction)RpLibraryObject_copy, METH_VARARGS|METH_KEYWORDS,
    727         RpLibraryObject_copy_doc},
     792    {"copy", (PyCFunction)CopyProc, METH_VARARGS|METH_KEYWORDS, CopyProcDoc},
    728793
    729794/*
    730     {"children", (PyCFunction)RpLibraryObject_children, METH_VARARGS|METH_KEYWORDS,
    731         RpLibraryObject_children_doc},
     795    {"children", (PyCFunction)ChildrenProc, METH_VARARGS|METH_KEYWORDS,
     796        ChildrenProcDoc},
    732797*/
    733798
    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},
    739803
    740804/*
    741     {"parent", (PyCFunction)RpLibraryObject_parent, METH_VARARGS|METH_KEYWORDS,
    742         RpLibraryObject_put_doc},
     805    {"parent", (PyCFunction)ParentProc, METH_VARARGS|METH_KEYWORDS,
     806         ParentProcDoc},
    743807*/
    744808
    745     {"put", (PyCFunction)RpLibraryObject_put, METH_VARARGS|METH_KEYWORDS,
    746         RpLibraryObject_put_doc},
     809    {"put", (PyCFunction)PutProc, METH_VARARGS|METH_KEYWORDS, PutProcDoc},
    747810
    748811/*
    749     {"remove", (PyCFunction)RpLibraryObject_remove, METH_VARARGS|METH_KEYWORDS,
    750         RpLibraryObject_remove_doc},
     812    {"remove", (PyCFunction)RemoveProc, METH_VARARGS|METH_KEYWORDS,
     813        RemoveProcDoc},
    751814*/
    752815
    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 */
    760822};
    761823
     
    769831    0,                                    /*tp_itemsize*/
    770832    /* methods */
    771     (destructor)RpLibraryObject_dealloc,  /*tp_dealloc*/
     833    (destructor)FreeProc,                 /*tp_dealloc*/
    772834    0,                                    /*tp_print*/
    773835    0,                                    /*tp_getattr*/
     
    800862    0,                                    /*tp_descr_set*/
    801863    0,                                    /*tp_dictoffset*/
    802     (initproc)RpLibraryObject_init,       /*tp_init*/
     864    (initproc)InitProc,                   /*tp_init*/
    803865    0,                                    /*tp_alloc*/
    804     RpLibraryObject_new,                  /*tp_new*/
     866    NewProc,                              /*tp_new*/
    805867    0,                                    /*tp_new*/
    806868};
     
    845907}
    846908
    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 could
    853  *  be associated with any of the following strings:
    854  *  "yes", "on", "true", "1".
    855  *
    856  *  outVal set to 0 if the boolean value inVal could
    857  *  be associated with any of the following strings:
    858  *  "no", "off", "false", "0".
    859  *
    860  *  returns a status integer to tell if the operation
    861  *  was successful (0) of if there was an error (!0)
    862  *
    863  *  note: string comparisons are case insensitive.
    864  */
    865 
    866 static int
    867 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 int
    902 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 function
    910         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 int
    959 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 function
    966         // argc cannot be null
    967         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 
    990909/* ---------- */
    991910
Note: See TracChangeset for help on using the changeset viewer.