Skip to content

gh-91118: Fix docstrings that do not honor --without-doc-strings #31769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 18, 2022
Merged
6 changes: 3 additions & 3 deletions Doc/c-api/typeobj.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2597,7 +2597,7 @@ A basic :ref:`static type <static-types>`::
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "mymod.MyObject",
.tp_basicsize = sizeof(MyObject),
.tp_doc = "My objects",
.tp_doc = PyDoc_STR("My objects"),
.tp_new = myobj_new,
.tp_dealloc = (destructor)myobj_dealloc,
.tp_repr = (reprfunc)myobj_repr,
Expand Down Expand Up @@ -2660,7 +2660,7 @@ A type that supports weakrefs, instance dicts, and hashing::
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "mymod.MyObject",
.tp_basicsize = sizeof(MyObject),
.tp_doc = "My objects",
.tp_doc = PyDoc_STR("My objects"),
.tp_weaklistoffset = offsetof(MyObject, weakreflist),
.tp_dictoffset = offsetof(MyObject, inst_dict),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Expand Down Expand Up @@ -2688,7 +2688,7 @@ to create instances (e.g. uses a separate factory func) using
.tp_name = "mymod.MyStr",
.tp_basicsize = sizeof(MyStr),
.tp_base = NULL, // set to &PyUnicode_Type in module init
.tp_doc = "my custom str",
.tp_doc = PyDoc_STR("my custom str"),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_repr = (reprfunc)myobj_repr,
};
Expand Down
4 changes: 2 additions & 2 deletions Doc/extending/newtypes_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The second bit is the definition of the type object. ::
static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "custom.Custom",
.tp_doc = "Custom objects",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
Expand Down Expand Up @@ -161,7 +161,7 @@ you will need to OR the corresponding flags.

We provide a doc string for the type in :c:member:`~PyTypeObject.tp_doc`. ::

.tp_doc = "Custom objects",
.tp_doc = PyDoc_STR("Custom objects"),

To enable object creation, we have to provide a :c:member:`~PyTypeObject.tp_new`
handler. This is the equivalent of the Python method :meth:`__new__`, but
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef struct {
static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "custom.Custom",
.tp_doc = "Custom objects",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/custom2.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static PyMethodDef Custom_methods[] = {
static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "custom2.Custom",
.tp_doc = "Custom objects",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/custom3.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static PyMethodDef Custom_methods[] = {
static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "custom3.Custom",
.tp_doc = "Custom objects",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/custom4.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static PyMethodDef Custom_methods[] = {
static PyTypeObject CustomType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "custom4.Custom",
.tp_doc = "Custom objects",
.tp_doc = PyDoc_STR("Custom objects"),
.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/sublist.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
static PyTypeObject SubListType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "sublist.SubList",
.tp_doc = "SubList objects",
.tp_doc = PyDoc_STR("SubList objects"),
.tp_basicsize = sizeof(SubListObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Classes and functions that unconditionally declared their docstrings
ignoring --without-doc-strings compilation flag no longer do so.

The classes affected are :class:`~ctypes.UnionType`,
:class:`~pickle.PickleBuffer`, and :class:`~types.GenericAlias`.

The functions affected are :func:`ctypes.addressof`,
:func:`ctypes.alignment`, :func:`ctypes.byref`,
:func:`ctypes.CopyComPointer`, :func:`ctypes.FormatError`,
:func:`ctypes.FreeLibrary`, :func:`ctypes.LoadLibrary`, and
:func:`ctypes.sizeof`, along with a bunch of methods in
:class:`~ctypes.PyCPointerType`, :class:`~ctypes.PyCSimpleType`, and
:class:`~ctypes.PyCStructType`.

Patch by Oleg Iarygin.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
All docstrings in code snippets are now wrapped into :func:`PyDoc_STR` to
not teach programmers violation of `PEP 7's Documentation Strings paragraph
<https://www.python.org/dev/peps/pep-0007/#documentation-strings>`_. Patch
by Oleg Iarygin.
28 changes: 14 additions & 14 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ UnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return StructUnionType_new(type, args, kwds, 0);
}

static const char from_address_doc[] =
"C.from_address(integer) -> C instance\naccess a C instance at the specified address";
PyDoc_STRVAR(from_address_doc,
"C.from_address(integer) -> C instance\naccess a C instance at the specified address");

static PyObject *
CDataType_from_address(PyObject *type, PyObject *value)
Expand All @@ -597,8 +597,8 @@ CDataType_from_address(PyObject *type, PyObject *value)
return PyCData_AtAddress(type, buf);
}

static const char from_buffer_doc[] =
"C.from_buffer(object, offset=0) -> C instance\ncreate a C instance from a writeable buffer";
PyDoc_STRVAR(from_buffer_doc,
"C.from_buffer(object, offset=0) -> C instance\ncreate a C instance from a writeable buffer");

static int
KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep);
Expand Down Expand Up @@ -677,8 +677,8 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
return result;
}

static const char from_buffer_copy_doc[] =
"C.from_buffer_copy(object, offset=0) -> C instance\ncreate a C instance from a readable buffer";
PyDoc_STRVAR(from_buffer_copy_doc,
"C.from_buffer_copy(object, offset=0) -> C instance\ncreate a C instance from a readable buffer");

static PyObject *
GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Expand Down Expand Up @@ -728,8 +728,8 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
return result;
}

static const char in_dll_doc[] =
"C.in_dll(dll, name) -> C instance\naccess a C instance in a dll";
PyDoc_STRVAR(in_dll_doc,
"C.in_dll(dll, name) -> C instance\naccess a C instance in a dll");

static PyObject *
CDataType_in_dll(PyObject *type, PyObject *args)
Expand Down Expand Up @@ -790,8 +790,8 @@ CDataType_in_dll(PyObject *type, PyObject *args)
return PyCData_AtAddress(type, address);
}

static const char from_param_doc[] =
"Convert a Python object into a function call parameter.";
PyDoc_STRVAR(from_param_doc,
"Convert a Python object into a function call parameter.");

static PyObject *
CDataType_from_param(PyObject *type, PyObject *value)
Expand Down Expand Up @@ -5485,12 +5485,12 @@ PyTypeObject PyCPointer_Type = {
* Module initialization.
*/

static const char module_docs[] =
"Create and manipulate C compatible data types in Python.";
PyDoc_STRVAR(_ctypes__doc__,
"Create and manipulate C compatible data types in Python.");

#ifdef MS_WIN32

static const char comerror_doc[] = "Raised when a COM method call failed.";
PyDoc_STRVAR(comerror_doc, "Raised when a COM method call failed.");

int
comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
Expand Down Expand Up @@ -5679,7 +5679,7 @@ wstring_at(const wchar_t *ptr, int size)
static struct PyModuleDef _ctypesmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "_ctypes",
.m_doc = module_docs,
.m_doc = _ctypes__doc__,
.m_size = -1,
.m_methods = _ctypes_module_methods,
};
Expand Down
32 changes: 16 additions & 16 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1302,11 +1302,11 @@ _parse_voidp(PyObject *obj, void **address)

#ifdef MS_WIN32

static const char format_error_doc[] =
PyDoc_STRVAR(format_error_doc,
"FormatError([integer]) -> string\n\
\n\
Convert a win32 error code into a string. If the error code is not\n\
given, the return value of a call to GetLastError() is used.\n";
given, the return value of a call to GetLastError() is used.\n");
static PyObject *format_error(PyObject *self, PyObject *args)
{
PyObject *result;
Expand All @@ -1326,13 +1326,13 @@ static PyObject *format_error(PyObject *self, PyObject *args)
return result;
}

static const char load_library_doc[] =
PyDoc_STRVAR(load_library_doc,
"LoadLibrary(name, load_flags) -> handle\n\
\n\
Load an executable (usually a DLL), and return a handle to it.\n\
The handle may be used to locate exported functions in this\n\
module. load_flags are as defined for LoadLibraryEx in the\n\
Windows API.\n";
Windows API.\n");
static PyObject *load_library(PyObject *self, PyObject *args)
{
PyObject *nameobj;
Expand Down Expand Up @@ -1377,10 +1377,10 @@ static PyObject *load_library(PyObject *self, PyObject *args)
#endif
}

static const char free_library_doc[] =
PyDoc_STRVAR(free_library_doc,
"FreeLibrary(handle) -> void\n\
\n\
Free the handle of an executable previously loaded by LoadLibrary.\n";
Free the handle of an executable previously loaded by LoadLibrary.\n");
static PyObject *free_library(PyObject *self, PyObject *args)
{
void *hMod;
Expand All @@ -1400,8 +1400,8 @@ static PyObject *free_library(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}

static const char copy_com_pointer_doc[] =
"CopyComPointer(src, dst) -> HRESULT value\n";
PyDoc_STRVAR(copy_com_pointer_doc,
"CopyComPointer(src, dst) -> HRESULT value\n");

static PyObject *
copy_com_pointer(PyObject *self, PyObject *args)
Expand Down Expand Up @@ -1639,10 +1639,10 @@ call_cdeclfunction(PyObject *self, PyObject *args)
/*****************************************************************
* functions
*/
static const char sizeof_doc[] =
PyDoc_STRVAR(sizeof_doc,
"sizeof(C type) -> integer\n"
"sizeof(C instance) -> integer\n"
"Return the size in bytes of a C instance";
"Return the size in bytes of a C instance");

static PyObject *
sizeof_func(PyObject *self, PyObject *obj)
Expand All @@ -1660,10 +1660,10 @@ sizeof_func(PyObject *self, PyObject *obj)
return NULL;
}

static const char alignment_doc[] =
PyDoc_STRVAR(alignment_doc,
"alignment(C type) -> integer\n"
"alignment(C instance) -> integer\n"
"Return the alignment requirements of a C instance";
"Return the alignment requirements of a C instance");

static PyObject *
align_func(PyObject *self, PyObject *obj)
Expand All @@ -1683,10 +1683,10 @@ align_func(PyObject *self, PyObject *obj)
return NULL;
}

static const char byref_doc[] =
PyDoc_STRVAR(byref_doc,
"byref(C instance[, offset=0]) -> byref-object\n"
"Return a pointer lookalike to a C instance, only usable\n"
"as function argument";
"as function argument");

/*
* We must return something which can be converted to a parameter,
Expand Down Expand Up @@ -1727,9 +1727,9 @@ byref(PyObject *self, PyObject *args)
return (PyObject *)parg;
}

static const char addressof_doc[] =
PyDoc_STRVAR(addressof_doc,
"addressof(C instance) -> integer\n"
"Return the address of the C instance internal buffer";
"Return the address of the C instance internal buffer");

static PyObject *
addressof(PyObject *self, PyObject *obj)
Expand Down
10 changes: 6 additions & 4 deletions Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
return newargs;
}

PyDoc_STRVAR(genericalias__doc__,
"Represent a PEP 585 generic type\n"
"\n"
"E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).");

static PyObject *
ga_getitem(PyObject *self, PyObject *item)
{
Expand Down Expand Up @@ -623,14 +628,11 @@ static PyNumberMethods ga_as_number = {

// TODO:
// - argument clinic?
// - __doc__?
// - cache?
PyTypeObject Py_GenericAliasType = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "types.GenericAlias",
.tp_doc = "Represent a PEP 585 generic type\n"
"\n"
"E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).",
.tp_doc = genericalias__doc__,
.tp_basicsize = sizeof(gaobject),
.tp_dealloc = ga_dealloc,
.tp_repr = ga_repr,
Expand Down
2 changes: 1 addition & 1 deletion Objects/picklebufobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static PyMethodDef picklebuf_methods[] = {
PyTypeObject PyPickleBuffer_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "pickle.PickleBuffer",
.tp_doc = "Wrapper for potentially out-of-band buffers",
.tp_doc = PyDoc_STR("Wrapper for potentially out-of-band buffers"),
.tp_basicsize = sizeof(PyPickleBufferObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_new = picklebuf_new,
Expand Down
4 changes: 2 additions & 2 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ union_getattro(PyObject *self, PyObject *name)
PyTypeObject _PyUnion_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "types.UnionType",
.tp_doc = "Represent a PEP 604 union type\n"
.tp_doc = PyDoc_STR("Represent a PEP 604 union type\n"
"\n"
"E.g. for int | str",
"E.g. for int | str"),
.tp_basicsize = sizeof(unionobject),
.tp_dealloc = unionobject_dealloc,
.tp_alloc = PyType_GenericAlloc,
Expand Down