This document gives coding conventions for the C code comprising the C implementation of Python. Please see the companion informational PEP describing style guidelines for Python code.
Note, rules are there to be broken. Two good reasons to break a particular rule:
<stdint.h>
and <inttypes.h>
. We require the fixed width integer types.static inline
functionsstatic inline
functions should be preferred over macros in new code.static int extra_ivars(PyTypeObject *type, PyTypeObject *base) { int t_size = PyType_BASICSIZE(type); int b_size = PyType_BASICSIZE(base); assert(t_size >= b_size); /* type smaller than base! */ ... return 1; }
if
, for
and the following left paren; no spaces inside the paren; braces are required everywhere, even where C permits them to be omitted, but do not add them to code you are not otherwise modifying. All new C code requires braces. Braces should be formatted as shown:
if (mro != NULL) { ... } else { ... }
return(albatross); /* incorrect */
Instead:
return albatross; /* correct */
foo(a, b, c)
– no space before the open paren, no spaces inside the parens, no spaces before commas, one space after each comma.PyErr_Format(PyExc_TypeError, "cannot create '%.100s' instances", type->tp_name);
if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 && type->tp_dictoffset == b_size && (size_t)t_size == b_size + sizeof(PyObject *)) { return 0; /* "Forgive" adding a __dict__ only */ }
It’s OK to put operators at ends of lines, especially to be consistent with surrounding code. (See PEP 8 for a longer discussion.)
do { ... } while (0)
macro idiom, without a final semicolon. Example:
#define ADD_INT_MACRO(MOD, INT) \ do { \ if (PyModule_AddIntConstant((MOD), (#INT), (INT)) < 0) { \ goto error; \ } \ } while (0) // To be used like a statement with a semicolon: ADD_INT_MACRO(m, SOME_CONSTANT);
#undef
file local macros after use.PyAPI_FUNC()
macro and PyAPI_DATA()
macro, like this:
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); PyAPI_DATA(PyTypeObject) PySuper_Type;
Py
prefix for public functions; never for static functions. The Py_
prefix is reserved for global service routines like Py_FatalError
; specific groups of routines (e.g. specific object type APIs) use a longer prefix, e.g. PyString_
for string functions.PyObject_GetAttr
, Py_BuildValue
, PyExc_TypeError
._Py
prefix for this, e.g.: _PyObject_Dump
.PyString_AS_STRING
, Py_PRINT_RAW
.ALL_CAPS
style, so they are easily distinguishable from C variables and struct members.PyDoc_STR()
or PyDoc_STRVAR()
macro for docstrings to support building Python without docstrings (./configure --without-doc-strings
).PyDoc_STRVAR(myfunction__doc__, "myfunction(name, value) -> bool\n\n\ Determine whether name and value make a valid pair.");
Always include a blank line between the signature line and the text of the description.
If the return value for the function is always None
(because there is no meaningful return value), do not include the indication of the return type.
PyDoc_STRVAR(myfunction__doc__, "myfunction(name, value) -> bool\n\n" "Determine whether name and value make a valid pair.");
Though some C compilers accept string literals without either:
/* BAD -- don't do this! */ PyDoc_STRVAR(myfunction__doc__, "myfunction(name, value) -> bool\n\n Determine whether name and value make a valid pair.");
not all do; the MSVC compiler is known to complain about this.
This document has been placed in the public domain.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4