def _func_hack_callback(hackref): """ Execute the cleanup action by the function hack weakref. Because the weakref callback can never be called until func's refcount is 0, we should NOT call Py_DECREF on the target index, or even access it to check, or we will segfault. But, we can be reasonably sure that the refcount is zero, so we can override that item. We don't have to worry about Py_DECREF being called on an undefined object because of our shoice of function, but if the C API implementation changed, we would. @param hackref: _FuncHackRef @type hackref: _FuncHackRef """ # refcount of new_func should be 3: # ref in calling function (1), ref as parameter here (2), # and reference held by the frame object (3) # func_rc = _Py_ssize_t.from_address(hackref.f_addr) # Remove the reference from the cache! _hack_cache.remove(hackref) _Py_INCREF(None) _tuple_set_item(hackref.f_const, hackref.f_index, None)
def _tuple_hack_callback(hackref): """ @param hackref: _TupleHackRef @type hackref: _TupleHackRef @return: None @rtype: None """ _hack_cache.remove(hackref) v = hackref.replacement _Py_INCREF(v) _tuple_set_item(hackref.tpl, hackref.t_index, v)