summaryrefslogtreecommitdiffstats
path: root/Doc/c-api/exceptions.rst
diff options
context:
space:
mode:
authorDaniel Stutzbach <daniel@stutzbachenterprises.com>2010-12-17 16:31:32 (GMT)
committerDaniel Stutzbach <daniel@stutzbachenterprises.com>2010-12-17 16:31:32 (GMT)
commit7cb3051dc304a0cf8da73a647b259a4dc215ea39 (patch)
treea4bba97b316f663cdd9ee0febae12bcbc2e038ac /Doc/c-api/exceptions.rst
parent94f58c3a650fa783ec85f3dbdfde98d4e2976f8d (diff)
downloadcpython-7cb3051dc304a0cf8da73a647b259a4dc215ea39.zip
cpython-7cb3051dc304a0cf8da73a647b259a4dc215ea39.tar.gz
cpython-7cb3051dc304a0cf8da73a647b259a4dc215ea39.tar.bz2
Issue 8753: Added documentation for Py_ReprEntr and Py_ReprLeave.
Diffstat (limited to 'Doc/c-api/exceptions.rst')
-rw-r--r--Doc/c-api/exceptions.rst29
1 files changed, 29 insertions, 0 deletions
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
index 894652e..db46902 100644
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -533,6 +533,35 @@ recursion depth automatically).
Ends a :c:func:`Py_EnterRecursiveCall`. Must be called once for each
*successful* invocation of :c:func:`Py_EnterRecursiveCall`.
+Properly implementing :attr:`tp_repr` for container types requires
+special recursion handling. In addition to protecting the stack,
+:attr:`tp_repr` also needs to track objects to prevent cycles. The
+following two functions facilitate this functionality. Effectively,
+these are the C equivalent to :func:`reprlib.recursive_repr`.
+
+.. c:function:: int Py_ReprEntr(PyObject *object)
+
+ Called at the beginning of the :attr:`tp_repr` implementation to
+ detect cycles.
+
+ If the object has already been processed, the function returns a
+ positive integer. In that case the :attr:`tp_repr` implementation
+ should return a string object indicating a cycle. As examples,
+ :class:`dict` objects return ``{...}`` and :class:`list` objects
+ return ``[...]``.
+
+ The function will return a negative integer if the recursion limit
+ is reached. In that case the :attr:`tp_repr` implementation should
+ typically return ``NULL``.
+
+ Otherwise, the function returns zero and the :attr:`tp_repr`
+ implementation can continue normally.
+
+.. c:function:: void Py_ReprLeave(PyObject *object)
+
+ Ends a :c:func:`Py_ReprEntr`. Must be called once for each
+ invocation of :c:func:`Py_ReprEntr` that returns zero.
+
.. _standardexceptions: