From ca28a17523239255128ccdc20a22e158b512b116 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 16 Nov 2023 10:37:18 +0100 Subject: [PATCH] PEP 7: Add C pre-processor macro style recommendations (#3516) --- peps/pep-0007.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/peps/pep-0007.rst b/peps/pep-0007.rst index d88373f29..fc86f0450 100644 --- a/peps/pep-0007.rst +++ b/peps/pep-0007.rst @@ -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. @@ -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 =====================