@@ -26,11 +26,12 @@ The Basics
2626==========
2727
2828The 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
3536So, if you want to define a new object type, you need to create a new type
3637object.
@@ -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
6364For contrast, let's take a look at the corresponding definition for standard
6465Python floats::
@@ -224,7 +225,7 @@ doesn't do anything. It can't even be subclassed.
224225Adding 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
228229the type usable as a base class. We'll create a new module, :mod: `noddy2 ` that
229230adds these capabilities:
230231
@@ -325,8 +326,8 @@ any arguments passed when the type was called, and that returns the new object
325326created. New methods always accept positional and keyword arguments, but they
326327often ignore the arguments, leaving the argument handling to initializer
327328methods. 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,
331332which 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
475460The 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
477462instance 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
479464a positional argument tuple or keyword argument dictionary. This method is
480465equivalent to the Python method::
481466
@@ -1122,9 +1107,6 @@ needed for methods inherited from a base type. One additional entry is needed
11221107at 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-
11281110The second table is used to define attributes which map directly to data stored
11291111in the instance. A variety of primitive C types are supported, and access may
11301112be 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
11441126convert Python values to and from C values. The :attr: `flags ` field is used to
11451127store flags which control how the attribute can be accessed.
11461128
1147- XXX Need to move some of this to a shared section!
1148-
11491129The following flag constants are defined in :file: `structmember.h `; they may be
11501130combined 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