@@ -70,18 +70,22 @@ static PyObject* py_SET(PyObject *self, PyObject *args) {
70
70
return NULL ;
71
71
}
72
72
73
- generic_port_instance_struct * port = PyCapsule_GetPointer (p -> port , "port" );
73
+ generic_port_instance_struct * port =
74
+ PyCapsule_GetPointer (p -> port , "port" );
74
75
if (port == NULL ) {
75
76
error_print ("Null pointer received." );
76
77
exit (1 );
77
78
}
78
79
79
- if (val ) {
80
+ if (val ) {
81
+ DEBUG_PRINT ("Setting value %p." , port -> value );
80
82
tmp = port -> value ;
81
83
Py_INCREF (val );
84
+ // Call the core lib API to set the port
82
85
_LF_SET (port , val );
83
86
84
- // Also set the values for the port capsule.
87
+ Py_INCREF (val );
88
+ // Also set the values for the port capsule.
85
89
p -> value = val ;
86
90
p -> is_present = true;
87
91
//Py_XDECREF(tmp); // Since value is allocated in Python, the Python garbage collector will manage and free this memory
@@ -288,7 +292,8 @@ port_capsule_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
288
292
self = (generic_port_capsule_struct * ) type -> tp_alloc (type , 0 );
289
293
if (self != NULL ) {
290
294
self -> port = NULL ;
291
- self -> value = NULL ;
295
+ Py_INCREF (Py_None );
296
+ self -> value = Py_None ;
292
297
self -> is_present = false;
293
298
self -> current_index = 0 ;
294
299
self -> width = -2 ;
@@ -366,29 +371,27 @@ port_iter_next(PyObject *self) {
366
371
static PyObject *
367
372
port_capsule_get_item (PyObject * self , PyObject * item ) {
368
373
generic_port_capsule_struct * port = (generic_port_capsule_struct * )self ;
369
- generic_port_capsule_struct * pyport = (generic_port_capsule_struct * )self -> ob_type -> tp_new (self -> ob_type , NULL , NULL );
374
+ generic_port_capsule_struct * pyport =
375
+ (generic_port_capsule_struct * )self -> ob_type -> tp_new (self -> ob_type , NULL , NULL );
370
376
long long index = -3 ;
371
377
372
378
// Port is not a multiport
373
- if (port -> width == -2 )
374
- {
379
+ if (port -> width == -2 ) {
375
380
return self ;
376
381
}
377
382
378
383
index = PyLong_AsLong (item );
379
- if (index == -3 )
380
- {
384
+ if (index == -3 ) {
381
385
PyErr_Format (PyExc_TypeError ,
382
386
"multiport indices must be integers, not %.200s" ,
383
387
Py_TYPE (item )-> tp_name );
384
388
return NULL ;
385
389
}
386
390
387
- if (index != -3 && port -> width > 0 )
388
- {
389
- generic_port_instance_struct * * cport = (generic_port_instance_struct * * )PyCapsule_GetPointer (port -> port ,"port" );
390
- if (cport == NULL )
391
- {
391
+ if (index != -3 && port -> width > 0 ) {
392
+ generic_port_instance_struct * * cport =
393
+ (generic_port_instance_struct * * )PyCapsule_GetPointer (port -> port ,"port" );
394
+ if (cport == NULL ) {
392
395
error_print_and_exit ("Null pointer received." );
393
396
}
394
397
@@ -398,11 +401,12 @@ port_capsule_get_item(PyObject *self, PyObject *item) {
398
401
pyport -> is_present = cport [index ]-> is_present ;
399
402
pyport -> width = -2 ;
400
403
404
+
405
+ LOG_PRINT ("Getting item index %d. Is present is %d." , index , pyport -> is_present );
401
406
}
402
407
403
408
404
- if (pyport -> value == NULL )
405
- {
409
+ if (pyport -> value == NULL ) {
406
410
Py_INCREF (Py_None );
407
411
pyport -> value = Py_None ;
408
412
}
@@ -831,8 +835,9 @@ PyObject* convert_C_port_to_py(void* port, int width) {
831
835
// Not a multiport
832
836
cport = (generic_port_instance_struct * )port ;
833
837
}
834
- // Create the action struct in Python
835
- PyObject * cap = PyObject_GC_New (generic_port_capsule_struct , & port_capsule_t );
838
+ // Create the port struct in Python
839
+ PyObject * cap =
840
+ PyObject_GC_New (generic_port_capsule_struct , & port_capsule_t );
836
841
if (cap == NULL ) {
837
842
error_print_and_exit ("Failed to convert port." );
838
843
}
@@ -848,7 +853,8 @@ PyObject* convert_C_port_to_py(void* port, int width) {
848
853
((generic_port_capsule_struct * )cap )-> width = width ;
849
854
850
855
if (width == -2 ) {
851
- ((generic_port_capsule_struct * )cap )-> is_present = cport -> is_present ;
856
+ ((generic_port_capsule_struct * )cap )-> is_present =
857
+ cport -> is_present ;
852
858
853
859
854
860
if (cport -> value == NULL ) {
@@ -860,8 +866,7 @@ PyObject* convert_C_port_to_py(void* port, int width) {
860
866
861
867
//Py_INCREF(cport->value);
862
868
((generic_port_capsule_struct * )cap )-> value = cport -> value ;
863
- }
864
- else {
869
+ } else {
865
870
// Value is absent
866
871
Py_INCREF (Py_None );
867
872
((generic_port_capsule_struct * )cap )-> value = Py_None ;
0 commit comments