43
45
PyGILState_STATE gstate;
44
46
PyObject *pytype = NULL;
45
47
PyObject *pyval = NULL;
48
PyObject *pystr = NULL;
46
49
PyObject *pytraceback = NULL;
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);
58
# if PY_MAJOR_VERSION >= 3
60
if (PyUnicode_Check(pytype))
61
type = PyUnicode_AsUTF8(pytype);
63
type = PyExceptionClass_Name(pytype);
66
val = PyUnicode_AsUTF8(pystr);
68
# else /* PY_MAJOR_VERSION >= 3 */
70
if (PyString_Check(pytype))
71
type = PyString_AsString(pytype);
73
type = PyExceptionClass_Name(pytype);
76
val = PyString_AsString(pystr);
78
# endif /*PY_MAJOR_VERSION >= 3*/
55
80
PyGILState_Release(gstate);
56
81
#endif /* HAVE_PYTHON */
119
145
return PCILIB_ERROR_FAILED;
122
PyObject *pywrap = PyImport_ImportModule("pcipywrap");
148
PyObject *pywrap = PyImport_ImportModule(PCILIB_PYTHON_WRAPPER);
124
150
pcilib_python_error("Error importing pcilib python wrapper");
125
151
return PCILIB_ERROR_FAILED;
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;
169
pynewdir = PyString_FromString(script_dir);
195
pynewdir = PyUnicode_FromString(script_dir);
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();
178
pystr = PyString_FromString("cur");
204
pystr = PyUnicode_FromString("cur");
180
206
PyDict_SetItem(pydict, pystr, pynewdir);
181
207
Py_DECREF(pystr);
184
pystr = PyString_FromString("path");
210
pystr = PyUnicode_FromString("path");
186
212
PyDict_SetItem(pydict, pystr, pypath);
187
213
Py_DECREF(pystr);
292
318
return PCILIB_ERROR_FAILED;
295
pystr = PyString_FromString("read_from_register");
321
pystr = PyUnicode_FromString("read_from_register");
297
323
if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R;
298
324
Py_DECREF(pystr);
301
pystr = PyString_FromString("write_to_register");
327
pystr = PyUnicode_FromString("write_to_register");
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);
327
353
case PCILIB_TYPE_DOUBLE:
328
354
fval = pcilib_get_value_as_float(ctx, val, &err);
359
385
PyGILState_STATE gstate;
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);
400
err = pcilib_set_value_from_string(ctx, val, PyString_AsString(buf));
403
err = PCILIB_ERROR_FAILED;
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 */
369
410
PyGILState_Release(gstate);
370
411
pcilib_error("Can't convert PyObject to polymorphic pcilib value");