PEP 7 – Style Guide for C Code | peps.python.org (original) (raw)

Author:

Guido van Rossum , Barry Warsaw

Status:

Active

Type:

Process

Created:

05-Jul-2001

Post-History:


Table of Contents

Introduction

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:

  1. When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules.
  2. To be consistent with surrounding code that also breaks it (maybe for historic reasons) – although this is also an opportunity to clean up someone else’s mess (in true XP style).

C standards

Follow the following standards. For features that aren’t in the relevant standard, use CPython-specific wrappers (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 unsupported compilers.

Common C code conventions

Code lay-out

#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);

Naming conventions

Documentation Strings

This document has been placed in the public domain.