scripter plugin: on creating a dict's key

Previous topic - Next topic

a.l.e

in the scripter plugin, dict items are created in the following way:

const char path[] = "path";
PyObject *pathkey = PyUnicode_FromString(path);
PyObject *pathvalue = PyUnicode_FromString(a.Extern().toUtf8());
PyDict_SetItem(drv, pathkey, pathvalue);
https://gitlab.com/scribus/scribus/-/blob/master/scribus/plugins/scriptplugin/cmdannotations.cpp#L213

what's the reason for not "simply" doing:

PyObject *pathvalue = PyUnicode_FromString(a.Extern().toUtf8());
PyDict_SetItem(drv, PyUnicode_FromString("path"), pathvalue);

or even:

PyDict_SetItem(drv, PyUnicode_FromString("path"), PyUnicode_FromString(a.Extern().toUtf8());

?

Nermander

I am not a developer, but my guess is to make the code more clear. The compiler probably generates exactly the same code from all these, but for a human reading the code the first one is a lot more clear ("self-documenting" code).

a.l.e

the problem is that if you have several items to add to the dict, the code gets rather messy; with lot of temporary variables and intermediate steps that are only distracting.

on the other side, all those variables are pointers and their lifetime is managed together by python and c++: so it it might be that by going for a single line, you get temporary variables that some times disappear without notice.

and if it works in my tests, it does not mean that it will also always work on everybody's computers when the script is run.
(and from what i've found in the internets, there seem to have had bugs in the past that at least have led to memory leaks: that might also be a reason why the developer having written the code was over zealous.)