Skip to content

Commit 2630329

Browse files
committed
Document Py_SAFE_DOWNCAST.
1 parent fa005af commit 2630329

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Doc/c-api/intro.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,22 @@ complete listing.
331331

332332
.. versionadded:: 3.11
333333

334+
.. c:macro:: Py_SAFE_DOWNCAST(value, larger, smaller)
335+
336+
Cast *value* to type *smaller* from type *larger*, validating that no
337+
information was lost.
338+
339+
On release builds of Python, this is roughly equivalent to
340+
:c:expr:`(smaller) value` (in C++, ``static_cast<smaller>(value)`` will be
341+
used instead).
342+
343+
On debug builds (implying that :c:macro:`Py_DEBUG` is defined), this asserts
344+
that no information was lost with the cast from *larger* to *smaller*.
345+
346+
*value*, *larger*, and *smaller* may all be evaluated more than once in the
347+
expression; as a consequence, do not pass an expensive operation directly to
348+
this macro.
349+
334350
.. c:macro:: Py_STRINGIFY(x)
335351
336352
Convert ``x`` to a C string. E.g. ``Py_STRINGIFY(123)`` returns

0 commit comments

Comments
 (0)