Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49ac6f4

Browse files
committed
Some corrections for the Doc/extending documentation. Closes #14129
1 parent e13b7fe commit 49ac6f4

3 files changed

Lines changed: 23 additions & 45 deletions

File tree

Doc/c-api/type.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ Type Objects
7575
7676
.. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
7777
78-
XXX: Document.
78+
Generic handler for the :attr:`tp_new` slot of a type object. Initialize
79+
all instance variables to *NULL*.
7980
8081
.. c:function:: int PyType_Ready(PyTypeObject *type)
8182

Doc/extending/extending.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,6 @@ optionally followed by an import of the module::
384384
imports it. */
385385
PyImport_ImportModule("spam");
386386

387-
An example may be found in the file :file:`Demo/embed/demo.c` in the Python
388-
source distribution.
389-
390387
.. note::
391388

392389
Removing entries from ``sys.modules`` or importing compiled modules into

Doc/extending/newtypes.rst

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ The Basics
2626
==========
2727

2828
The Python runtime sees all Python objects as variables of type
29-
:c:type:`PyObject\*`. A :c:type:`PyObject` is not a very magnificent object - it
30-
just contains the refcount and a pointer to the object's "type object". This is
31-
where the action is; the type object determines which (C) functions get called
32-
when, for instance, an attribute gets looked up on an object or it is multiplied
33-
by another object. These C functions are called "type methods".
29+
:c:type:`PyObject\*`, which serves as a "base type" for all Python objects.
30+
:c:type:`PyObject` itself only contains the refcount and a pointer to the
31+
object's "type object". This is where the action is; the type object determines
32+
which (C) functions get called when, for instance, an attribute gets looked
33+
up on an object or it is multiplied by another object. These C functions
34+
are called "type methods".
3435

3536
So, if you want to define a new object type, you need to create a new type
3637
object.
@@ -50,15 +51,15 @@ The first bit that will be new is::
5051
PyObject_HEAD
5152
} noddy_NoddyObject;
5253

53-
This is what a Noddy object will contain---in this case, nothing more than every
54-
Python object contains, namely a refcount and a pointer to a type object. These
55-
are the fields the ``PyObject_HEAD`` macro brings in. The reason for the macro
56-
is to standardize the layout and to enable special debugging fields in debug
57-
builds. Note that there is no semicolon after the ``PyObject_HEAD`` macro; one
58-
is included in the macro definition. Be wary of adding one by accident; it's
59-
easy to do from habit, and your compiler might not complain, but someone else's
60-
probably will! (On Windows, MSVC is known to call this an error and refuse to
61-
compile the code.)
54+
This is what a Noddy object will contain---in this case, nothing more than what
55+
every Python object contains---a refcount and a pointer to a type object.
56+
These are the fields the ``PyObject_HEAD`` macro brings in. The reason for the
57+
macro is to standardize the layout and to enable special debugging fields in
58+
debug builds. Note that there is no semicolon after the ``PyObject_HEAD``
59+
macro; one is included in the macro definition. Be wary of adding one by
60+
accident; it's easy to do from habit, and your compiler might not complain,
61+
but someone else's probably will! (On Windows, MSVC is known to call this an
62+
error and refuse to compile the code.)
6263

6364
For contrast, let's take a look at the corresponding definition for standard
6465
Python floats::
@@ -224,7 +225,7 @@ doesn't do anything. It can't even be subclassed.
224225
Adding data and methods to the Basic example
225226
--------------------------------------------
226227

227-
Let's expend the basic example to add some data and methods. Let's also make
228+
Let's extend the basic example to add some data and methods. Let's also make
228229
the type usable as a base class. We'll create a new module, :mod:`noddy2` that
229230
adds these capabilities:
230231

@@ -325,8 +326,8 @@ any arguments passed when the type was called, and that returns the new object
325326
created. New methods always accept positional and keyword arguments, but they
326327
often ignore the arguments, leaving the argument handling to initializer
327328
methods. Note that if the type supports subclassing, the type passed may not be
328-
the type being defined. The new method calls the tp_alloc slot to allocate
329-
memory. We don't fill the :attr:`tp_alloc` slot ourselves. Rather
329+
the type being defined. The new method calls the :attr:`tp_alloc` slot to
330+
allocate memory. We don't fill the :attr:`tp_alloc` slot ourselves. Rather
330331
:c:func:`PyType_Ready` fills it for us by inheriting it from our base class,
331332
which is :class:`object` by default. Most types use the default allocation.
332333

@@ -443,15 +444,6 @@ concatenation of the first and last names. ::
443444
static PyObject *
444445
Noddy_name(Noddy* self)
445446
{
446-
static PyObject *format = NULL;
447-
PyObject *args, *result;
448-
449-
if (format == NULL) {
450-
format = PyString_FromString("%s %s");
451-
if (format == NULL)
452-
return NULL;
453-
}
454-
455447
if (self->first == NULL) {
456448
PyErr_SetString(PyExc_AttributeError, "first");
457449
return NULL;
@@ -462,20 +454,13 @@ concatenation of the first and last names. ::
462454
return NULL;
463455
}
464456

465-
args = Py_BuildValue("OO", self->first, self->last);
466-
if (args == NULL)
467-
return NULL;
468-
469-
result = PyString_Format(format, args);
470-
Py_DECREF(args);
471-
472-
return result;
457+
return PyUnicode_FromFormat("%S %S", self->first, self->last);
473458
}
474459

475460
The method is implemented as a C function that takes a :class:`Noddy` (or
476461
:class:`Noddy` subclass) instance as the first argument. Methods always take an
477462
instance as the first argument. Methods often take positional and keyword
478-
arguments as well, but in this cased we don't take any and don't need to accept
463+
arguments as well, but in this case we don't take any and don't need to accept
479464
a positional argument tuple or keyword argument dictionary. This method is
480465
equivalent to the Python method::
481466

@@ -1122,9 +1107,6 @@ needed for methods inherited from a base type. One additional entry is needed
11221107
at the end; it is a sentinel that marks the end of the array. The
11231108
:attr:`ml_name` field of the sentinel must be *NULL*.
11241109

1125-
XXX Need to refer to some unified discussion of the structure fields, shared
1126-
with the next section.
1127-
11281110
The second table is used to define attributes which map directly to data stored
11291111
in the instance. A variety of primitive C types are supported, and access may
11301112
be read-only or read-write. The structures in the table are defined as::
@@ -1144,8 +1126,6 @@ type which will be able to extract a value from the instance structure. The
11441126
convert Python values to and from C values. The :attr:`flags` field is used to
11451127
store flags which control how the attribute can be accessed.
11461128

1147-
XXX Need to move some of this to a shared section!
1148-
11491129
The following flag constants are defined in :file:`structmember.h`; they may be
11501130
combined using bitwise-OR.
11511131

@@ -1370,7 +1350,7 @@ Here is a desultory example of the implementation of the call function. ::
13701350
return result;
13711351
}
13721352

1373-
XXX some fields need to be added here... ::
1353+
::
13741354

13751355
/* Iterators */
13761356
getiterfunc tp_iter;

0 commit comments

Comments
 (0)