Skip to content

Commit 8cad7b9

Browse files
committed
pytrap: add capability to set arrays from dict
UniRec arrays were not set in UnirecTemplate.setFromDict() due to missing check for value sequence. New test ArrayFromDict was prepared to check this feature.
1 parent b5c0896 commit 8cad7b9

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pytrap/src/unirecmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ UnirecTemplate_setFromDict(pytrap_unirectemplate *self, PyObject *dict, int skip
15231523
Py_DECREF(idkey);
15241524
return NULL;
15251525
}
1526-
} else if (PyLong_Check(v)) {
1526+
} else if (PyLong_Check(v) || PySequence_Check(v)) {
15271527
if (UnirecTemplate_set_local(self, self->data, id, v) == NULL) {
15281528
Py_DECREF(idkey);
15291529
return NULL;

pytrap/test/unirectemplate_unittest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,3 +839,39 @@ def runTest(self):
839839
elapsed_time = time.process_time() - startt
840840
print(f"Elapsed time for {messages} messages is: {elapsed_time}")
841841

842+
class ArrayFromDict(unittest.TestCase):
843+
def runTest(self):
844+
import pytrap
845+
rec = pytrap.UnirecTemplate("int8* PPI_PKT_DIRECTIONS,uint8* PPI_PKT_FLAGS,uint16* PPI_PKT_LENGTHS,uint32* DBI_BRST_BYTES,time* DBI_BRST_TIME_START")
846+
rec.createMessage(10000)
847+
# prepare dict
848+
data = {
849+
"PPI_PKT_DIRECTIONS": [
850+
1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
851+
-1, -1, -1, -1, -1, -1, -1, 1, -1, -1
852+
],
853+
"PPI_PKT_FLAGS": [
854+
24, 16, 24, 16, 24, 24, 24, 24, 24, 24, 24, 24, 16, 24, 16, 24, 16, 24, 16,
855+
24, 16, 24, 16, 24, 16, 24, 16, 24, 24, 16
856+
],
857+
"PPI_PKT_LENGTHS": [
858+
517, 1400, 1400, 1400, 282, 74, 98, 416, 978, 31, 590, 1400, 1400, 1400,
859+
1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400,
860+
1400, 31, 1400, 1400
861+
],
862+
"DBI_BRST_BYTES": [
863+
71274
864+
],
865+
"DBI_BRST_TIME_START": [
866+
pytrap.UnirecTime(1669885132, 853),
867+
pytrap.UnirecTime(1669985132, 853)
868+
]
869+
}
870+
871+
# set it into UnirecTemplate
872+
rec.setFromDict(data)
873+
# get the content
874+
stored = rec.getDict()
875+
# compare it
876+
self.assertEqual(stored, data)
877+

0 commit comments

Comments
 (0)