Skip to content

Commit d0e3032

Browse files
committed
Progress on chasing error due to input multiport width not being set properly
1 parent e4f205c commit d0e3032

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

org.lflang/src/lib/Python/pythontarget.c

+26-21
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,22 @@ static PyObject* py_SET(PyObject *self, PyObject *args) {
7070
return NULL;
7171
}
7272

73-
generic_port_instance_struct* port = PyCapsule_GetPointer(p->port, "port");
73+
generic_port_instance_struct* port =
74+
PyCapsule_GetPointer(p->port, "port");
7475
if (port == NULL) {
7576
error_print("Null pointer received.");
7677
exit(1);
7778
}
7879

79-
if (val) {
80+
if (val) {
81+
DEBUG_PRINT("Setting value %p.", port->value);
8082
tmp = port->value;
8183
Py_INCREF(val);
84+
// Call the core lib API to set the port
8285
_LF_SET(port, val);
8386

84-
// Also set the values for the port capsule.
87+
Py_INCREF(val);
88+
// Also set the values for the port capsule.
8589
p->value = val;
8690
p->is_present = true;
8791
//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) {
288292
self = (generic_port_capsule_struct *) type->tp_alloc(type, 0);
289293
if (self != NULL) {
290294
self->port = NULL;
291-
self->value = NULL;
295+
Py_INCREF(Py_None);
296+
self->value = Py_None;
292297
self->is_present = false;
293298
self->current_index = 0;
294299
self->width = -2;
@@ -366,29 +371,27 @@ port_iter_next(PyObject *self) {
366371
static PyObject *
367372
port_capsule_get_item(PyObject *self, PyObject *item) {
368373
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);
370376
long long index = -3;
371377

372378
// Port is not a multiport
373-
if (port->width == -2)
374-
{
379+
if (port->width == -2) {
375380
return self;
376381
}
377382

378383
index = PyLong_AsLong(item);
379-
if (index == -3)
380-
{
384+
if (index == -3) {
381385
PyErr_Format(PyExc_TypeError,
382386
"multiport indices must be integers, not %.200s",
383387
Py_TYPE(item)->tp_name);
384388
return NULL;
385389
}
386390

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) {
392395
error_print_and_exit("Null pointer received.");
393396
}
394397

@@ -398,11 +401,12 @@ port_capsule_get_item(PyObject *self, PyObject *item) {
398401
pyport->is_present = cport[index]->is_present;
399402
pyport->width = -2;
400403

404+
405+
LOG_PRINT("Getting item index %d. Is present is %d.", index, pyport->is_present);
401406
}
402407

403408

404-
if (pyport->value == NULL)
405-
{
409+
if (pyport->value == NULL) {
406410
Py_INCREF(Py_None);
407411
pyport->value = Py_None;
408412
}
@@ -831,8 +835,9 @@ PyObject* convert_C_port_to_py(void* port, int width) {
831835
// Not a multiport
832836
cport = (generic_port_instance_struct *)port;
833837
}
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);
836841
if (cap == NULL) {
837842
error_print_and_exit("Failed to convert port.");
838843
}
@@ -848,7 +853,8 @@ PyObject* convert_C_port_to_py(void* port, int width) {
848853
((generic_port_capsule_struct*)cap)->width = width;
849854

850855
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;
852858

853859

854860
if (cport->value == NULL) {
@@ -860,8 +866,7 @@ PyObject* convert_C_port_to_py(void* port, int width) {
860866

861867
//Py_INCREF(cport->value);
862868
((generic_port_capsule_struct*)cap)->value = cport->value;
863-
}
864-
else {
869+
} else {
865870
// Value is absent
866871
Py_INCREF(Py_None);
867872
((generic_port_capsule_struct*)cap)->value = Py_None;

0 commit comments

Comments
 (0)