/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pcilib/py.c

  • Committer: Vasilii Chernov
  • Date: 2016-02-11 11:00:54 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160211110054-h6x2oxdx1dqaekhe
Change error logging method in Python wrap. Move functions, that converts values between PyObject and pcilib_value_t to py.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
196
196
    return pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj));
197
197
}
198
198
 
 
199
void* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val)
 
200
{
 
201
        int err;
 
202
        
 
203
        switch(val->type)
 
204
        {
 
205
                case PCILIB_TYPE_INVALID:
 
206
                pcilib_error("Invalid register output type (PCILIB_TYPE_INVALID)");
 
207
                        return NULL;
 
208
                        
 
209
                case PCILIB_TYPE_STRING:
 
210
                pcilib_error("Invalid register output type (PCILIB_TYPE_STRING)");
 
211
                        return NULL;
 
212
                
 
213
                case PCILIB_TYPE_LONG:
 
214
                {
 
215
                        long ret;
 
216
                        ret = pcilib_get_value_as_int(ctx, val, &err);
 
217
                        
 
218
                        if(err)
 
219
                        {
 
220
                    pcilib_error("Failed: pcilib_get_value_as_int (%i)", err);
 
221
                                return NULL;
 
222
                        }
 
223
                        return (PyObject*)PyInt_FromLong((long) ret);
 
224
                }
 
225
                
 
226
                case PCILIB_TYPE_DOUBLE:
 
227
                {
 
228
                        double ret;
 
229
                        ret = pcilib_get_value_as_float(ctx, val, &err);
 
230
                        
 
231
                        if(err)
 
232
                        {
 
233
                    pcilib_error("Failed: pcilib_get_value_as_int (%i)", err);
 
234
                                return NULL;
 
235
                        }
 
236
                        return (PyObject*)PyFloat_FromDouble((double) ret);
 
237
                }
 
238
                
 
239
                default:
 
240
                pcilib_error("Invalid register output type (unknown)");
 
241
                        return NULL;
 
242
        }
 
243
}
 
244
 
 
245
int pcilib_convert_pyobject_to_val(pcilib_t* ctx, void* pyObjVal, pcilib_value_t *val)
 
246
{
 
247
        PyObject* pyVal = pyObjVal;
 
248
        int err;
 
249
        
 
250
    if(PyInt_Check(pyVal))
 
251
    {
 
252
        err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyVal));
 
253
    }
 
254
    else
 
255
        if(PyFloat_Check(pyVal))
 
256
            err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyVal));
 
257
        else
 
258
            if(PyString_Check(pyVal))
 
259
                err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(pyVal));
 
260
                else
 
261
                {
 
262
                    pcilib_error("Invalid input. Input type should be int, float or string.");
 
263
                    return PCILIB_ERROR_NOTSUPPORTED;
 
264
                }
 
265
    if(err)
 
266
        return err;
 
267
        
 
268
    return 0;
 
269
}
199
270
 
200
271
int pcilib_init_py_script(pcilib_t *ctx, char* module_name, pcilib_script_t **module, pcilib_access_mode_t *mode)
201
272
{
347
418
{
348
419
        int err;
349
420
                
350
 
        PyObject *input = pcilib_convert_val_to_pyobject(ctx, val, printf);
 
421
    PyObject *input = pcilib_convert_val_to_pyobject(ctx, val);
351
422
        if(!input)
352
423
        {
353
424
           printf("Failed to convert input value to Python object");