49
49
gstate = PyGILState_Ensure();
50
50
if (PyErr_Occurred()) {
51
51
PyErr_Fetch(&pytype, &pyval, &pytraceback);
52
type = PyString_AsString(pytype);
53
val = PyString_AsString(pyval);
53
#if PY_MAJOR_VERSION >= 3
54
type = PyUnicode_AsUTF8(pytype);
55
val = PyUnicode_AsUTF8(pyval);
56
#else /*PY_MAJOR_VERSION >= 3*/
57
PyObject *buf = PyUnicode_AsASCIIString(pytype);
58
type = PyString_AsString(buf);
61
buf = PyUnicode_AsASCIIString(pyval);
62
val = PyString_AsString(buf);
64
#endif /*PY_MAJOR_VERSION >= 3*/
55
68
PyGILState_Release(gstate);
56
69
#endif /* HAVE_PYTHON */
101
114
if(!Py_IsInitialized()) {
104
// Since python is being initializing from c programm, it needs to initialize threads to work properly with c threads
117
// Since python is being initializing from c programm, it needs to initialize threads to work properly with c threads
105
118
PyEval_InitThreads();
106
119
PyEval_ReleaseLock();
107
120
ctx->py->finalyze = 1;
125
138
return PCILIB_ERROR_FAILED;
128
PyObject *mod_name = PyString_FromString("Pcilib");
129
PyObject *pyctx = PyCObject_FromVoidPtr(ctx, NULL);
141
PyObject *mod_name = PyUnicode_FromString("Pcilib");
142
PyObject *pyctx = PyCapsule_New(ctx, "pcilib", NULL);
130
144
ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, pyctx, NULL);
131
145
Py_XDECREF(pyctx);
132
146
Py_XDECREF(mod_name);
166
180
return PCILIB_ERROR_FAILED;
169
pynewdir = PyString_FromString(script_dir);
183
pynewdir = PyUnicode_FromString(script_dir);
171
186
pcilib_python_error("Can't create python string");
172
187
return PCILIB_ERROR_MEMORY;
175
190
// Checking if the directory already in the path
176
191
pydict = PyDict_New();
178
pystr = PyString_FromString("cur");
193
pystr = PyUnicode_FromString("cur");
180
195
PyDict_SetItem(pydict, pystr, pynewdir);
181
196
Py_DECREF(pystr);
184
pystr = PyString_FromString("path");
199
pystr = PyUnicode_FromString("path");
186
201
PyDict_SetItem(pydict, pystr, pypath);
187
202
Py_DECREF(pystr);
292
307
return PCILIB_ERROR_FAILED;
295
pystr = PyString_FromString("read_from_register");
310
pystr = PyUnicode_FromString("read_from_register");
297
312
if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R;
298
313
Py_DECREF(pystr);
301
pystr = PyString_FromString("write_to_register");
316
pystr = PyUnicode_FromString("write_to_register");
303
318
if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W;
304
319
Py_DECREF(pystr);
322
337
switch(val->type) {
323
338
case PCILIB_TYPE_LONG:
324
339
ival = pcilib_get_value_as_int(ctx, val, &err);
325
if (!err) res = (PyObject*)PyInt_FromLong(ival);
340
if (!err) res = (PyObject*)PyLong_FromLong(ival);
327
342
case PCILIB_TYPE_DOUBLE:
328
343
fval = pcilib_get_value_as_float(ctx, val, &err);
359
374
PyGILState_STATE gstate;
361
376
gstate = PyGILState_Ensure();
377
#if PY_MAJOR_VERSION < 3
362
378
if (PyInt_Check(pyval)) {
363
err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval));
379
err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyval));
380
} else if (PyString_Check(pyval)) {
381
err = pcilib_set_value_from_string(ctx, val, PyString_AsString(pyval));
383
#endif /*PY_MAJOR_VERSION >= 3*/
384
if (PyLong_Check(pyval)) {
385
err = pcilib_set_value_from_int(ctx, val, PyLong_AsLong(pyval));
364
386
} else if (PyFloat_Check(pyval)) {
365
387
err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyval));
366
} else if (PyString_Check(pyval)) {
367
err = pcilib_set_value_from_string(ctx, val, PyString_AsString(pyval));
388
} else if (PyUnicode_Check(pyval)) {
389
#if PY_MAJOR_VERSION >= 3
390
err = pcilib_set_value_from_string(ctx, val, PyUnicode_AsUTF8(pyval));
391
#else /*PY_MAJOR_VERSION >= 3*/
392
PyObject *buf = PyUnicode_AsASCIIString(pyval);
393
err = pcilib_set_value_from_string(ctx, val, PyString_AsString(buf));
395
#endif /*PY_MAJOR_VERSION >= 3*/
369
397
PyGILState_Release(gstate);
370
398
pcilib_error("Can't convert PyObject to polymorphic pcilib value");