Ignore:
Timestamp:
Jul 26, 2012 1:34:19 PM (12 years ago)
Author:
gah
Message:

Made "install" target depend on "all".

Changed argument progress' percentage argument to now take a
double (floating point) value instead of an integer. The underlying
Rappture::Utils::progress routine may take only an integer, but this
allows python programs to compute a percentage without worrying about
the type of the result.

Fixed boolean handling routines to check what kind of python object
it is considering (boolean, long, double, or string), before converting
to a boolean (0 or 1) value.

File:
1 edited

Legend:

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

    r1527 r3113  
    1111#include <Python.h>
    1212#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
    1320
    1421#ifndef Py_RETURN_NONE
     
    2835
    2936static PyObject *ErrorObject;
    30 RpLibrary * RpLibraryObject_AsLibrary(PyObject *lib);
     37static RpLibrary * RpLibraryObject_AsLibrary(PyObject *lib);
    3138static PyObject * RpLibraryObject_FromLibrary(RpLibrary *lib);
    32 int boolAsInt(const char *inVal, int *outVal);
    33 int boolIntFromPyObject ( PyObject *inPyObj, const char *defaultVal,
    34                             const char *argName, int *boolVal);
    35 int getArgCount ( PyObject *args, PyObject *keywds, int *argc);
     39static int boolAsInt(const char *inVal, int *outVal);
     40static int boolIntFromPyObject (PyObject *inPyObj, const char *defaultVal,
     41        const char *argName, int *boolVal);
     42static int getArgCount(PyObject *args, PyObject *keywds, int *argc);
    3643
    3744typedef struct {
     
    4653 * correct initialization, use RpLibraryObject_IsValid.
    4754 */
    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;
     55static int
     56RpLibraryObject_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;
    6265}
    6366
     
    7073 * in by the user is a valid Rappture.library object
    7174 */
    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;
     75static int
     76RpLibraryObject_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;
    8790}
    8891
     
    97100    }
    98101    else {
    99         PyErr_SetString(PyExc_RuntimeError,"trouble creating new RpLibraryObject");
    100     }
    101 
     102        PyErr_SetString(PyExc_RuntimeError,
     103                "trouble creating new RpLibraryObject");
     104    }
    102105    return (PyObject *)self;
    103106}
     
    175178    char *frompath = (char *)"";
    176179    int argc = 0;
    177     int status = 0;
    178180    PyObject *fromobj = (PyObject *) self;
    179181
     
    191193    }
    192194
    193     status = getArgCount(args,keywds,&argc);
    194     if (status != 0) {
     195    if (getArgCount(args,keywds,&argc) != RP_OK) {
    195196        // trouble ensues
    196197        // error message was set in getArgCount()
     
    274275    }
    275276
    276     status = getArgCount(args,keywds,&argc);
    277     if (status != 0) {
     277    if (getArgCount(args,keywds,&argc) != RP_OK) {
    278278        // trouble ensues
    279279        // error message was set in getArgCount()
     
    346346
    347347    PyObject* decode = NULL;
    348     int decodeVal = 0;
     348    int decodeVal = FALSE;
    349349
    350350    PyObject* retVal = NULL;
     
    367367    }
    368368
    369     status = getArgCount(args,keywds,&argc);
    370     if (status != 0) {
     369    if (getArgCount(args,keywds,&argc) != RP_OK) {
    371370        // trouble ensues
    372371        // error message was set in getArgCount()
     
    387386        return NULL;
    388387    }
    389 
    390     status = boolIntFromPyObject(decode,"yes","decode",&decodeVal);
    391     if (status != 0) {
     388    if (boolIntFromPyObject(decode,"yes", "decode", &decodeVal) != RP_OK) {
    392389        // tested with GetTests.testArgumentsDecodeError()
    393390        return NULL;
    394391    }
    395392
    396     if (decodeVal == 1) {
     393    if (decodeVal) {
    397394        // tested with GetTests.testArgumentsDecode()
    398395        // tested with GetTests.testArgumentsDecodeYes()
     
    413410        retVal = PyString_FromStringAndSize(retValBuf.bytes(),retValBuf.size());
    414411    }
    415 
    416412    return (PyObject *)retVal;
    417413}
     
    448444    };
    449445
    450     status = getArgCount(args,keywds,&argc);
    451     if (status != 0) {
     446    if (getArgCount(args,keywds,&argc) != RP_OK) {
    452447        // trouble ensues
    453448        // error message was set in getArgCount()
     
    459454        return NULL;
    460455    }
    461 
    462456    if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss",
    463457            kwlist, &path, &as)) {
    464458        /* 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    }
    469463    if (self->lib) {
    470464        retlib = self->lib->parent(std::string(path));
    471     }
    472     else {
     465    } else {
    473466        PyErr_SetString(PyExc_RuntimeError,
    474467            "incorrectly initialized Rappture Library Object");
     
    477470
    478471    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))) {
    482473            retVal = RpLibraryObject_FromLibrary(retlib);
    483         }
    484         else if ((*as == 'c') && (strcmp("component",as) == 0)) {
     474        } else if ((*as == 'c') && (strcmp("component",as) == 0)) {
    485475            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)) {
    488477            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)) {
    491479            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)) {
    494481            retVal = PyString_FromString(retlib->nodePath().c_str());
    495         }
    496         else {
     482        } else {
    497483            PyErr_Format(PyExc_ValueError,
    498484                "parent() \'as\' arg must be \'object\' or \'component\' or \'id\' or \'type\' or \'path\'");
    499485        }
    500486    }
    501 
    502487    return (PyObject *)retVal;
    503488}
     
    544529
    545530    PyObject* append = NULL;
    546     int appendInt = 0;
     531    int appendInt = FALSE;
    547532
    548533    char *type = (char *)"string";
    549534
    550535    PyObject* compress = NULL;
    551     int compressInt = 0;
     536    int compressInt = FALSE;
    552537
    553538    int argc = 0;
     
    827812 * used to create a RpLibrary object from a PyObject
    828813 */
    829 RpLibrary *
     814static RpLibrary *
    830815RpLibraryObject_AsLibrary(PyObject *lib)
    831816{
     
    882867 */
    883868
    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) {
     869static int
     870boolAsInt(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 {
    900898        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
     905static int
     906boolIntFromPyObject (PyObject *objPtr, const char *defValue,
     907                     const char *argName, int *resultPtr)
     908{
     909    int value;
     910
     911    if ((defValue == NULL) || (argName == NULL) || (resultPtr == NULL)) {
    952912        // incorrect use of function
    953913        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) {
    968942            PyErr_Format(PyExc_TypeError,
    969943                "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
     952static int
     953getArgCount (PyObject *args, PyObject *keywds, int *argc)
     954{
    991955    int args_cnt = 0;
    992956    int keywds_cnt = 0;
     
    996960        // argc cannot be null
    997961        PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL");
    998         return status;
    999     }
    1000 
     962        return RP_ERROR;
     963    }
    1001964    if (args != NULL) {
    1002965        if (!PyTuple_Check(args)) {
    1003966            PyErr_Format(PyExc_TypeError,
    1004967                "getArgCount(): \'args\' should be a PyTuple");
    1005             return status;
     968            return RP_ERROR;
    1006969        }
    1007970        args_cnt = PyTuple_Size(args);
    1008971    }
    1009 
    1010972    if (keywds != NULL) {
    1011973        if (!PyDict_Check(keywds)) {
    1012974            PyErr_Format(PyExc_TypeError,
    1013975                "getArgCount(): \'keywds\' should be a PyDict");
    1014             return status;
     976            return RP_ERROR;
    1015977        }
    1016978        keywds_cnt = PyDict_Size(keywds);
    1017979    }
    1018 
    1019980    *argc = args_cnt + keywds_cnt;
    1020     status = 0;
    1021 
    1022     return status;
     981    return RP_OK;
    1023982}
    1024983
Note: See TracChangeset for help on using the changeset viewer.