Skip to content

Commit 3cdfdd9

Browse files
authored
PEP 7: Remove Python 2.2 advice, add C syntax highlighting and green/red sidebars (#3702)
1 parent 11b538f commit 3cdfdd9

File tree

1 file changed

+95
-81
lines changed

1 file changed

+95
-81
lines changed

peps/pep-0007.rst

Lines changed: 95 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
PEP: 7
22
Title: Style Guide for C Code
3-
Version: $Revision$
4-
Last-Modified: $Date$
53
Author: Guido van Rossum <[email protected]>, Barry Warsaw <[email protected]>
64
Status: Active
75
Type: Process
8-
Content-Type: text/x-rst
96
Created: 05-Jul-2001
107
Post-History:
118

9+
.. highlight:: c
1210

1311
Introduction
1412
============
@@ -77,36 +75,51 @@ Code lay-out
7775

7876
* Function definition style: function name in column 1, outermost
7977
curly braces in column 1, blank line after local variable
80-
declarations. ::
78+
declarations.
8179

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
8782
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+
}
9293
9394
* Code structure: one space between keywords like ``if``, ``for`` and
9495
the following left paren; no spaces inside the paren; braces are
9596
required everywhere, even where C permits them to be omitted, but do
9697
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
98114
99-
if (mro != NULL) {
100-
...
101-
}
102-
else {
103-
...
104-
}
115+
return(albatross); /* incorrect */
105116
106-
* The return statement should *not* get redundant parentheses::
117+
Instead:
107118

108-
return albatross; /* correct */
109-
return(albatross); /* incorrect */
119+
.. code-block::
120+
:class: good
121+
122+
return albatross; /* correct */
110123
111124
* Function and macro call style: ``foo(a, b, c)`` -- no space before
112125
the open paren, no spaces inside the parens, no spaces before
@@ -118,39 +131,48 @@ Code lay-out
118131

119132
* Breaking long lines: if you can, break after commas in the outermost
120133
argument list. Always indent continuation lines appropriately,
121-
e.g.::
134+
e.g.:
135+
136+
.. code-block::
137+
:class: good
122138
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);
126142
127143
* When you break a long expression at a binary operator, the
128144
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.:
130146

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+
}
137156
138157
* Vertically align line continuation characters in multi-line macros.
139158

140159
* Macros intended to be used as a statement should use the
141160
``do { ... } while (0)`` macro idiom,
142161
without a final semicolon.
143-
Example::
162+
Example:
163+
164+
.. code-block::
165+
:class: good
144166
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)
151173
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);
154176
155177
* ``#undef`` file local macros after use.
156178

@@ -160,15 +182,18 @@ Code lay-out
160182
* Comments go before the code they describe.
161183

162184
* 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.
164186

165187
* For external functions and variables, we always have a declaration
166188
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:
168190

169-
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
191+
.. code-block::
192+
:class: good
170193
171-
PyAPI_DATA(PyTypeObject) PySuper_Type;
194+
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
195+
196+
PyAPI_DATA(PyTypeObject) PySuper_Type;
172197
173198
174199
Naming conventions
@@ -200,44 +225,44 @@ Documentation Strings
200225
to support building Python without docstrings (``./configure
201226
--without-doc-strings``).
202227

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-
212228
* The first line of each function docstring should be a "signature
213229
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
215234
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.");
219238
220239
Always include a blank line between the signature line and the text
221240
of the description.
222241

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
224243
is no meaningful return value), do not include the indication of the
225244
return type.
226245

227246
* When writing multi-line docstrings, be sure to always use backslash
228247
continuations, as in the example above, or string literal
229-
concatenation::
248+
concatenation:
230249

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
234252
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.");
236256
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.");
241266
242267
not all do; the MSVC compiler is known to complain about this.
243268

@@ -246,14 +271,3 @@ Copyright
246271
=========
247272

248273
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

Comments
 (0)