1
1
PEP: 7
2
2
Title: Style Guide for C Code
3
- Version: $Revision$
4
- Last-Modified: $Date$
5
3
Author: Guido van Rossum <
[email protected] >, Barry Warsaw <
[email protected] >
6
4
Status: Active
7
5
Type: Process
8
- Content-Type: text/x-rst
9
6
Created: 05-Jul-2001
10
7
Post-History:
11
8
9
+ .. highlight :: c
12
10
13
11
Introduction
14
12
============
@@ -77,36 +75,51 @@ Code lay-out
77
75
78
76
* Function definition style: function name in column 1, outermost
79
77
curly braces in column 1, blank line after local variable
80
- declarations. ::
78
+ declarations.
81
79
82
- static int
83
- extra_ivars(PyTypeObject *type, PyTypeObject *base)
84
- {
85
- int t_size = PyType_BASICSIZE(type);
86
- int b_size = PyType_BASICSIZE(base);
80
+ .. code-block ::
81
+ :class: good
87
82
88
- assert(t_size >= b_size); /* type smaller than base! */
89
- ...
90
- return 1;
91
- }
83
+ static int
84
+ extra_ivars(PyTypeObject *type, PyTypeObject *base)
85
+ {
86
+ int t_size = PyType_BASICSIZE(type);
87
+ int b_size = PyType_BASICSIZE(base);
88
+
89
+ assert(t_size >= b_size); /* type smaller than base! */
90
+ ...
91
+ return 1;
92
+ }
92
93
93
94
* Code structure: one space between keywords like ``if ``, ``for `` and
94
95
the following left paren; no spaces inside the paren; braces are
95
96
required everywhere, even where C permits them to be omitted, but do
96
97
not add them to code you are not otherwise modifying. All new C
97
- code requires braces. Braces should be formatted as shown::
98
+ code requires braces. Braces should be formatted as shown:
99
+
100
+ .. code-block ::
101
+ :class: good
102
+
103
+ if (mro != NULL) {
104
+ ...
105
+ }
106
+ else {
107
+ ...
108
+ }
109
+
110
+ * The return statement should *not * get redundant parentheses:
111
+
112
+ .. code-block ::
113
+ :class: bad
98
114
99
- if (mro != NULL) {
100
- ...
101
- }
102
- else {
103
- ...
104
- }
115
+ return(albatross); /* incorrect */
105
116
106
- * The return statement should * not * get redundant parentheses: :
117
+ Instead :
107
118
108
- return albatross; /* correct */
109
- return(albatross); /* incorrect */
119
+ .. code-block ::
120
+ :class: good
121
+
122
+ return albatross; /* correct */
110
123
111
124
* Function and macro call style: ``foo(a, b, c) `` -- no space before
112
125
the open paren, no spaces inside the parens, no spaces before
@@ -118,39 +131,48 @@ Code lay-out
118
131
119
132
* Breaking long lines: if you can, break after commas in the outermost
120
133
argument list. Always indent continuation lines appropriately,
121
- e.g.::
134
+ e.g.:
135
+
136
+ .. code-block ::
137
+ :class: good
122
138
123
- PyErr_Format(PyExc_TypeError,
124
- "cannot create '%.100s' instances",
125
- type->tp_name);
139
+ PyErr_Format(PyExc_TypeError,
140
+ "cannot create '%.100s' instances",
141
+ type->tp_name);
126
142
127
143
* When you break a long expression at a binary operator, the
128
144
operator goes at the end of the previous line, and braces should be
129
- formatted as shown. E.g.::
145
+ formatted as shown. E.g.:
130
146
131
- if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 &&
132
- type->tp_dictoffset == b_size &&
133
- (size_t)t_size == b_size + sizeof(PyObject *))
134
- {
135
- return 0; /* "Forgive" adding a __dict__ only */
136
- }
147
+ .. code-block ::
148
+ :class: good
149
+
150
+ if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 &&
151
+ type->tp_dictoffset == b_size &&
152
+ (size_t)t_size == b_size + sizeof(PyObject *))
153
+ {
154
+ return 0; /* "Forgive" adding a __dict__ only */
155
+ }
137
156
138
157
* Vertically align line continuation characters in multi-line macros.
139
158
140
159
* Macros intended to be used as a statement should use the
141
160
``do { ... } while (0) `` macro idiom,
142
161
without a final semicolon.
143
- Example::
162
+ Example:
163
+
164
+ .. code-block ::
165
+ :class: good
144
166
145
- #define ADD_INT_MACRO(MOD, INT) \
146
- do { \
147
- if (PyModule_AddIntConstant((MOD), (#INT), (INT)) < 0) { \
148
- goto error; \
149
- } \
150
- } while (0)
167
+ #define ADD_INT_MACRO(MOD, INT) \
168
+ do { \
169
+ if (PyModule_AddIntConstant((MOD), (#INT), (INT)) < 0) { \
170
+ goto error; \
171
+ } \
172
+ } while (0)
151
173
152
- // To be used like a statement with a semicolon:
153
- ADD_INT_MACRO(m, SOME_CONSTANT);
174
+ // To be used like a statement with a semicolon:
175
+ ADD_INT_MACRO(m, SOME_CONSTANT);
154
176
155
177
* ``#undef `` file local macros after use.
156
178
@@ -160,15 +182,18 @@ Code lay-out
160
182
* Comments go before the code they describe.
161
183
162
184
* All functions and global variables should be declared static unless
163
- they are to be part of a published interface
185
+ they are to be part of a published interface.
164
186
165
187
* For external functions and variables, we always have a declaration
166
188
in an appropriate header file in the "Include" directory, which uses
167
- the ``PyAPI_FUNC() `` macro and ``PyAPI_DATA() `` macro, like this::
189
+ the ``PyAPI_FUNC() `` macro and ``PyAPI_DATA() `` macro, like this:
168
190
169
- PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
191
+ .. code-block ::
192
+ :class: good
170
193
171
- PyAPI_DATA(PyTypeObject) PySuper_Type;
194
+ PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
195
+
196
+ PyAPI_DATA(PyTypeObject) PySuper_Type;
172
197
173
198
174
199
Naming conventions
@@ -200,44 +225,44 @@ Documentation Strings
200
225
to support building Python without docstrings (``./configure
201
226
--without-doc-strings ``).
202
227
203
- For C code that needs to support versions of Python older than 2.3,
204
- you can include this after including ``Python.h ``::
205
-
206
- #ifndef PyDoc_STR
207
- #define PyDoc_VAR(name) static char name[]
208
- #define PyDoc_STR(str) (str)
209
- #define PyDoc_STRVAR(name, str) PyDoc_VAR(name) = PyDoc_STR(str)
210
- #endif
211
-
212
228
* The first line of each function docstring should be a "signature
213
229
line" that gives a brief synopsis of the arguments and return value.
214
- For example::
230
+ For example:
231
+
232
+ .. code-block ::
233
+ :class: good
215
234
216
- PyDoc_STRVAR(myfunction__doc__,
217
- "myfunction(name, value) -> bool\n\n\
218
- Determine whether name and value make a valid pair.");
235
+ PyDoc_STRVAR(myfunction__doc__,
236
+ "myfunction(name, value) -> bool\n\n\
237
+ Determine whether name and value make a valid pair.");
219
238
220
239
Always include a blank line between the signature line and the text
221
240
of the description.
222
241
223
- If the return value for the function is always None (because there
242
+ If the return value for the function is always `` None `` (because there
224
243
is no meaningful return value), do not include the indication of the
225
244
return type.
226
245
227
246
* When writing multi-line docstrings, be sure to always use backslash
228
247
continuations, as in the example above, or string literal
229
- concatenation::
248
+ concatenation:
230
249
231
- PyDoc_STRVAR(myfunction__doc__,
232
- "myfunction(name, value) -> bool\n\n"
233
- "Determine whether name and value make a valid pair.");
250
+ .. code-block ::
251
+ :class: good
234
252
235
- Though some C compilers accept string literals without either::
253
+ PyDoc_STRVAR(myfunction__doc__,
254
+ "myfunction(name, value) -> bool\n\n"
255
+ "Determine whether name and value make a valid pair.");
236
256
237
- /* BAD -- don't do this! */
238
- PyDoc_STRVAR(myfunction__doc__,
239
- "myfunction(name, value) -> bool\n\n
240
- Determine whether name and value make a valid pair.");
257
+ Though some C compilers accept string literals without either:
258
+
259
+ .. code-block ::
260
+ :class: bad
261
+
262
+ /* BAD -- don't do this! */
263
+ PyDoc_STRVAR(myfunction__doc__,
264
+ "myfunction(name, value) -> bool\n\n
265
+ Determine whether name and value make a valid pair.");
241
266
242
267
not all do; the MSVC compiler is known to complain about this.
243
268
@@ -246,14 +271,3 @@ Copyright
246
271
=========
247
272
248
273
This document has been placed in the public domain.
249
-
250
-
251
-
252
- ..
253
- Local Variables:
254
- mode: indented-text
255
- indent-tabs-mode: nil
256
- sentence-end-double-space: t
257
- fill-column: 70
258
- coding: utf-8
259
- End:
0 commit comments