Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Doc/extending/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ be callable from Python as follows:
>>> import spam
>>> status = spam.system("ls -l")

We also want this function to be well-documented; that is, we want the
function's :func:`help` message to appear as ``system(command)`` plus a short
description.

Begin by creating a file :file:`spammodule.c`. (Historically, if a module is
called ``spam``, the C file containing its implementation is called
:file:`spammodule.c`; if the module name is very long, like ``spammify``, the
Expand Down Expand Up @@ -321,7 +325,7 @@ First, we need to list its name and address in a "method table"::
static PyMethodDef SpamMethods[] = {
...
{"system", spam_system, METH_VARARGS,
"Execute a shell command."},
"system(command)\n--\n\nExecute a shell command."},
...
{NULL, NULL, 0, NULL} /* Sentinel */
};
Expand All @@ -341,6 +345,17 @@ accept a third ``PyObject *`` parameter which will be a dictionary of keywords.
Use :c:func:`PyArg_ParseTupleAndKeywords` to parse the arguments to such a
function.

The fourth entry is the function's docstring. Note the
``"system(command)\n--\n\n"`` at the beginning. Since C extension functions
take a fixed number of arguments---e.g. two for ``METH_VARARGS``---the Python
interpreter has no way of knowing the function's signature until the function
is called. However, by making the docstring start with something of the form
``"myfunc(arg1, arg2='my_default', arg3=17)\n--\n\n"``, the signature will be
constructed and the remainder of the docstring will be used as the function's
``__doc__`` attribute. This can be useful for generating nice messages with the
:func:`help` function or for getting the signature in object form with the
:func:`inspect.signature` function.
Comment on lines +348 to +357
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The fourth entry is the function's docstring. Note the
``"system(command)\n--\n\n"`` at the beginning. Since C extension functions
take a fixed number of arguments---e.g. two for ``METH_VARARGS``---the Python
interpreter has no way of knowing the function's signature until the function
is called. However, by making the docstring start with something of the form
``"myfunc(arg1, arg2='my_default', arg3=17)\n--\n\n"``, the signature will be
constructed and the remainder of the docstring will be used as the function's
``__doc__`` attribute. This can be useful for generating nice messages with the
:func:`help` function or for getting the signature in object form with the
:func:`inspect.signature` function.
The fourth entry is the function's docstring. Note the
``"system(command)\n--\n\n"`` at the beginning. Since C extension functions
take a fixed number of arguments (for instance, two for ``METH_VARARGS``) the Python
interpreter has no way of knowing the function's signature until the function
is called. However, by making the docstring start with something of the form
``"myfunc(arg1, arg2='my_default', arg3=17)\n--\n\n"``, the signature will be
constructed and the remainder of the docstring will be used as the function's
``__doc__`` attribute. This is typically useful for generating nice messages with
:func:`help` or for getting the signature object with :func:`inspect.signature`.


The method table must be referenced in the module definition structure::

static struct PyModuleDef spammodule = {
Expand Down
Loading