summaryrefslogtreecommitdiffstats
path: root/pywrap
diff options
context:
space:
mode:
authorVasilii Chernov <vchernov@inr.ru>2016-03-02 14:38:59 +0100
committerVasilii Chernov <vchernov@inr.ru>2016-03-02 14:38:59 +0100
commit02d0026e2df2ba5c68c0c1a67aec4437c9e8e8f3 (patch)
tree2714eb4c293453e04d4886c356d033bbd7923012 /pywrap
parent8e7c95957ee73d6c11ef28f7c0e2319a11103783 (diff)
downloadpcitool-02d0026e2df2ba5c68c0c1a67aec4437c9e8e8f3.tar.gz
pcitool-02d0026e2df2ba5c68c0c1a67aec4437c9e8e8f3.tar.bz2
pcitool-02d0026e2df2ba5c68c0c1a67aec4437c9e8e8f3.tar.xz
pcitool-02d0026e2df2ba5c68c0c1a67aec4437c9e8e8f3.zip
Add Python3 support
Diffstat (limited to 'pywrap')
-rw-r--r--pywrap/pcipywrap.c101
-rw-r--r--pywrap/test_pcilib.py45
2 files changed, 76 insertions, 70 deletions
diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c
index 0f6729e..a1092be 100644
--- a/pywrap/pcipywrap.c
+++ b/pywrap/pcipywrap.c
@@ -128,12 +128,12 @@ void add_pcilib_value_to_dict(pcilib_t* ctx, PyObject* dict, pcilib_value_t* val
if(py_val)
pcilib_pydict_set_item(dict,
- PyString_FromString(name),
+ PyUnicode_FromString(name),
py_val);
else
pcilib_pydict_set_item(dict,
- PyString_FromString("defvalue"),
- PyString_FromString("invalid"));
+ PyUnicode_FromString("defvalue"),
+ PyUnicode_FromString("invalid"));
}
PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_property_info_t listItem)
@@ -142,18 +142,18 @@ PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_proper
if(listItem.name)
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("name"),
- PyString_FromString(listItem.name));
+ PyUnicode_FromString("name"),
+ PyUnicode_FromString(listItem.name));
if(listItem.description)
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("description"),
- PyString_FromString(listItem.description));
+ PyUnicode_FromString("description"),
+ PyUnicode_FromString(listItem.description));
if(listItem.path)
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("path"),
- PyString_FromString(listItem.path));
+ PyUnicode_FromString("path"),
+ PyUnicode_FromString(listItem.path));
//serialize types
const char* type = "invalid";
@@ -175,40 +175,40 @@ PyObject * pcilib_convert_property_info_to_pyobject(pcilib_t* ctx, pcilib_proper
break;
}
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("type"),
- PyString_FromString(type));
+ PyUnicode_FromString("type"),
+ PyUnicode_FromString(type));
//serialize modes
PyObject* modes = PyList_New(0);
if((listItem.mode & PCILIB_ACCESS_R ) == PCILIB_REGISTER_R)
- pcilib_pylist_append(modes, PyString_FromString("R"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("R"));
if((listItem.mode & PCILIB_ACCESS_W ) == PCILIB_REGISTER_W)
- pcilib_pylist_append(modes, PyString_FromString("W"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("W"));
if((listItem.mode & PCILIB_ACCESS_RW ) == PCILIB_REGISTER_RW)
- pcilib_pylist_append(modes, PyString_FromString("RW"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("RW"));
if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT)
- pcilib_pylist_append(modes, PyString_FromString("NO_CHK"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("NO_CHK"));
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("mode"),
+ PyUnicode_FromString("mode"),
modes);
//serialize flags
PyObject* flags = PyList_New(0);
if((listItem.flags & PCILIB_LIST_FLAG_CHILDS ) == PCILIB_LIST_FLAG_CHILDS)
- pcilib_pylist_append(flags, PyString_FromString("childs"));
+ pcilib_pylist_append(flags, PyUnicode_FromString("childs"));
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("flags"),
+ PyUnicode_FromString("flags"),
flags);
if(listItem.unit)
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("unit"),
- PyString_FromString(listItem.unit));
+ PyUnicode_FromString("unit"),
+ PyUnicode_FromString(listItem.unit));
return pylistItem;
}
@@ -219,42 +219,42 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist
if(listItem.name)
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("name"),
- PyString_FromString(listItem.name));
+ PyUnicode_FromString("name"),
+ PyUnicode_FromString(listItem.name));
if(listItem.description)
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("description"),
- PyString_FromString(listItem.description));
+ PyUnicode_FromString("description"),
+ PyUnicode_FromString(listItem.description));
if(listItem.bank)
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("bank"),
- PyString_FromString(listItem.bank));
+ PyUnicode_FromString("bank"),
+ PyUnicode_FromString(listItem.bank));
//serialize modes
PyObject* modes = PyList_New(0);
if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R)
- pcilib_pylist_append(modes, PyString_FromString("R"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("R"));
if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W)
- pcilib_pylist_append(modes, PyString_FromString("W"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("W"));
if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW)
- pcilib_pylist_append(modes, PyString_FromString("RW"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("RW"));
if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C)
- pcilib_pylist_append(modes, PyString_FromString("W1C"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("W1C"));
if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C)
- pcilib_pylist_append(modes, PyString_FromString("RW1C"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("RW1C"));
if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I)
- pcilib_pylist_append(modes, PyString_FromString("W1I"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("W1I"));
if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I)
- pcilib_pylist_append(modes, PyString_FromString("RW1I"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("RW1I"));
if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT)
- pcilib_pylist_append(modes, PyString_FromString("NO_CHK"));
+ pcilib_pylist_append(modes, PyUnicode_FromString("NO_CHK"));
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("mode"),
+ PyUnicode_FromString("mode"),
modes);
pcilib_value_t defval = {0};
@@ -273,7 +273,7 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist
add_pcilib_value_to_dict(ctx, range, &minval, "min");
add_pcilib_value_to_dict(ctx, range, &maxval, "max");
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("range"),
+ PyUnicode_FromString("range"),
range);
}
@@ -301,20 +301,20 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist
if(listItem.values[j].name)
pcilib_pydict_set_item(valuesItem,
- PyString_FromString("name"),
- PyString_FromString(listItem.values[j].name));
+ PyUnicode_FromString("name"),
+ PyUnicode_FromString(listItem.values[j].name));
if(listItem.values[j].description)
{
pcilib_pydict_set_item(valuesItem,
- PyString_FromString("description"),
- PyString_FromString(listItem.values[j].description));
+ PyUnicode_FromString("description"),
+ PyUnicode_FromString(listItem.values[j].description));
}
pcilib_pylist_append(values, valuesItem);
}
pcilib_pydict_set_item(pylistItem,
- PyString_FromString("values"),
+ PyUnicode_FromString("values"),
values);
}
@@ -340,16 +340,17 @@ Pcipywrap *new_Pcipywrap(const char* fpga_device, const char* model)
Pcipywrap *create_Pcipywrap(PyObject* ctx)
{
- if(!PyCObject_Check(ctx))
+ if(!PyCapsule_CheckExact(ctx))
{
- set_python_exception("Incorrect ctx type. Only PyCObject is allowed");
+ set_python_exception("Incorrect ctx type. Only PyCapsule is allowed");
return NULL;
}
Pcipywrap *self;
self = (Pcipywrap *) malloc(sizeof(Pcipywrap));
self->shared = 1;
- self->ctx = PyCObject_AsVoidPtr(ctx);
+ self->ctx = PyCapsule_GetPointer(ctx, PyCapsule_GetName(ctx));
+
return self;
}
@@ -415,7 +416,7 @@ PyObject* Pcipywrap_write_register(Pcipywrap *self, PyObject* val, const char *r
return NULL;
}
- return PyInt_FromLong((long)1);
+ return PyLong_FromLong((long)1);
}
PyObject* Pcipywrap_get_property(Pcipywrap *self, const char *prop)
@@ -453,7 +454,7 @@ PyObject* Pcipywrap_set_property(Pcipywrap *self, PyObject* val, const char *pro
return NULL;
}
- return PyInt_FromLong((long)1);
+ return PyLong_FromLong((long)1);
}
PyObject* Pcipywrap_get_registers_list(Pcipywrap *self, const char *bank)
@@ -542,7 +543,7 @@ PyObject* Pcipywrap_lock_global(Pcipywrap *self)
return NULL;
}
- return PyInt_FromLong((long)1);
+ return PyLong_FromLong((long)1);
}
void Pcipywrap_unlock_global(Pcipywrap *self)
@@ -570,7 +571,7 @@ PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id)
return NULL;
}
- return PyInt_FromLong((long)1);
+ return PyLong_FromLong((long)1);
}
PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id)
@@ -591,7 +592,7 @@ PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id)
return NULL;
}
- return PyInt_FromLong((long)1);
+ return PyLong_FromLong((long)1);
}
PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id)
@@ -606,7 +607,7 @@ PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id)
}
pcilib_unlock(lock);
- return PyInt_FromLong((long)1);
+ return PyLong_FromLong((long)1);
}
diff --git a/pywrap/test_pcilib.py b/pywrap/test_pcilib.py
index 69540ec..aed2dc3 100644
--- a/pywrap/test_pcilib.py
+++ b/pywrap/test_pcilib.py
@@ -55,17 +55,17 @@ class test_pcilib():
{'command': 'unlock_global'},
{'command': 'help'}]
r = requests.get(url, data=json.dumps(payload[message]), headers=headers)
- print json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))
+ print(json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': ')))
def testThreadSafeReadWrite(self):
def threadFunc():
if random.randint(0, 100) >= (self.write_percentage * 100):
ret = self.pcilib.get_property(self.prop)
- print self.register, ':', ret
+ print(self.register, ':', ret)
del ret
else:
val = random.randint(0, 65536)
- print 'set value:', val
+ print('set value:', val)
self.pcilib.set_property(val, self.prop)
try:
while(1):
@@ -74,25 +74,25 @@ class test_pcilib():
thread_list[i].start()
for i in range(0, self.num_threads):
thread_list[i].join()
- print 'cycle done'
+ print('cycle done')
except KeyboardInterrupt:
- print 'testing done'
+ print('testing done')
pass
def testMemoryLeak(self):
try:
while(1):
- val = random.randint(0, 8096)
+ val = long(random.randint(0, 8096))
self.pcilib = pcilib.Pcilib(self.device, self.model)
- print self.pcilib.get_property_list(self.branch)
- print self.pcilib.get_register_info(self.register)
- print self.pcilib.get_registers_list();
- print self.pcilib.write_register(val, self.register)
- print self.pcilib.read_register(self.register)
- print self.pcilib.set_property(val, self.prop)
- print self.pcilib.get_property(self.prop)
+ print(self.pcilib.get_property_list(self.branch))
+ print(self.pcilib.get_register_info(self.register))
+ print(self.pcilib.get_registers_list())
+ print(self.pcilib.write_register(val, self.register))
+ print(self.pcilib.read_register(self.register))
+ print(self.pcilib.set_property(val, self.prop))
+ print(self.pcilib.get_property(self.prop))
except KeyboardInterrupt:
- print 'testing done'
+ print('testing done')
pass
def testServer(self):
@@ -111,14 +111,19 @@ class test_pcilib():
def sendRandomMessage():
message_number = random.randint(1, len(payload) - 1)
- print 'message number: ', message_number
+ print('message number: ', message_number)
payload[message_number]['value'] = random.randint(0, 8096)
r = requests.get(url, data=json.dumps(payload[message_number]), headers=headers)
- print json.dumps(r.json(), sort_keys=True, indent=4, separators=(',', ': '))
-
+ if(r.headers['content-type'] == 'application/json'):
+ print(json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': ')))
+ else:
+ print(r.content)
try:
r = requests.get(url, data=json.dumps(payload[1]), headers=headers)
- print json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))
+ if(r.headers['content-type'] == 'application/json'):
+ print(json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': ')))
+ else:
+ print(r.content)
while(1):
time.sleep(self.server_message_delay)
@@ -127,10 +132,10 @@ class test_pcilib():
thread_list[i].start()
for i in range(0, self.num_threads):
thread_list[i].join()
- print 'cycle done'
+ print('cycle done')
except KeyboardInterrupt:
- print 'testing done'
+ print('testing done')
pass
if __name__ == '__main__':