Skip to content

Commit c88fece

Browse files
authored
Doc: define PY_SSIZE_T_CLEAN always (GH-12794)
1 parent a304b13 commit c88fece

File tree

10 files changed

+22
-4
lines changed

10 files changed

+22
-4
lines changed

Doc/c-api/intro.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Include Files
4848
All function, type and macro definitions needed to use the Python/C API are
4949
included in your code by the following line::
5050

51-
#include "Python.h"
51+
#define PY_SSIZE_T_CLEAN
52+
#include <Python.h>
5253

5354
This implies inclusion of the following standard headers: ``<stdio.h>``,
5455
``<string.h>``, ``<errno.h>``, ``<limits.h>``, ``<assert.h>`` and ``<stdlib.h>``
@@ -60,6 +61,9 @@ This implies inclusion of the following standard headers: ``<stdio.h>``,
6061
headers on some systems, you *must* include :file:`Python.h` before any standard
6162
headers are included.
6263

64+
It is recommended to always define ``PY_SSIZE_T_CLEAN`` before including
65+
``Python.h``. See :ref:`arg-parsing` for a description of this macro.
66+
6367
All user visible names defined by Python.h (except those defined by the included
6468
standard headers) have one of the prefixes ``Py`` or ``_Py``. Names beginning
6569
with ``_Py`` are for internal use by the Python implementation and should not be

Doc/extending/embedding.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ interface. This interface is intended to execute a Python script without needing
5353
to interact with the application directly. This can for example be used to
5454
perform some operation on a file. ::
5555

56+
#define PY_SSIZE_T_CLEAN
5657
#include <Python.h>
5758

5859
int

Doc/extending/extending.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ called ``spam``, the C file containing its implementation is called
5555
:file:`spammodule.c`; if the module name is very long, like ``spammify``, the
5656
module name can be just :file:`spammify.c`.)
5757

58-
The first line of our file can be::
58+
The first two lines of our file can be::
5959

60+
#define PY_SSIZE_T_CLEAN
6061
#include <Python.h>
6162

6263
which pulls in the Python API (you can add a comment describing the purpose of
@@ -68,6 +69,9 @@ the module and a copyright notice if you like).
6869
headers on some systems, you *must* include :file:`Python.h` before any standard
6970
headers are included.
7071

72+
It is recommended to always define ``PY_SSIZE_T_CLEAN`` before including
73+
``Python.h``. See :ref:`parsetuple` for a description of this macro.
74+
7175
All user-visible symbols defined by :file:`Python.h` have a prefix of ``Py`` or
7276
``PY``, except those defined in standard header files. For convenience, and
7377
since they are used extensively by the Python interpreter, ``"Python.h"``
@@ -729,7 +733,8 @@ it returns false and raises an appropriate exception.
729733
Here is an example module which uses keywords, based on an example by Geoff
730734
Philbrick ([email protected])::
731735

732-
#include "Python.h"
736+
#define PY_SSIZE_T_CLEAN /* Make "s#" use Py_ssize_t rather than int. */
737+
#include <Python.h>
733738

734739
static PyObject *
735740
keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)
@@ -1228,7 +1233,7 @@ The function :c:func:`spam_system` is modified in a trivial way::
12281233

12291234
In the beginning of the module, right after the line ::
12301235

1231-
#include "Python.h"
1236+
#include <Python.h>
12321237

12331238
two more lines must be added::
12341239

Doc/faq/extending.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ solution then is to call :c:func:`PyParser_ParseString` and test for ``e.error``
280280
equal to ``E_EOF``, which means the input is incomplete. Here's a sample code
281281
fragment, untested, inspired by code from Alex Farber::
282282

283+
#define PY_SSIZE_T_CLEAN
283284
#include <Python.h>
284285
#include <node.h>
285286
#include <errcode.h>
@@ -318,6 +319,7 @@ complete example using the GNU readline library (you may want to ignore
318319
#include <stdio.h>
319320
#include <readline.h>
320321

322+
#define PY_SSIZE_T_CLEAN
321323
#include <Python.h>
322324
#include <object.h>
323325
#include <compile.h>

Doc/includes/custom.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define PY_SSIZE_T_CLEAN
12
#include <Python.h>
23

34
typedef struct {

Doc/includes/custom2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define PY_SSIZE_T_CLEAN
12
#include <Python.h>
23
#include "structmember.h"
34

Doc/includes/custom3.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define PY_SSIZE_T_CLEAN
12
#include <Python.h>
23
#include "structmember.h"
34

Doc/includes/custom4.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define PY_SSIZE_T_CLEAN
12
#include <Python.h>
23
#include "structmember.h"
34

Doc/includes/run-func.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define PY_SSIZE_T_CLEAN
12
#include <Python.h>
23

34
int

Doc/includes/sublist.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define PY_SSIZE_T_CLEAN
12
#include <Python.h>
23

34
typedef struct {

0 commit comments

Comments
 (0)