Changeset 14097


Ignore:
Timestamp:
12/05/12 09:13:32 (12 years ago)
Author:
jschierm
Message:

NEW: Allow Python scalars to be embedded in one-element tuples or lists for module input.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp

    r14093 r14097  
    2323
    2424        /*return internal value: */
    25         if      (PyFloat_Check((PyArrayObject*)py_float))
     25        if      (PyFloat_Check(py_float))
    2626                dscalar=PyFloat_AsDouble(py_float);
    27         else if (PyInt_Check((PyArrayObject*)py_float))
     27        else if (PyInt_Check(py_float))
    2828                dscalar=(double)PyInt_AsLong(py_float);
    29         else if (PyLong_Check((PyArrayObject*)py_float))
     29        else if (PyLong_Check(py_float))
    3030                dscalar=PyLong_AsDouble(py_float);
    31         else if (PyBool_Check((PyArrayObject*)py_float))
     31        else if (PyBool_Check(py_float))
    3232                dscalar=(double)PyLong_AsLong(py_float);
     33        else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
     34                FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
     35        else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
     36                FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
    3337        else
    3438                _error_("unrecognized float type in input!");
     
    4448
    4549        /*return internal value: */
    46         if      (PyInt_Check((PyArrayObject*)py_long))
     50        if      (PyInt_Check(py_long))
    4751                iscalar=(int)PyInt_AsLong(py_long);
    48         else if (PyLong_Check((PyArrayObject*)py_long))
     52        else if (PyLong_Check(py_long))
    4953                iscalar=(int)PyLong_AsLong(py_long);
    50         else if (PyFloat_Check((PyArrayObject*)py_long))
     54        else if (PyFloat_Check(py_long))
    5155                iscalar=(int)PyFloat_AsDouble(py_long);
    52         else if (PyBool_Check((PyArrayObject*)py_long))
     56        else if (PyBool_Check(py_long))
    5357                iscalar=(int)PyLong_AsLong(py_long);
     58        else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
     59                FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
     60        else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
     61                FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
    5462        else
    5563                _error_("unrecognized long type in input!");
     
    6472        bool bscalar;
    6573
    66         /*check this is indeed a subtype of long type: */
    67         if(!PyBool_Check(py_boolean))_error_("expecting a boolean in input!");
    68 
    69         /*extract boolean: */
    70         bscalar=(bool)PyLong_AsLong(py_boolean);
    71 
    72         /*simple copy: */
     74        /*return internal value: */
     75        if      (PyBool_Check(py_boolean))
     76                bscalar=(bool)PyLong_AsLong(py_boolean);
     77        else if (PyInt_Check(py_boolean))
     78                bscalar=(bool)PyInt_AsLong(py_boolean);
     79        else if (PyLong_Check(py_boolean))
     80                bscalar=(bool)PyLong_AsLong(py_boolean);
     81        else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
     82                FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
     83        else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
     84                FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
     85        else
     86                _error_("unrecognized boolean type in input!");
     87
     88        /*output: */
    7389        *pscalar=bscalar;
    74 
    7590}
    7691/*}}}*/
     
    132147        }
    133148
    134         else if (PyFloat_Check((PyArrayObject*)py_matrix) ||
    135                          PyInt_Check((PyArrayObject*)py_matrix) ||
    136                          PyLong_Check((PyArrayObject*)py_matrix) ||
    137                          PyBool_Check((PyArrayObject*)py_matrix)) {
     149        else {
    138150                M=1;
    139151                N=1;
     
    141153                FetchData(&(matrix[0]),py_matrix);
    142154        }
    143 
    144         else
    145                 _error_("unrecognized array type in input!");
    146155
    147156        /*output: */
     
    208217        }
    209218
    210         else if (PyInt_Check((PyArrayObject*)py_matrix) ||
    211                          PyLong_Check((PyArrayObject*)py_matrix) ||
    212                          PyFloat_Check((PyArrayObject*)py_matrix) ||
    213                          PyBool_Check((PyArrayObject*)py_matrix)) {
     219        else {
    214220                M=1;
    215221                N=1;
     
    217223                FetchData(&(matrix[0]),py_matrix);
    218224        }
    219 
    220         else
    221                 _error_("unrecognized array type in input!");
    222225
    223226        /*output: */
Note: See TracChangeset for help on using the changeset viewer.