* Add an item to the identity cache. The storage location must not change * unless the cache is cleared. * * @param tb The mapping. * @param key The key, must be a C-array of pointers of the length * corresponding to the mapping. * @param value Normally a Python object, no reference counting is done. * use NULL to clear an item. If the item does not exist, no * actio
| 185 | * the RuntimeError. |
| 186 | */ |
| 187 | NPY_NO_EXPORT int |
| 188 | PyArrayIdentityHash_SetItem(PyArrayIdentityHash *tb, |
| 189 | PyObject *const *key, PyObject *value, int replace) |
| 190 | { |
| 191 | if (value != NULL && _resize_if_necessary(tb) < 0) { |
| 192 | /* Shrink, only if a new value is added. */ |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | PyObject **tb_item = find_item(tb, key); |
| 197 | if (value != NULL) { |
| 198 | if (tb_item[0] != NULL && !replace) { |
| 199 | PyErr_SetString(PyExc_RuntimeError, |
| 200 | "Identity cache already includes the item."); |
| 201 | return -1; |
| 202 | } |
| 203 | tb_item[0] = value; |
| 204 | memcpy(tb_item+1, key, tb->key_len * sizeof(PyObject *)); |
| 205 | tb->nelem += 1; |
| 206 | } |
| 207 | else { |
| 208 | /* Clear the bucket -- just the value should be enough though. */ |
| 209 | memset(tb_item, 0, (tb->key_len + 1) * sizeof(PyObject *)); |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | NPY_NO_EXPORT PyObject * |
no test coverage detected