/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: Suren A. Chilingaryan
  • Date: 2016-03-02 22:36:19 UTC
  • mfrom: (346.1.35 pcitool)
  • Revision ID: csa@suren.me-20160302223619-r1zd62x6kwbyd321
Further improvements of Python scripting and web-interface API for register manipulations by Vasiliy Chernov

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include "error.h"
18
18
 
19
19
#ifdef HAVE_PYTHON
 
20
#define PCILIB_PYTHON_WRAPPER "pcipywrap"
 
21
 
20
22
typedef struct pcilib_script_s pcilib_script_t;
21
23
 
22
24
struct pcilib_script_s {
43
45
    PyGILState_STATE gstate;
44
46
    PyObject *pytype = NULL;
45
47
    PyObject *pyval = NULL;
 
48
    PyObject *pystr = NULL;
46
49
    PyObject *pytraceback = NULL;
47
50
 
48
51
 
49
52
    gstate = PyGILState_Ensure();
50
53
    if (PyErr_Occurred()) {
51
54
        PyErr_Fetch(&pytype, &pyval, &pytraceback);
52
 
        type = PyString_AsString(pytype);
53
 
        val = PyString_AsString(pyval);
 
55
        PyErr_NormalizeException(&pytype, &pyval, &pytraceback);
 
56
        if (pyval) pystr = PyObject_Str(pyval);
 
57
 
 
58
# if PY_MAJOR_VERSION >= 3
 
59
        if (pytype) {
 
60
            if (PyUnicode_Check(pytype))
 
61
                type = PyUnicode_AsUTF8(pytype);
 
62
            else
 
63
                type = PyExceptionClass_Name(pytype);
 
64
        }
 
65
        if (pystr) {
 
66
            val = PyUnicode_AsUTF8(pystr);
 
67
        }
 
68
# else /* PY_MAJOR_VERSION >= 3 */
 
69
        if (pytype) {
 
70
            if (PyString_Check(pytype))
 
71
                type = PyString_AsString(pytype);
 
72
            else
 
73
                type = PyExceptionClass_Name(pytype);
 
74
        }
 
75
        if (pystr) {
 
76
            val = PyString_AsString(pystr);
 
77
        }
 
78
# endif /*PY_MAJOR_VERSION >= 3*/
54
79
    }
55
80
    PyGILState_Release(gstate);
56
81
#endif /* HAVE_PYTHON */
57
 
    
 
82
 
58
83
    va_start(va, msg);
59
84
    if (type) {
60
85
        char *str;
83
108
    va_end(va);
84
109
 
85
110
#ifdef HAVE_PYTHON
 
111
    if (pystr) Py_DECREF(pystr);
86
112
    if (pytype) Py_DECREF(pytype);
87
113
    if (pyval) Py_DECREF(pyval);
88
114
    if (pytraceback) Py_DECREF(pytraceback);
119
145
        return PCILIB_ERROR_FAILED;
120
146
    }
121
147
 
122
 
    PyObject *pywrap = PyImport_ImportModule("pcipywrap");
 
148
    PyObject *pywrap = PyImport_ImportModule(PCILIB_PYTHON_WRAPPER);
123
149
    if (!pywrap) {
124
150
        pcilib_python_error("Error importing pcilib python wrapper");
125
151
        return PCILIB_ERROR_FAILED;
126
152
    }
127
153
        
128
 
    PyObject *mod_name = PyString_FromString("Pcipywrap");
129
 
    PyObject *pyctx = PyCObject_FromVoidPtr(ctx, NULL);
 
154
    PyObject *mod_name = PyUnicode_FromString(PCILIB_PYTHON_WRAPPER);
 
155
    PyObject *pyctx = PyCapsule_New(ctx, "pcilib", NULL);
130
156
    ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, pyctx, NULL);
131
157
    Py_XDECREF(pyctx);
132
158
    Py_XDECREF(mod_name);
166
192
        return PCILIB_ERROR_FAILED;
167
193
    }
168
194
 
169
 
    pynewdir = PyString_FromString(script_dir);
 
195
    pynewdir = PyUnicode_FromString(script_dir);
170
196
    if (!pynewdir) {
171
197
        pcilib_python_error("Can't create python string");
172
198
        return PCILIB_ERROR_MEMORY;
175
201
        // Checking if the directory already in the path?
176
202
    pydict = PyDict_New();
177
203
    if (pydict) {
178
 
        pystr = PyString_FromString("cur");
 
204
        pystr = PyUnicode_FromString("cur");
179
205
        if (pystr) {
180
206
            PyDict_SetItem(pydict, pystr, pynewdir);
181
207
            Py_DECREF(pystr);
182
208
        }
183
209
 
184
 
        pystr = PyString_FromString("path");
 
210
        pystr = PyUnicode_FromString("path");
185
211
        if (pystr) {
186
212
            PyDict_SetItem(pydict, pystr, pypath);
187
213
            Py_DECREF(pystr);
292
318
        return PCILIB_ERROR_FAILED;
293
319
    }
294
320
    
295
 
    pystr = PyString_FromString("read_from_register");
 
321
    pystr = PyUnicode_FromString("read_from_register");
296
322
    if (pystr) {
297
323
        if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R;
298
324
        Py_DECREF(pystr);
299
325
    }
300
326
 
301
 
    pystr = PyString_FromString("write_to_register");
 
327
    pystr = PyUnicode_FromString("write_to_register");
302
328
    if (pystr) {
303
329
        if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W;
304
330
        Py_DECREF(pystr);
322
348
    switch(val->type) {
323
349
     case PCILIB_TYPE_LONG:
324
350
        ival = pcilib_get_value_as_int(ctx, val, &err);
325
 
        if (!err) res = (PyObject*)PyInt_FromLong(ival);
 
351
        if (!err) res = (PyObject*)PyLong_FromLong(ival);
326
352
        break;
327
353
     case PCILIB_TYPE_DOUBLE:
328
354
        fval = pcilib_get_value_as_float(ctx, val, &err);
359
385
    PyGILState_STATE gstate;
360
386
        
361
387
    gstate = PyGILState_Ensure();
362
 
    if (PyInt_Check(pyval)) {
363
 
        err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval));
 
388
    if (PyLong_Check(pyval)) {
 
389
        err = pcilib_set_value_from_int(ctx, val, PyLong_AsLong(pyval));
364
390
    } else if (PyFloat_Check(pyval)) {
365
391
        err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyval));
 
392
#if PY_MAJOR_VERSION < 3
 
393
    } else if (PyInt_Check(pyval)) {
 
394
        err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval));
366
395
    } else if (PyString_Check(pyval)) {
367
396
        err = pcilib_set_value_from_string(ctx, val, PyString_AsString(pyval));
 
397
    } else if (PyUnicode_Check(pyval)) {
 
398
        PyObject *buf = PyUnicode_AsASCIIString(pyval);
 
399
        if (buf) {
 
400
            err = pcilib_set_value_from_string(ctx, val, PyString_AsString(buf));
 
401
            Py_DecRef(buf);
 
402
        } else {
 
403
            err = PCILIB_ERROR_FAILED;
 
404
        }
 
405
#else /* PY_MAJOR_VERSION < 3 */
 
406
    } else if (PyUnicode_Check(pyval)) {
 
407
        err = pcilib_set_value_from_string(ctx, val, PyUnicode_AsUTF8(pyval));
 
408
#endif /* PY_MAJOR_VERSION < 3 */
368
409
    } else {
369
410
        PyGILState_Release(gstate);
370
411
        pcilib_error("Can't convert PyObject to polymorphic pcilib value");