I'm wondering how memory management/reference counting works when a new value is set into an existing field within a PyDict (within a C extension).
For instance, assume a dictionary is created and populated in the following way:
myPyDict = PyDict_New();
tempPyObj = PyString_FromString("Original Value");
PyDict_SetItemString(myPyDict,"fieldname",tempPyObj);
Py_DECREF(tempPyObj);
From a memory and reference counting perspective, what happens when there is a subsequent
tempPyObj = PyString_FromString("New Value");
PyDict_SetItemString(myPyDict,"fieldname",tempPyObj);
Py_DECREF(tempPyObj);
Is the reference count for the original value automatically decremented (and the memory automatically released)?
The doc for PyList_SetItem
specifically mentions what happens for lists: This function “steals” a reference to item and discards a reference to an item already in the list at the affected position.
But neither PyDic_SetItem
nor PyDict_SetItemString
say how the replacement is handled for dictionaries.