Skip to content

Commit cac2eb4

Browse files
committed
Add PyUnstable_EnableTryIncRef to the example
1 parent 405952c commit cac2eb4

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Doc/c-api/object.rst

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,20 +642,31 @@ Object Protocol
642642
643643
.. code-block:: c
644644
645+
PyMutex mutex;
646+
647+
PyObject *
648+
add_entry(weakmap_key_type *key, PyObject* value)
649+
{
650+
PyUnstable_EnableTryIncRef(value);
651+
weakmap_type weakmap = ...;
652+
weakmap_add_entry(weakmap, key, value);
653+
Py_RETURN_NONE;
654+
}
655+
645656
PyObject *
646657
get_value(weakmap_key_type *key)
647658
{
648659
weakmap_type weakmap = ...;
649-
weakmap_lock_mutex(weakmap);
660+
PyMutex_Lock(mutex);
650661
PyObject *result = weakmap_find(weakmap, key);
651662
if (PyUnstable_TryIncRef(result)) {
652663
// `result` is safe to use
653-
weakmap_unlock_mutex(weakmap);
664+
PyMutex_Unlock(mutex);
654665
return result;
655666
}
656667
// if we get here, `result` is starting to be garbage-collected,
657668
// but has not been removed from the weakmap yet
658-
weakmap_unlock_mutex(weakmap->mutex);
669+
PyMutex_Unlock(mutex);
659670
return NULL;
660671
}
661672
@@ -664,14 +675,13 @@ Object Protocol
664675
value_dealloc(PyObject *value)
665676
{
666677
weakmap_type weakmap = ...;
667-
weakmap_lock_mutex(weakmap);
678+
PyMutex_Lock(mutex);
668679
weakmap_remove_value(weakmap, value);
669680
670681
...
671-
weakmap_unlock_mutex(weakmap);
682+
PyMutex_Unlock(mutex);
672683
}
673684
674-
675685
.. versionadded:: 3.14
676686
677687
.. c:function:: void PyUnstable_EnableTryIncRef(PyObject *obj)

0 commit comments

Comments
 (0)