Skip to content

Commit 4b091af

Browse files
committed
document curses C API
1 parent 87942d9 commit 4b091af

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

Doc/c-api/curses.rst

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
.. highlight:: c
2+
3+
Curses C API
4+
------------
5+
6+
:mod:`curses` exposes a small C interface for extension modules.
7+
Consumers must include the header file :file:`py_curses.h` (which is not
8+
included by default by :file:`Python.h`) and :c:func:`import_curses` must
9+
be invoked, usually as part of the module initialisation function, to populate
10+
:c:var:`PyCurses_API`.
11+
12+
.. c:macro:: import_curses()
13+
14+
Import the curses C API. The macro does not need a semi-colon to be called.
15+
16+
On success, populate the :c:var:`PyCurses_API` pointer.
17+
18+
On failure, set :c:var:`PyCurses_API` to NULL and set an exception.
19+
The caller must check if an error occurred via :c:func:`PyErr_Occurred`:
20+
21+
.. code-block::
22+
23+
import_curses(); // semi-colon is optional but recommended
24+
if (PyErr_Occurred()) { /* cleanup */ }
25+
26+
27+
.. c:var:: void **PyCurses_API
28+
29+
Dynamically allocated object containing the curses C API.
30+
This variable is only available once :c:macro:`import_curses` succeeded.
31+
32+
``PyCurses_API[0]`` corresponds to :c:data:`PyCursesWindow_Type`.
33+
34+
``PyCurses_API[1]``, ``PyCurses_API[2]``, and ``PyCurses_API[3]``
35+
are pointers to predicate functions of type ``int (*)(void)``.
36+
37+
When called, these predicates return whether :func:`curses.setupterm`,
38+
:func:`curses.initscr`, and :func:`curses.start_color` have been called
39+
respectively.
40+
41+
See also the convenience macros :c:macro:`PyCursesSetupTermCalled`,
42+
:c:macro:`PyCursesInitialised`, and :c:macro:`PyCursesInitialisedColor`.
43+
44+
45+
.. c:data:: PyTypeObject PyCursesWindow_Type
46+
47+
The :ref:`heap type <heap-types>` corresponding to :class:`curses.window`.
48+
49+
50+
.. c:macro:: PyCursesWindow_Check(op)
51+
52+
Return *1* if *op* is an :class:`curses.window` instance, *0* otherwise.
53+
54+
The macro expansion is equivalent to:
55+
56+
.. code-block::
57+
58+
Py_IS_TYPE((op), &PyCursesWindow_Type)
59+
60+
61+
The following macros are convenience macros expanding into C statements.
62+
In particular, they can only be used as ``macro;`` or ``macro``, but not
63+
``macro()`` or ``macro();``.
64+
65+
.. c:macro:: PyCursesSetupTermCalled
66+
67+
Macro checking if :func:`curses.setupterm` has been called.
68+
69+
The macro expansion is roughly equivalent to:
70+
71+
.. code-block::
72+
73+
{
74+
typedef int (*predicate_t)(void);
75+
predicate_t was_setupterm_called = (predicate_t)PyCurses_API[1];
76+
if (!was_setupterm_called()) {
77+
return NULL;
78+
}
79+
}
80+
81+
82+
.. c:macro:: PyCursesInitialised
83+
84+
Macro checking if :func:`curses.initscr` has been called.
85+
86+
The macro expansion is roughly equivalent to:
87+
88+
.. code-block::
89+
90+
{
91+
typedef int (*predicate_t)(void);
92+
predicate_t was_initscr_called = (predicate_t)PyCurses_API[2];
93+
if (!was_initscr_called()) {
94+
return NULL;
95+
}
96+
}
97+
98+
99+
.. c:macro:: PyCursesInitialisedColor
100+
101+
Macro checking if :func:`curses.start_color` has been called.
102+
103+
The macro expansion is roughly equivalent to:
104+
105+
.. code-block::
106+
107+
{
108+
typedef int (*predicate_t)(void);
109+
predicate_t was_start_color_called = (predicate_t)PyCurses_API[3];
110+
if (!was_start_color_called()) {
111+
return NULL;
112+
}
113+
}
114+
115+
116+
Internal data
117+
-------------
118+
119+
The following objects are exposed by the C API but should be considered
120+
internal-only.
121+
122+
123+
.. c:macro:: PyCurses_API_pointers
124+
125+
The number of accessible fields in :c:var:`PyCurses_API`.
126+
127+
Internal usage only.
128+
129+
130+
.. c:macro:: PyCurses_CAPSULE_NAME
131+
132+
Name of the curses capsule to pass to :c:func:`PyCapsule_Import`.
133+
134+
Internal usage only. Use :c:macro:`import_curses` instead.
135+

0 commit comments

Comments
 (0)