This document gives coding conventions for the C code comprising the Cimplementation of Python. Please see the companion informational PEPdescribingstyle guidelines for Python code.
Note, rules are there to be broken. Two good reasons to break aparticular rule:
Follow the following standards.For features that aren’t in the relevant standard, use CPython-specificwrappers (for example:_Py_atomic_store_int32,Py_ALWAYS_INLINE,Py_ARITHMETIC_RIGHT_SHIFT;_Py_ALIGNED_DEF in public headers).When adding such wrappers, try to make them easy to adjust for unsupportedcompilers.
(As a reminder to any users reading this: this PEP is astyle guide; theserules are there to be broken.)
<stdint.h> and<inttypes.h>. Werequire the fixed width integer types.staticinline functions(void) to declarefunctions with no arguments.staticinline functions should be preferred over macros in newcode.staticintextra_ivars(PyTypeObject*type,PyTypeObject*base){intt_size=PyType_BASICSIZE(type);intb_size=PyType_BASICSIZE(base);assert(t_size>=b_size);/* type smaller than base! */...return1;}
if,for andthe following left paren; no spaces inside the paren; braces arerequired everywhere, even where C permits them to be omitted, but donot add them to code you are not otherwise modifying. All new Ccode requires braces. Braces should be formatted as shown:if(mro!=NULL){...}else{...}
return(albatross);/* incorrect */
Instead:
returnalbatross;/* correct */
foo(a,b,c) – no space beforethe open paren, no spaces inside the parens, no spaces beforecommas, 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*)){return0;/* "Forgive" adding a __dict__ only */}
It’s OK to put operators at ends of lines, especially to beconsistent with surrounding code.(SeePEP 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 andPyAPI_DATA() macro, like this:PyAPI_FUNC(PyObject*)PyObject_Repr(PyObject*);PyAPI_DATA(PyTypeObject)PySuper_Type;
Py prefix for public functions; never for staticfunctions. ThePy_ prefix is reserved for global serviceroutines likePy_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() orPyDoc_STRVAR() macro for docstringsto 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 textof the description.
If the return value for the function is alwaysNone (because thereis no meaningful return value), do not include the indication of thereturn 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\nDeterminewhethernameandvaluemakeavalidpair.");
not all do; the MSVC compiler is known to complain about this.
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0007.rst
Last modified:2025-08-27 10:48:57 GMT