All function, type and macro definitions needed to use the Python/C API are
included in your code by the following line::
- #include "Python.h"
+ #define PY_SSIZE_T_CLEAN
+ #include <Python.h>
This implies inclusion of the following standard headers: ``<stdio.h>``,
``<string.h>``, ``<errno.h>``, ``<limits.h>``, ``<assert.h>`` and ``<stdlib.h>``
headers on some systems, you *must* include :file:`Python.h` before any standard
headers are included.
+ It is recommended to always define ``PY_SSIZE_T_CLEAN`` before including
+ ``Python.h``. See :ref:`arg-parsing` for a description of this macro.
+
All user visible names defined by Python.h (except those defined by the included
standard headers) have one of the prefixes ``Py`` or ``_Py``. Names beginning
with ``_Py`` are for internal use by the Python implementation and should not be
to interact with the application directly. This can for example be used to
perform some operation on a file. ::
+ #define PY_SSIZE_T_CLEAN
#include <Python.h>
int
:file:`spammodule.c`; if the module name is very long, like ``spammify``, the
module name can be just :file:`spammify.c`.)
-The first line of our file can be::
+The first two lines of our file can be::
+ #define PY_SSIZE_T_CLEAN
#include <Python.h>
which pulls in the Python API (you can add a comment describing the purpose of
headers on some systems, you *must* include :file:`Python.h` before any standard
headers are included.
+ It is recommended to always define ``PY_SSIZE_T_CLEAN`` before including
+ ``Python.h``. See :ref:`parsetuple` for a description of this macro.
+
All user-visible symbols defined by :file:`Python.h` have a prefix of ``Py`` or
``PY``, except those defined in standard header files. For convenience, and
since they are used extensively by the Python interpreter, ``"Python.h"``
Here is an example module which uses keywords, based on an example by Geoff
Philbrick (philbrick@hks.com)::
- #include "Python.h"
+ #define PY_SSIZE_T_CLEAN /* Make "s#" use Py_ssize_t rather than int. */
+ #include <Python.h>
static PyObject *
keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)
In the beginning of the module, right after the line ::
- #include "Python.h"
+ #include <Python.h>
two more lines must be added::
equal to ``E_EOF``, which means the input is incomplete. Here's a sample code
fragment, untested, inspired by code from Alex Farber::
+ #define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <node.h>
#include <errcode.h>
#include <stdio.h>
#include <readline.h>
+ #define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <object.h>
#include <compile.h>
+#define PY_SSIZE_T_CLEAN
#include <Python.h>
typedef struct {
+#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
+#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
+#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
+#define PY_SSIZE_T_CLEAN
#include <Python.h>
int
+#define PY_SSIZE_T_CLEAN
#include <Python.h>
typedef struct {