Академический Документы
Профессиональный Документы
Культура Документы
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 [1]_.
1. When applying the rule would make the code less readable, even for
someone who is used to reading code that follows the rules.
C dialect
=========
* Python versions before 3.6 use ANSI/ISO standard C (the 1989 version
of the standard). This means (amongst many other things) that all
declarations must be at the top of a block (not necessarily at the
top of function).
* Python versions greater than or equal to 3.6 use C89 with several
select C99 features:
Code lay-out
============
static int
extra_ivars(PyTypeObject *type, PyTypeObject *base)
{
int t_size = PyType_BASICSIZE(type);
int b_size = PyType_BASICSIZE(base);
* Code structure: one space between keywords like ``if``, ``for`` and
the following left paren; no spaces inside the paren; braces are
required everywhere, even where C permits them to be omitted, but do
not add them to code you are not otherwise modifying. All new C
code requires braces. Braces should be formatted as shown::
if (mro != NULL) {
...
}
else {
...
}
* Breaking long lines: if you can, break after commas in the outermost
argument list. Always indent continuation lines appropriately,
e.g.::
PyErr_Format(PyExc_TypeError,
"cannot create '%.100s' instances",
type->tp_name);
Naming conventions
==================
* Macros should have a MixedCase prefix and then use upper case, for
example: ``PyString_AS_STRING``, ``Py_PRINT_RAW``.
Documentation Strings
=====================
For C code that needs to support versions of Python older than 2.3,
you can include this after including ``Python.h``::
#ifndef PyDoc_STR
#define PyDoc_VAR(name) static char name[]
#define PyDoc_STR(str) (str)
#define PyDoc_STRVAR(name, str) PyDoc_VAR(name) = PyDoc_STR(str)
#endif
PyDoc_STRVAR(myfunction__doc__,
"myfunction(name, value) -> bool\n\n\
Determine whether name and value make a valid pair.");
Always include a blank line between the signature line and the text
of the description.
If the return value for the function is always None (because there
is no meaningful return value), do not include the indication of the
return type.
PyDoc_STRVAR(myfunction__doc__,
"myfunction(name, value) -> bool\n\n"
"Determine whether name and value make a valid pair.");
not all do; the MSVC compiler is known to complain about this.
References
==========
.. [1] PEP 8, "Style Guide for Python Code", van Rossum, Warsaw
(http://www.python.org/dev/peps/pep-0008)
Copyright
=========
..
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
coding: utf-8
End: