1.. highlight:: c 2 3.. _defining-new-types: 4 5********************************** 6Defining Extension Types: Tutorial 7********************************** 8 9.. sectionauthor:: Michael Hudson <mwh@python.net> 10.. sectionauthor:: Dave Kuhlman <dkuhlman@rexx.com> 11.. sectionauthor:: Jim Fulton <jim@zope.com> 12 13 14Python allows the writer of a C extension module to define new types that 15can be manipulated from Python code, much like the built-in :class:`str` 16and :class:`list` types. The code for all extension types follows a 17pattern, but there are some details that you need to understand before you 18can get started. This document is a gentle introduction to the topic. 19 20 21.. _dnt-basics: 22 23The Basics 24========== 25 26The :term:`CPython` runtime sees all Python objects as variables of type 27:c:expr:`PyObject*`, which serves as a "base type" for all Python objects. 28The :c:type:`PyObject` structure itself only contains the object's 29:term:`reference count` and a pointer to the object's "type object". 30This is where the action is; the type object determines which (C) functions 31get called by the interpreter when, for instance, an attribute gets looked up 32on an object, a method called, or it is multiplied by another object. These 33C functions are called "type methods". 34 35So, if you want to define a new extension type, you need to create a new type 36object. 37 38This sort of thing can only be explained by example, so here's a minimal, but 39complete, module that defines a new type named :class:`!Custom` inside a C 40extension module :mod:`!custom`: 41 42.. note:: 43 What we're showing here is the traditional way of defining *static* 44 extension types. It should be adequate for most uses. The C API also 45 allows defining heap-allocated extension types using the 46 :c:func:`PyType_FromSpec` function, which isn't covered in this tutorial. 47 48.. literalinclude:: ../includes/newtypes/custom.c 49 50Now that's quite a bit to take in at once, but hopefully bits will seem familiar 51from the previous chapter. This file defines three things: 52 53#. What a :class:`!Custom` **object** contains: this is the ``CustomObject`` 54 struct, which is allocated once for each :class:`!Custom` instance. 55#. How the :class:`!Custom` **type** behaves: this is the ``CustomType`` struct, 56 which defines a set of flags and function pointers that the interpreter 57 inspects when specific operations are requested. 58#. How to initialize the :mod:`!custom` module: this is the ``PyInit_custom`` 59 function and the associated ``custommodule`` struct. 60 61The first bit is:: 62 63 typedef struct { 64 PyObject_HEAD 65 } CustomObject; 66 67This is what a Custom object will contain. ``PyObject_HEAD`` is mandatory 68at the start of each object struct and defines a field called ``ob_base`` 69of type :c:type:`PyObject`, containing a pointer to a type object and a 70reference count (these can be accessed using the macros :c:macro:`Py_TYPE` 71and :c:macro:`Py_REFCNT` respectively). The reason for the macro is to 72abstract away the layout and to enable additional fields in :ref:`debug builds 73<debug-build>`. 74 75.. note:: 76 There is no semicolon above after the :c:macro:`PyObject_HEAD` macro. 77 Be wary of adding one by accident: some compilers will complain. 78 79Of course, objects generally store additional data besides the standard 80``PyObject_HEAD`` boilerplate; for example, here is the definition for 81standard Python floats:: 82 83 typedef struct { 84 PyObject_HEAD 85 double ob_fval; 86 } PyFloatObject; 87 88The second bit is the definition of the type object. :: 89 90 static PyTypeObject CustomType = { 91 .ob_base = PyVarObject_HEAD_INIT(NULL, 0) 92 .tp_name = "custom.Custom", 93 .tp_doc = PyDoc_STR("Custom objects"), 94 .tp_basicsize = sizeof(CustomObject), 95 .tp_itemsize = 0, 96 .tp_flags = Py_TPFLAGS_DEFAULT, 97 .tp_new = PyType_GenericNew, 98 }; 99 100.. note:: 101 We recommend using C99-style designated initializers as above, to 102 avoid listing all the :c:type:`PyTypeObject` fields that you don't care 103 about and also to avoid caring about the fields' declaration order. 104 105The actual definition of :c:type:`PyTypeObject` in :file:`object.h` has 106many more :ref:`fields <type-structs>` than the definition above. The 107remaining fields will be filled with zeros by the C compiler, and it's 108common practice to not specify them explicitly unless you need them. 109 110We're going to pick it apart, one field at a time:: 111 112 .ob_base = PyVarObject_HEAD_INIT(NULL, 0) 113 114This line is mandatory boilerplate to initialize the ``ob_base`` 115field mentioned above. :: 116 117 .tp_name = "custom.Custom", 118 119The name of our type. This will appear in the default textual representation of 120our objects and in some error messages, for example: 121 122.. code-block:: pycon 123 124 >>> "" + custom.Custom() 125 Traceback (most recent call last): 126 File "<stdin>", line 1, in <module> 127 TypeError: can only concatenate str (not "custom.Custom") to str 128 129Note that the name is a dotted name that includes both the module name and the 130name of the type within the module. The module in this case is :mod:`!custom` and 131the type is :class:`!Custom`, so we set the type name to :class:`!custom.Custom`. 132Using the real dotted import path is important to make your type compatible 133with the :mod:`pydoc` and :mod:`pickle` modules. :: 134 135 .tp_basicsize = sizeof(CustomObject), 136 .tp_itemsize = 0, 137 138This is so that Python knows how much memory to allocate when creating 139new :class:`!Custom` instances. :c:member:`~PyTypeObject.tp_itemsize` is 140only used for variable-sized objects and should otherwise be zero. 141 142.. note:: 143 144 If you want your type to be subclassable from Python, and your type has the same 145 :c:member:`~PyTypeObject.tp_basicsize` as its base type, you may have problems with multiple 146 inheritance. A Python subclass of your type will have to list your type first 147 in its :attr:`~type.__bases__`, or else it will not be able to call your type's 148 :meth:`~object.__new__` method without getting an error. You can avoid this problem by 149 ensuring that your type has a larger value for :c:member:`~PyTypeObject.tp_basicsize` than its 150 base type does. Most of the time, this will be true anyway, because either your 151 base type will be :class:`object`, or else you will be adding data members to 152 your base type, and therefore increasing its size. 153 154We set the class flags to :c:macro:`Py_TPFLAGS_DEFAULT`. :: 155 156 .tp_flags = Py_TPFLAGS_DEFAULT, 157 158All types should include this constant in their flags. It enables all of the 159members defined until at least Python 3.3. If you need further members, 160you will need to OR the corresponding flags. 161 162We provide a doc string for the type in :c:member:`~PyTypeObject.tp_doc`. :: 163 164 .tp_doc = PyDoc_STR("Custom objects"), 165 166To enable object creation, we have to provide a :c:member:`~PyTypeObject.tp_new` 167handler. This is the equivalent of the Python method :meth:`~object.__new__`, but 168has to be specified explicitly. In this case, we can just use the default 169implementation provided by the API function :c:func:`PyType_GenericNew`. :: 170 171 .tp_new = PyType_GenericNew, 172 173Everything else in the file should be familiar, except for some code in 174:c:func:`!PyInit_custom`:: 175 176 if (PyType_Ready(&CustomType) < 0) 177 return; 178 179This initializes the :class:`!Custom` type, filling in a number of members 180to the appropriate default values, including :c:member:`~PyObject.ob_type` that we initially 181set to ``NULL``. :: 182 183 if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) { 184 Py_DECREF(m); 185 return NULL; 186 } 187 188This adds the type to the module dictionary. This allows us to create 189:class:`!Custom` instances by calling the :class:`!Custom` class: 190 191.. code-block:: pycon 192 193 >>> import custom 194 >>> mycustom = custom.Custom() 195 196That's it! All that remains is to build it; put the above code in a file called 197:file:`custom.c`, 198 199.. literalinclude:: ../includes/newtypes/pyproject.toml 200 201in a file called :file:`pyproject.toml`, and 202 203.. code-block:: python 204 205 from setuptools import Extension, setup 206 setup(ext_modules=[Extension("custom", ["custom.c"])]) 207 208in a file called :file:`setup.py`; then typing 209 210.. code-block:: shell-session 211 212 $ python -m pip install . 213 214in a shell should produce a file :file:`custom.so` in a subdirectory 215and install it; now fire up Python --- you should be able to ``import custom`` 216and play around with ``Custom`` objects. 217 218That wasn't so hard, was it? 219 220Of course, the current Custom type is pretty uninteresting. It has no data and 221doesn't do anything. It can't even be subclassed. 222 223 224Adding data and methods to the Basic example 225============================================ 226 227Let's extend the basic example to add some data and methods. Let's also make 228the type usable as a base class. We'll create a new module, :mod:`!custom2` that 229adds these capabilities: 230 231.. literalinclude:: ../includes/newtypes/custom2.c 232 233 234This version of the module has a number of changes. 235 236The :class:`!Custom` type now has three data attributes in its C struct, 237*first*, *last*, and *number*. The *first* and *last* variables are Python 238strings containing first and last names. The *number* attribute is a C integer. 239 240The object structure is updated accordingly:: 241 242 typedef struct { 243 PyObject_HEAD 244 PyObject *first; /* first name */ 245 PyObject *last; /* last name */ 246 int number; 247 } CustomObject; 248 249Because we now have data to manage, we have to be more careful about object 250allocation and deallocation. At a minimum, we need a deallocation method:: 251 252 static void 253 Custom_dealloc(CustomObject *self) 254 { 255 Py_XDECREF(self->first); 256 Py_XDECREF(self->last); 257 Py_TYPE(self)->tp_free((PyObject *) self); 258 } 259 260which is assigned to the :c:member:`~PyTypeObject.tp_dealloc` member:: 261 262 .tp_dealloc = (destructor) Custom_dealloc, 263 264This method first clears the reference counts of the two Python attributes. 265:c:func:`Py_XDECREF` correctly handles the case where its argument is 266``NULL`` (which might happen here if ``tp_new`` failed midway). It then 267calls the :c:member:`~PyTypeObject.tp_free` member of the object's type 268(computed by ``Py_TYPE(self)``) to free the object's memory. Note that 269the object's type might not be :class:`!CustomType`, because the object may 270be an instance of a subclass. 271 272.. note:: 273 The explicit cast to ``destructor`` above is needed because we defined 274 ``Custom_dealloc`` to take a ``CustomObject *`` argument, but the ``tp_dealloc`` 275 function pointer expects to receive a ``PyObject *`` argument. Otherwise, 276 the compiler will emit a warning. This is object-oriented polymorphism, 277 in C! 278 279We want to make sure that the first and last names are initialized to empty 280strings, so we provide a ``tp_new`` implementation:: 281 282 static PyObject * 283 Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 284 { 285 CustomObject *self; 286 self = (CustomObject *) type->tp_alloc(type, 0); 287 if (self != NULL) { 288 self->first = PyUnicode_FromString(""); 289 if (self->first == NULL) { 290 Py_DECREF(self); 291 return NULL; 292 } 293 self->last = PyUnicode_FromString(""); 294 if (self->last == NULL) { 295 Py_DECREF(self); 296 return NULL; 297 } 298 self->number = 0; 299 } 300 return (PyObject *) self; 301 } 302 303and install it in the :c:member:`~PyTypeObject.tp_new` member:: 304 305 .tp_new = Custom_new, 306 307The ``tp_new`` handler is responsible for creating (as opposed to initializing) 308objects of the type. It is exposed in Python as the :meth:`~object.__new__` method. 309It is not required to define a ``tp_new`` member, and indeed many extension 310types will simply reuse :c:func:`PyType_GenericNew` as done in the first 311version of the :class:`!Custom` type above. In this case, we use the ``tp_new`` 312handler to initialize the ``first`` and ``last`` attributes to non-``NULL`` 313default values. 314 315``tp_new`` is passed the type being instantiated (not necessarily ``CustomType``, 316if a subclass is instantiated) and any arguments passed when the type was 317called, and is expected to return the instance created. ``tp_new`` handlers 318always accept positional and keyword arguments, but they often ignore the 319arguments, leaving the argument handling to initializer (a.k.a. ``tp_init`` 320in C or ``__init__`` in Python) methods. 321 322.. note:: 323 ``tp_new`` shouldn't call ``tp_init`` explicitly, as the interpreter 324 will do it itself. 325 326The ``tp_new`` implementation calls the :c:member:`~PyTypeObject.tp_alloc` 327slot to allocate memory:: 328 329 self = (CustomObject *) type->tp_alloc(type, 0); 330 331Since memory allocation may fail, we must check the :c:member:`~PyTypeObject.tp_alloc` 332result against ``NULL`` before proceeding. 333 334.. note:: 335 We didn't fill the :c:member:`~PyTypeObject.tp_alloc` slot ourselves. Rather 336 :c:func:`PyType_Ready` fills it for us by inheriting it from our base class, 337 which is :class:`object` by default. Most types use the default allocation 338 strategy. 339 340.. note:: 341 If you are creating a co-operative :c:member:`~PyTypeObject.tp_new` (one 342 that calls a base type's :c:member:`~PyTypeObject.tp_new` or :meth:`~object.__new__`), 343 you must *not* try to determine what method to call using method resolution 344 order at runtime. Always statically determine what type you are going to 345 call, and call its :c:member:`~PyTypeObject.tp_new` directly, or via 346 ``type->tp_base->tp_new``. If you do not do this, Python subclasses of your 347 type that also inherit from other Python-defined classes may not work correctly. 348 (Specifically, you may not be able to create instances of such subclasses 349 without getting a :exc:`TypeError`.) 350 351We also define an initialization function which accepts arguments to provide 352initial values for our instance:: 353 354 static int 355 Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) 356 { 357 static char *kwlist[] = {"first", "last", "number", NULL}; 358 PyObject *first = NULL, *last = NULL, *tmp; 359 360 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist, 361 &first, &last, 362 &self->number)) 363 return -1; 364 365 if (first) { 366 tmp = self->first; 367 Py_INCREF(first); 368 self->first = first; 369 Py_XDECREF(tmp); 370 } 371 if (last) { 372 tmp = self->last; 373 Py_INCREF(last); 374 self->last = last; 375 Py_XDECREF(tmp); 376 } 377 return 0; 378 } 379 380by filling the :c:member:`~PyTypeObject.tp_init` slot. :: 381 382 .tp_init = (initproc) Custom_init, 383 384The :c:member:`~PyTypeObject.tp_init` slot is exposed in Python as the 385:meth:`~object.__init__` method. It is used to initialize an object after it's 386created. Initializers always accept positional and keyword arguments, 387and they should return either ``0`` on success or ``-1`` on error. 388 389Unlike the ``tp_new`` handler, there is no guarantee that ``tp_init`` 390is called at all (for example, the :mod:`pickle` module by default 391doesn't call :meth:`~object.__init__` on unpickled instances). It can also be 392called multiple times. Anyone can call the :meth:`!__init__` method on 393our objects. For this reason, we have to be extra careful when assigning 394the new attribute values. We might be tempted, for example to assign the 395``first`` member like this:: 396 397 if (first) { 398 Py_XDECREF(self->first); 399 Py_INCREF(first); 400 self->first = first; 401 } 402 403But this would be risky. Our type doesn't restrict the type of the 404``first`` member, so it could be any kind of object. It could have a 405destructor that causes code to be executed that tries to access the 406``first`` member; or that destructor could release the 407:term:`Global interpreter Lock <GIL>` and let arbitrary code run in other 408threads that accesses and modifies our object. 409 410To be paranoid and protect ourselves against this possibility, we almost 411always reassign members before decrementing their reference counts. When 412don't we have to do this? 413 414* when we absolutely know that the reference count is greater than 1; 415 416* when we know that deallocation of the object [#]_ will neither release 417 the :term:`GIL` nor cause any calls back into our type's code; 418 419* when decrementing a reference count in a :c:member:`~PyTypeObject.tp_dealloc` 420 handler on a type which doesn't support cyclic garbage collection [#]_. 421 422We want to expose our instance variables as attributes. There are a 423number of ways to do that. The simplest way is to define member definitions:: 424 425 static PyMemberDef Custom_members[] = { 426 {"first", Py_T_OBJECT_EX, offsetof(CustomObject, first), 0, 427 "first name"}, 428 {"last", Py_T_OBJECT_EX, offsetof(CustomObject, last), 0, 429 "last name"}, 430 {"number", Py_T_INT, offsetof(CustomObject, number), 0, 431 "custom number"}, 432 {NULL} /* Sentinel */ 433 }; 434 435and put the definitions in the :c:member:`~PyTypeObject.tp_members` slot:: 436 437 .tp_members = Custom_members, 438 439Each member definition has a member name, type, offset, access flags and 440documentation string. See the :ref:`Generic-Attribute-Management` section 441below for details. 442 443A disadvantage of this approach is that it doesn't provide a way to restrict the 444types of objects that can be assigned to the Python attributes. We expect the 445first and last names to be strings, but any Python objects can be assigned. 446Further, the attributes can be deleted, setting the C pointers to ``NULL``. Even 447though we can make sure the members are initialized to non-``NULL`` values, the 448members can be set to ``NULL`` if the attributes are deleted. 449 450We define a single method, :meth:`!Custom.name`, that outputs the objects name as the 451concatenation of the first and last names. :: 452 453 static PyObject * 454 Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored)) 455 { 456 if (self->first == NULL) { 457 PyErr_SetString(PyExc_AttributeError, "first"); 458 return NULL; 459 } 460 if (self->last == NULL) { 461 PyErr_SetString(PyExc_AttributeError, "last"); 462 return NULL; 463 } 464 return PyUnicode_FromFormat("%S %S", self->first, self->last); 465 } 466 467The method is implemented as a C function that takes a :class:`!Custom` (or 468:class:`!Custom` subclass) instance as the first argument. Methods always take an 469instance as the first argument. Methods often take positional and keyword 470arguments as well, but in this case we don't take any and don't need to accept 471a positional argument tuple or keyword argument dictionary. This method is 472equivalent to the Python method: 473 474.. code-block:: python 475 476 def name(self): 477 return "%s %s" % (self.first, self.last) 478 479Note that we have to check for the possibility that our :attr:`!first` and 480:attr:`!last` members are ``NULL``. This is because they can be deleted, in which 481case they are set to ``NULL``. It would be better to prevent deletion of these 482attributes and to restrict the attribute values to be strings. We'll see how to 483do that in the next section. 484 485Now that we've defined the method, we need to create an array of method 486definitions:: 487 488 static PyMethodDef Custom_methods[] = { 489 {"name", (PyCFunction) Custom_name, METH_NOARGS, 490 "Return the name, combining the first and last name" 491 }, 492 {NULL} /* Sentinel */ 493 }; 494 495(note that we used the :c:macro:`METH_NOARGS` flag to indicate that the method 496is expecting no arguments other than *self*) 497 498and assign it to the :c:member:`~PyTypeObject.tp_methods` slot:: 499 500 .tp_methods = Custom_methods, 501 502Finally, we'll make our type usable as a base class for subclassing. We've 503written our methods carefully so far so that they don't make any assumptions 504about the type of the object being created or used, so all we need to do is 505to add the :c:macro:`Py_TPFLAGS_BASETYPE` to our class flag definition:: 506 507 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, 508 509We rename :c:func:`!PyInit_custom` to :c:func:`!PyInit_custom2`, update the 510module name in the :c:type:`PyModuleDef` struct, and update the full class 511name in the :c:type:`PyTypeObject` struct. 512 513Finally, we update our :file:`setup.py` file to include the new module, 514 515.. code-block:: python 516 517 from setuptools import Extension, setup 518 setup(ext_modules=[ 519 Extension("custom", ["custom.c"]), 520 Extension("custom2", ["custom2.c"]), 521 ]) 522 523and then we re-install so that we can ``import custom2``: 524 525.. code-block:: shell-session 526 527 $ python -m pip install . 528 529Providing finer control over data attributes 530============================================ 531 532In this section, we'll provide finer control over how the :attr:`!first` and 533:attr:`!last` attributes are set in the :class:`!Custom` example. In the previous 534version of our module, the instance variables :attr:`!first` and :attr:`!last` 535could be set to non-string values or even deleted. We want to make sure that 536these attributes always contain strings. 537 538.. literalinclude:: ../includes/newtypes/custom3.c 539 540 541To provide greater control, over the :attr:`!first` and :attr:`!last` attributes, 542we'll use custom getter and setter functions. Here are the functions for 543getting and setting the :attr:`!first` attribute:: 544 545 static PyObject * 546 Custom_getfirst(CustomObject *self, void *closure) 547 { 548 Py_INCREF(self->first); 549 return self->first; 550 } 551 552 static int 553 Custom_setfirst(CustomObject *self, PyObject *value, void *closure) 554 { 555 PyObject *tmp; 556 if (value == NULL) { 557 PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute"); 558 return -1; 559 } 560 if (!PyUnicode_Check(value)) { 561 PyErr_SetString(PyExc_TypeError, 562 "The first attribute value must be a string"); 563 return -1; 564 } 565 tmp = self->first; 566 Py_INCREF(value); 567 self->first = value; 568 Py_DECREF(tmp); 569 return 0; 570 } 571 572The getter function is passed a :class:`!Custom` object and a "closure", which is 573a void pointer. In this case, the closure is ignored. (The closure supports an 574advanced usage in which definition data is passed to the getter and setter. This 575could, for example, be used to allow a single set of getter and setter functions 576that decide the attribute to get or set based on data in the closure.) 577 578The setter function is passed the :class:`!Custom` object, the new value, and the 579closure. The new value may be ``NULL``, in which case the attribute is being 580deleted. In our setter, we raise an error if the attribute is deleted or if its 581new value is not a string. 582 583We create an array of :c:type:`PyGetSetDef` structures:: 584 585 static PyGetSetDef Custom_getsetters[] = { 586 {"first", (getter) Custom_getfirst, (setter) Custom_setfirst, 587 "first name", NULL}, 588 {"last", (getter) Custom_getlast, (setter) Custom_setlast, 589 "last name", NULL}, 590 {NULL} /* Sentinel */ 591 }; 592 593and register it in the :c:member:`~PyTypeObject.tp_getset` slot:: 594 595 .tp_getset = Custom_getsetters, 596 597The last item in a :c:type:`PyGetSetDef` structure is the "closure" mentioned 598above. In this case, we aren't using a closure, so we just pass ``NULL``. 599 600We also remove the member definitions for these attributes:: 601 602 static PyMemberDef Custom_members[] = { 603 {"number", Py_T_INT, offsetof(CustomObject, number), 0, 604 "custom number"}, 605 {NULL} /* Sentinel */ 606 }; 607 608We also need to update the :c:member:`~PyTypeObject.tp_init` handler to only 609allow strings [#]_ to be passed:: 610 611 static int 612 Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) 613 { 614 static char *kwlist[] = {"first", "last", "number", NULL}; 615 PyObject *first = NULL, *last = NULL, *tmp; 616 617 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist, 618 &first, &last, 619 &self->number)) 620 return -1; 621 622 if (first) { 623 tmp = self->first; 624 Py_INCREF(first); 625 self->first = first; 626 Py_DECREF(tmp); 627 } 628 if (last) { 629 tmp = self->last; 630 Py_INCREF(last); 631 self->last = last; 632 Py_DECREF(tmp); 633 } 634 return 0; 635 } 636 637With these changes, we can assure that the ``first`` and ``last`` members are 638never ``NULL`` so we can remove checks for ``NULL`` values in almost all cases. 639This means that most of the :c:func:`Py_XDECREF` calls can be converted to 640:c:func:`Py_DECREF` calls. The only place we can't change these calls is in 641the ``tp_dealloc`` implementation, where there is the possibility that the 642initialization of these members failed in ``tp_new``. 643 644We also rename the module initialization function and module name in the 645initialization function, as we did before, and we add an extra definition to the 646:file:`setup.py` file. 647 648 649Supporting cyclic garbage collection 650==================================== 651 652Python has a :term:`cyclic garbage collector (GC) <garbage collection>` that 653can identify unneeded objects even when their reference counts are not zero. 654This can happen when objects are involved in cycles. For example, consider: 655 656.. code-block:: pycon 657 658 >>> l = [] 659 >>> l.append(l) 660 >>> del l 661 662In this example, we create a list that contains itself. When we delete it, it 663still has a reference from itself. Its reference count doesn't drop to zero. 664Fortunately, Python's cyclic garbage collector will eventually figure out that 665the list is garbage and free it. 666 667In the second version of the :class:`!Custom` example, we allowed any kind of 668object to be stored in the :attr:`!first` or :attr:`!last` attributes [#]_. 669Besides, in the second and third versions, we allowed subclassing 670:class:`!Custom`, and subclasses may add arbitrary attributes. For any of 671those two reasons, :class:`!Custom` objects can participate in cycles: 672 673.. code-block:: pycon 674 675 >>> import custom3 676 >>> class Derived(custom3.Custom): pass 677 ... 678 >>> n = Derived() 679 >>> n.some_attribute = n 680 681To allow a :class:`!Custom` instance participating in a reference cycle to 682be properly detected and collected by the cyclic GC, our :class:`!Custom` type 683needs to fill two additional slots and to enable a flag that enables these slots: 684 685.. literalinclude:: ../includes/newtypes/custom4.c 686 687 688First, the traversal method lets the cyclic GC know about subobjects that could 689participate in cycles:: 690 691 static int 692 Custom_traverse(CustomObject *self, visitproc visit, void *arg) 693 { 694 int vret; 695 if (self->first) { 696 vret = visit(self->first, arg); 697 if (vret != 0) 698 return vret; 699 } 700 if (self->last) { 701 vret = visit(self->last, arg); 702 if (vret != 0) 703 return vret; 704 } 705 return 0; 706 } 707 708For each subobject that can participate in cycles, we need to call the 709:c:func:`!visit` function, which is passed to the traversal method. The 710:c:func:`!visit` function takes as arguments the subobject and the extra argument 711*arg* passed to the traversal method. It returns an integer value that must be 712returned if it is non-zero. 713 714Python provides a :c:func:`Py_VISIT` macro that automates calling visit 715functions. With :c:func:`Py_VISIT`, we can minimize the amount of boilerplate 716in ``Custom_traverse``:: 717 718 static int 719 Custom_traverse(CustomObject *self, visitproc visit, void *arg) 720 { 721 Py_VISIT(self->first); 722 Py_VISIT(self->last); 723 return 0; 724 } 725 726.. note:: 727 The :c:member:`~PyTypeObject.tp_traverse` implementation must name its 728 arguments exactly *visit* and *arg* in order to use :c:func:`Py_VISIT`. 729 730Second, we need to provide a method for clearing any subobjects that can 731participate in cycles:: 732 733 static int 734 Custom_clear(CustomObject *self) 735 { 736 Py_CLEAR(self->first); 737 Py_CLEAR(self->last); 738 return 0; 739 } 740 741Notice the use of the :c:func:`Py_CLEAR` macro. It is the recommended and safe 742way to clear data attributes of arbitrary types while decrementing 743their reference counts. If you were to call :c:func:`Py_XDECREF` instead 744on the attribute before setting it to ``NULL``, there is a possibility 745that the attribute's destructor would call back into code that reads the 746attribute again (*especially* if there is a reference cycle). 747 748.. note:: 749 You could emulate :c:func:`Py_CLEAR` by writing:: 750 751 PyObject *tmp; 752 tmp = self->first; 753 self->first = NULL; 754 Py_XDECREF(tmp); 755 756 Nevertheless, it is much easier and less error-prone to always 757 use :c:func:`Py_CLEAR` when deleting an attribute. Don't 758 try to micro-optimize at the expense of robustness! 759 760The deallocator ``Custom_dealloc`` may call arbitrary code when clearing 761attributes. It means the circular GC can be triggered inside the function. 762Since the GC assumes reference count is not zero, we need to untrack the object 763from the GC by calling :c:func:`PyObject_GC_UnTrack` before clearing members. 764Here is our reimplemented deallocator using :c:func:`PyObject_GC_UnTrack` 765and ``Custom_clear``:: 766 767 static void 768 Custom_dealloc(CustomObject *self) 769 { 770 PyObject_GC_UnTrack(self); 771 Custom_clear(self); 772 Py_TYPE(self)->tp_free((PyObject *) self); 773 } 774 775Finally, we add the :c:macro:`Py_TPFLAGS_HAVE_GC` flag to the class flags:: 776 777 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, 778 779That's pretty much it. If we had written custom :c:member:`~PyTypeObject.tp_alloc` or 780:c:member:`~PyTypeObject.tp_free` handlers, we'd need to modify them for cyclic 781garbage collection. Most extensions will use the versions automatically provided. 782 783 784Subclassing other types 785======================= 786 787It is possible to create new extension types that are derived from existing 788types. It is easiest to inherit from the built in types, since an extension can 789easily use the :c:type:`PyTypeObject` it needs. It can be difficult to share 790these :c:type:`PyTypeObject` structures between extension modules. 791 792In this example we will create a :class:`!SubList` type that inherits from the 793built-in :class:`list` type. The new type will be completely compatible with 794regular lists, but will have an additional :meth:`!increment` method that 795increases an internal counter: 796 797.. code-block:: pycon 798 799 >>> import sublist 800 >>> s = sublist.SubList(range(3)) 801 >>> s.extend(s) 802 >>> print(len(s)) 803 6 804 >>> print(s.increment()) 805 1 806 >>> print(s.increment()) 807 2 808 809.. literalinclude:: ../includes/newtypes/sublist.c 810 811 812As you can see, the source code closely resembles the :class:`!Custom` examples in 813previous sections. We will break down the main differences between them. :: 814 815 typedef struct { 816 PyListObject list; 817 int state; 818 } SubListObject; 819 820The primary difference for derived type objects is that the base type's 821object structure must be the first value. The base type will already include 822the :c:func:`PyObject_HEAD` at the beginning of its structure. 823 824When a Python object is a :class:`!SubList` instance, its ``PyObject *`` pointer 825can be safely cast to both ``PyListObject *`` and ``SubListObject *``:: 826 827 static int 828 SubList_init(SubListObject *self, PyObject *args, PyObject *kwds) 829 { 830 if (PyList_Type.tp_init((PyObject *) self, args, kwds) < 0) 831 return -1; 832 self->state = 0; 833 return 0; 834 } 835 836We see above how to call through to the :meth:`~object.__init__` method of the base 837type. 838 839This pattern is important when writing a type with custom 840:c:member:`~PyTypeObject.tp_new` and :c:member:`~PyTypeObject.tp_dealloc` 841members. The :c:member:`~PyTypeObject.tp_new` handler should not actually 842create the memory for the object with its :c:member:`~PyTypeObject.tp_alloc`, 843but let the base class handle it by calling its own :c:member:`~PyTypeObject.tp_new`. 844 845The :c:type:`PyTypeObject` struct supports a :c:member:`~PyTypeObject.tp_base` 846specifying the type's concrete base class. Due to cross-platform compiler 847issues, you can't fill that field directly with a reference to 848:c:type:`PyList_Type`; it should be done later in the module initialization 849function:: 850 851 PyMODINIT_FUNC 852 PyInit_sublist(void) 853 { 854 PyObject* m; 855 SubListType.tp_base = &PyList_Type; 856 if (PyType_Ready(&SubListType) < 0) 857 return NULL; 858 859 m = PyModule_Create(&sublistmodule); 860 if (m == NULL) 861 return NULL; 862 863 if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) { 864 Py_DECREF(m); 865 return NULL; 866 } 867 868 return m; 869 } 870 871Before calling :c:func:`PyType_Ready`, the type structure must have the 872:c:member:`~PyTypeObject.tp_base` slot filled in. When we are deriving an 873existing type, it is not necessary to fill out the :c:member:`~PyTypeObject.tp_alloc` 874slot with :c:func:`PyType_GenericNew` -- the allocation function from the base 875type will be inherited. 876 877After that, calling :c:func:`PyType_Ready` and adding the type object to the 878module is the same as with the basic :class:`!Custom` examples. 879 880 881.. rubric:: Footnotes 882 883.. [#] This is true when we know that the object is a basic type, like a string or a 884 float. 885 886.. [#] We relied on this in the :c:member:`~PyTypeObject.tp_dealloc` handler 887 in this example, because our type doesn't support garbage collection. 888 889.. [#] We now know that the first and last members are strings, so perhaps we 890 could be less careful about decrementing their reference counts, however, 891 we accept instances of string subclasses. Even though deallocating normal 892 strings won't call back into our objects, we can't guarantee that deallocating 893 an instance of a string subclass won't call back into our objects. 894 895.. [#] Also, even with our attributes restricted to strings instances, the user 896 could pass arbitrary :class:`str` subclasses and therefore still create 897 reference cycles. 898