Skip to content

Commit 9ded6f0

Browse files
picnixzskirpichev
andauthored
gh-111178: fix incorrect function signatures in docs (#132395)
Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent 3e1a47b commit 9ded6f0

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
lines changed

Doc/c-api/typeobj.rst

+14-7
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,13 @@ and :c:data:`PyType_Type` effectively act as defaults.)
703703

704704
.. code-block:: c
705705
706-
static void foo_dealloc(foo_object *self) {
706+
static void
707+
foo_dealloc(PyObject *op)
708+
{
709+
foo_object *self = (foo_object *) op;
707710
PyObject_GC_UnTrack(self);
708711
Py_CLEAR(self->ref);
709-
Py_TYPE(self)->tp_free((PyObject *)self);
712+
Py_TYPE(self)->tp_free(self);
710713
}
711714
712715
Finally, if the type is heap allocated (:c:macro:`Py_TPFLAGS_HEAPTYPE`), the
@@ -717,10 +720,12 @@ and :c:data:`PyType_Type` effectively act as defaults.)
717720

718721
.. code-block:: c
719722
720-
static void foo_dealloc(foo_object *self) {
721-
PyTypeObject *tp = Py_TYPE(self);
723+
static void
724+
foo_dealloc(PyObject *op)
725+
{
726+
PyTypeObject *tp = Py_TYPE(op);
722727
// free references and buffers here
723-
tp->tp_free(self);
728+
tp->tp_free(op);
724729
Py_DECREF(tp);
725730
}
726731
@@ -1416,8 +1421,9 @@ and :c:data:`PyType_Type` effectively act as defaults.)
14161421
:mod:`!_thread` extension module::
14171422

14181423
static int
1419-
local_traverse(localobject *self, visitproc visit, void *arg)
1424+
local_traverse(PyObject *op, visitproc visit, void *arg)
14201425
{
1426+
localobject *self = (localobject *) op;
14211427
Py_VISIT(self->args);
14221428
Py_VISIT(self->kw);
14231429
Py_VISIT(self->dict);
@@ -1511,8 +1517,9 @@ and :c:data:`PyType_Type` effectively act as defaults.)
15111517
members to ``NULL``, as in the following example::
15121518

15131519
static int
1514-
local_clear(localobject *self)
1520+
local_clear(PyObject *op)
15151521
{
1522+
localobject *self = (localobject *) op;
15161523
Py_CLEAR(self->key);
15171524
Py_CLEAR(self->args);
15181525
Py_CLEAR(self->kw);

Doc/extending/newtypes.rst

+43-33
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,24 @@ object itself needs to be freed here as well. Here is an example of this
7070
function::
7171

7272
static void
73-
newdatatype_dealloc(newdatatypeobject *obj)
73+
newdatatype_dealloc(PyObject *op)
7474
{
75-
free(obj->obj_UnderlyingDatatypePtr);
76-
Py_TYPE(obj)->tp_free((PyObject *)obj);
75+
newdatatypeobject *self = (newdatatypeobject *) op;
76+
free(self->obj_UnderlyingDatatypePtr);
77+
Py_TYPE(self)->tp_free(self);
7778
}
7879

7980
If your type supports garbage collection, the destructor should call
8081
:c:func:`PyObject_GC_UnTrack` before clearing any member fields::
8182

8283
static void
83-
newdatatype_dealloc(newdatatypeobject *obj)
84+
newdatatype_dealloc(PyObject *op)
8485
{
85-
PyObject_GC_UnTrack(obj);
86-
Py_CLEAR(obj->other_obj);
86+
newdatatypeobject *self = (newdatatypeobject *) op;
87+
PyObject_GC_UnTrack(op);
88+
Py_CLEAR(self->other_obj);
8789
...
88-
Py_TYPE(obj)->tp_free((PyObject *)obj);
90+
Py_TYPE(self)->tp_free(self);
8991
}
9092

9193
.. index::
@@ -117,17 +119,19 @@ done. This can be done using the :c:func:`PyErr_Fetch` and
117119
PyErr_Fetch(&err_type, &err_value, &err_traceback);
118120

119121
cbresult = PyObject_CallNoArgs(self->my_callback);
120-
if (cbresult == NULL)
121-
PyErr_WriteUnraisable(self->my_callback);
122-
else
122+
if (cbresult == NULL) {
123+
PyErr_WriteUnraisable(self->my_callback);
124+
}
125+
else {
123126
Py_DECREF(cbresult);
127+
}
124128

125129
/* This restores the saved exception state */
126130
PyErr_Restore(err_type, err_value, err_traceback);
127131

128132
Py_DECREF(self->my_callback);
129133
}
130-
Py_TYPE(obj)->tp_free((PyObject*)self);
134+
Py_TYPE(self)->tp_free(self);
131135
}
132136

133137
.. note::
@@ -168,10 +172,11 @@ representation of the instance for which it is called. Here is a simple
168172
example::
169173

170174
static PyObject *
171-
newdatatype_repr(newdatatypeobject *obj)
175+
newdatatype_repr(PyObject *op)
172176
{
177+
newdatatypeobject *self = (newdatatypeobject *) op;
173178
return PyUnicode_FromFormat("Repr-ified_newdatatype{{size:%d}}",
174-
obj->obj_UnderlyingDatatypePtr->size);
179+
self->obj_UnderlyingDatatypePtr->size);
175180
}
176181

177182
If no :c:member:`~PyTypeObject.tp_repr` handler is specified, the interpreter will supply a
@@ -188,10 +193,11 @@ used instead.
188193
Here is a simple example::
189194

190195
static PyObject *
191-
newdatatype_str(newdatatypeobject *obj)
196+
newdatatype_str(PyObject *op)
192197
{
198+
newdatatypeobject *self = (newdatatypeobject *) op;
193199
return PyUnicode_FromFormat("Stringified_newdatatype{{size:%d}}",
194-
obj->obj_UnderlyingDatatypePtr->size);
200+
self->obj_UnderlyingDatatypePtr->size);
195201
}
196202

197203

@@ -329,16 +335,16 @@ method of a class would be called.
329335
Here is an example::
330336

331337
static PyObject *
332-
newdatatype_getattr(newdatatypeobject *obj, char *name)
338+
newdatatype_getattr(PyObject *op, char *name)
333339
{
334-
if (strcmp(name, "data") == 0)
335-
{
336-
return PyLong_FromLong(obj->data);
340+
newdatatypeobject *self = (newdatatypeobject *) op;
341+
if (strcmp(name, "data") == 0) {
342+
return PyLong_FromLong(self->data);
337343
}
338344

339345
PyErr_Format(PyExc_AttributeError,
340346
"'%.100s' object has no attribute '%.400s'",
341-
Py_TYPE(obj)->tp_name, name);
347+
Py_TYPE(self)->tp_name, name);
342348
return NULL;
343349
}
344350

@@ -349,7 +355,7 @@ example that simply raises an exception; if this were really all you wanted, the
349355
:c:member:`~PyTypeObject.tp_setattr` handler should be set to ``NULL``. ::
350356

351357
static int
352-
newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v)
358+
newdatatype_setattr(PyObject *op, char *name, PyObject *v)
353359
{
354360
PyErr_Format(PyExc_RuntimeError, "Read-only attribute: %s", name);
355361
return -1;
@@ -379,8 +385,10 @@ Here is a sample implementation, for a datatype that is considered equal if the
379385
size of an internal pointer is equal::
380386

381387
static PyObject *
382-
newdatatype_richcmp(newdatatypeobject *obj1, newdatatypeobject *obj2, int op)
388+
newdatatype_richcmp(PyObject *lhs, PyObject *rhs, int op)
383389
{
390+
newdatatypeobject *obj1 = (newdatatypeobject *) lhs;
391+
newdatatypeobject *obj2 = (newdatatypeobject *) rhs;
384392
PyObject *result;
385393
int c, size1, size2;
386394

@@ -399,8 +407,7 @@ size of an internal pointer is equal::
399407
case Py_GE: c = size1 >= size2; break;
400408
}
401409
result = c ? Py_True : Py_False;
402-
Py_INCREF(result);
403-
return result;
410+
return Py_NewRef(result);
404411
}
405412

406413

@@ -439,12 +446,14 @@ This function, if you choose to provide it, should return a hash number for an
439446
instance of your data type. Here is a simple example::
440447

441448
static Py_hash_t
442-
newdatatype_hash(newdatatypeobject *obj)
449+
newdatatype_hash(PyObject *op)
443450
{
451+
newdatatypeobject *self = (newdatatypeobject *) op;
444452
Py_hash_t result;
445-
result = obj->some_size + 32767 * obj->some_number;
446-
if (result == -1)
447-
result = -2;
453+
result = self->some_size + 32767 * self->some_number;
454+
if (result == -1) {
455+
result = -2;
456+
}
448457
return result;
449458
}
450459

@@ -478,8 +487,9 @@ This function takes three arguments:
478487
Here is a toy ``tp_call`` implementation::
479488

480489
static PyObject *
481-
newdatatype_call(newdatatypeobject *obj, PyObject *args, PyObject *kwds)
490+
newdatatype_call(PyObject *op, PyObject *args, PyObject *kwds)
482491
{
492+
newdatatypeobject *self = (newdatatypeobject *) op;
483493
PyObject *result;
484494
const char *arg1;
485495
const char *arg2;
@@ -490,7 +500,7 @@ Here is a toy ``tp_call`` implementation::
490500
}
491501
result = PyUnicode_FromFormat(
492502
"Returning -- value: [%d] arg1: [%s] arg2: [%s] arg3: [%s]\n",
493-
obj->obj_UnderlyingDatatypePtr->size,
503+
self->obj_UnderlyingDatatypePtr->size,
494504
arg1, arg2, arg3);
495505
return result;
496506
}
@@ -563,12 +573,12 @@ The only further addition is that ``tp_dealloc`` needs to clear any weak
563573
references (by calling :c:func:`PyObject_ClearWeakRefs`)::
564574

565575
static void
566-
Trivial_dealloc(TrivialObject *self)
576+
Trivial_dealloc(PyObject *op)
567577
{
568578
/* Clear weakrefs first before calling any destructors */
569-
PyObject_ClearWeakRefs((PyObject *) self);
579+
PyObject_ClearWeakRefs(op);
570580
/* ... remainder of destruction code omitted for brevity ... */
571-
Py_TYPE(self)->tp_free((PyObject *) self);
581+
Py_TYPE(op)->tp_free(op);
572582
}
573583

574584

Doc/whatsnew/3.9.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,8 @@ Changes in the C API
11611161
.. code-block:: c
11621162
11631163
int
1164-
foo_traverse(foo_struct *self, visitproc visit, void *arg) {
1164+
foo_traverse(PyObject *self, visitproc visit, void *arg)
1165+
{
11651166
// Rest of the traverse function
11661167
#if PY_VERSION_HEX >= 0x03090000
11671168
// This was not needed before Python 3.9 (Python issue 35810 and 40217)

0 commit comments

Comments
 (0)