Skip to content

Commit

Permalink
PEP 7: Add C pre-processor macro style recommendations (python#3516)
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland authored Nov 16, 2023
1 parent c0870de commit ca28a17
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions peps/pep-0007.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ Code lay-out
return 0; /* "Forgive" adding a __dict__ only */
}

* Vertically align line continuation characters in multi-line macros.

* Macros intended to be used as a statement should use the
``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.

* Put blank lines around functions, structure definitions, and major
sections inside functions.

Expand Down Expand Up @@ -170,6 +189,9 @@ Naming conventions
* Macros should have a MixedCase prefix and then use upper case, for
example: ``PyString_AS_STRING``, ``Py_PRINT_RAW``.

* Macro parameters should use ``ALL_CAPS`` style,
so they are easily distinguishable from C variables and struct members.


Documentation Strings
=====================
Expand Down

0 comments on commit ca28a17

Please sign in to comment.