/alps/pcitool

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pcilib/py.c

  • Committer: Vasilii Chernov
  • Date: 2016-03-02 13:38:59 UTC
  • mto: This revision was merged to the branch mainline in revision 367.
  • Revision ID: vchernov@inr.ru-20160302133859-vtvqokdu4rmkw3n9
Add Python3 support

Show diffs side-by-side

added added

removed removed

Lines of Context:
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);
 
52
      
 
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);
 
59
    Py_DecRef(buf);
 
60
    
 
61
    buf = PyUnicode_AsASCIIString(pyval);
 
62
    val = PyString_AsString(buf);
 
63
    Py_DecRef(buf);
 
64
#endif /*PY_MAJOR_VERSION >= 3*/
 
65
 
 
66
      
54
67
    }
55
68
    PyGILState_Release(gstate);
56
69
#endif /* HAVE_PYTHON */
101
114
    if(!Py_IsInitialized()) {
102
115
        Py_Initialize();
103
116
        
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;
126
139
    }
127
140
        
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);
 
143
    
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;
167
181
    }
168
182
 
169
 
    pynewdir = PyString_FromString(script_dir);
 
183
    pynewdir = PyUnicode_FromString(script_dir);
 
184
    
170
185
    if (!pynewdir) {
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();
177
192
    if (pydict) {
178
 
           pystr = PyString_FromString("cur");
 
193
      pystr = PyUnicode_FromString("cur");
179
194
        if (pystr) {
180
195
          PyDict_SetItem(pydict, pystr, pynewdir);
181
196
          Py_DECREF(pystr);
182
197
      }
183
198
 
184
 
      pystr = PyString_FromString("path");
 
199
      pystr = PyUnicode_FromString("path");
185
200
      if (pystr) {
186
201
          PyDict_SetItem(pydict, pystr, pypath);
187
202
          Py_DECREF(pystr);
292
307
      return PCILIB_ERROR_FAILED;
293
308
    }
294
309
    
295
 
    pystr = PyString_FromString("read_from_register");
 
310
    pystr = PyUnicode_FromString("read_from_register");
296
311
    if (pystr) {
297
312
      if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R;
298
313
      Py_DECREF(pystr);
299
314
    }
300
315
 
301
 
    pystr = PyString_FromString("write_to_register");
 
316
    pystr = PyUnicode_FromString("write_to_register");
302
317
    if (pystr) {
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);
326
341
      break;
327
342
        case PCILIB_TYPE_DOUBLE:
328
343
      fval = pcilib_get_value_as_float(ctx, val, &err);
359
374
    PyGILState_STATE gstate;
360
375
        
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));
 
382
    } else
 
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));
 
394
        Py_DecRef(buf);
 
395
#endif /*PY_MAJOR_VERSION >= 3*/
368
396
    } else {
369
397
        PyGILState_Release(gstate);
370
398
        pcilib_error("Can't convert PyObject to polymorphic pcilib value");