Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 18.117.192.64
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
extending /
[ HOME SHELL ]
Name
Size
Permission
Action
building.html
16
KB
-rw-r--r--
embedding.html
36.01
KB
-rw-r--r--
extending.html
128.85
KB
-rw-r--r--
index.html
11.13
KB
-rw-r--r--
newtypes.html
246.07
KB
-rw-r--r--
windows.html
28.36
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : newtypes.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>2. Defining New Types — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="Extending and Embedding the Python Interpreter" href="index.html" /> <link rel="next" title="3. Building C and C++ Extensions with distutils" href="building.html" /> <link rel="prev" title="1. Extending Python with C or C++" href="extending.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="building.html" title="3. Building C and C++ Extensions with distutils" accesskey="N">next</a> |</li> <li class="right" > <a href="extending.html" title="1. Extending Python with C or C++" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" accesskey="U">Extending and Embedding the Python Interpreter</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="defining-new-types"> <span id="id1"></span><h1>2. Defining New Types<a class="headerlink" href="#defining-new-types" title="Permalink to this headline">¶</a></h1> <p>As mentioned in the last chapter, Python allows the writer of an extension module to define new types that can be manipulated from Python code, much like strings and lists in core Python.</p> <p>This is not hard; the code for all extension types follows a pattern, but there are some details that you need to understand before you can get started.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The way new types are defined changed dramatically (and for the better) in Python 2.2. This document documents how to define new types for Python 2.2 and later. If you need to support older versions of Python, you will need to refer to <a class="reference external" href="http://www.python.org/doc/versions/">older versions of this documentation</a>.</p> </div> <div class="section" id="the-basics"> <span id="dnt-basics"></span><h2>2.1. The Basics<a class="headerlink" href="#the-basics" title="Permalink to this headline">¶</a></h2> <p>The Python runtime sees all Python objects as variables of type <a class="reference internal" href="../c-api/structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a>. A <a class="reference internal" href="../c-api/structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject</span></tt></a> is not a very magnificent object - it just contains the refcount and a pointer to the object’s “type object”. This is where the action is; the type object determines which (C) functions get called when, for instance, an attribute gets looked up on an object or it is multiplied by another object. These C functions are called “type methods”.</p> <p>So, if you want to define a new object type, you need to create a new type object.</p> <p>This sort of thing can only be explained by example, so here’s a minimal, but complete, module that defines a new type:</p> <div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <Python.h></span> <span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="cm">/* Type-specific fields go here. */</span> <span class="p">}</span> <span class="n">noddy_NoddyObject</span><span class="p">;</span> <span class="k">static</span> <span class="n">PyTypeObject</span> <span class="n">noddy_NoddyType</span> <span class="o">=</span> <span class="p">{</span> <span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*ob_size*/</span> <span class="s">"noddy.Noddy"</span><span class="p">,</span> <span class="cm">/*tp_name*/</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">noddy_NoddyObject</span><span class="p">),</span> <span class="cm">/*tp_basicsize*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_itemsize*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_dealloc*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_print*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_compare*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_repr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_number*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_sequence*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_mapping*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_hash */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_call*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_str*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_buffer*/</span> <span class="n">Py_TPFLAGS_DEFAULT</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> <span class="s">"Noddy objects"</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">noddy_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="cp">#ifndef PyMODINIT_FUNC </span><span class="cm">/* declarations for DLL import/export */</span><span class="cp"></span> <span class="cp">#define PyMODINIT_FUNC void</span> <span class="cp">#endif</span> <span class="n">PyMODINIT_FUNC</span> <span class="nf">initnoddy</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span><span class="o">*</span> <span class="n">m</span><span class="p">;</span> <span class="n">noddy_NoddyType</span><span class="p">.</span><span class="n">tp_new</span> <span class="o">=</span> <span class="n">PyType_GenericNew</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">noddy_NoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">m</span> <span class="o">=</span> <span class="n">Py_InitModule3</span><span class="p">(</span><span class="s">"noddy"</span><span class="p">,</span> <span class="n">noddy_methods</span><span class="p">,</span> <span class="s">"Example module that creates an extension type."</span><span class="p">);</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="o">&</span><span class="n">noddy_NoddyType</span><span class="p">);</span> <span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Noddy"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="o">&</span><span class="n">noddy_NoddyType</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>Now that’s quite a bit to take in at once, but hopefully bits will seem familiar from the last chapter.</p> <p>The first bit that will be new is:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="p">}</span> <span class="n">noddy_NoddyObject</span><span class="p">;</span> </pre></div> </div> <p>This is what a Noddy object will contain—in this case, nothing more than every Python object contains, namely a refcount and a pointer to a type object. These are the fields the <tt class="docutils literal"><span class="pre">PyObject_HEAD</span></tt> macro brings in. The reason for the macro is to standardize the layout and to enable special debugging fields in debug builds. Note that there is no semicolon after the <tt class="docutils literal"><span class="pre">PyObject_HEAD</span></tt> macro; one is included in the macro definition. Be wary of adding one by accident; it’s easy to do from habit, and your compiler might not complain, but someone else’s probably will! (On Windows, MSVC is known to call this an error and refuse to compile the code.)</p> <p>For contrast, let’s take a look at the corresponding definition for standard Python integers:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="kt">long</span> <span class="n">ob_ival</span><span class="p">;</span> <span class="p">}</span> <span class="n">PyIntObject</span><span class="p">;</span> </pre></div> </div> <p>Moving on, we come to the crunch — the type object.</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyTypeObject</span> <span class="n">noddy_NoddyType</span> <span class="o">=</span> <span class="p">{</span> <span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*ob_size*/</span> <span class="s">"noddy.Noddy"</span><span class="p">,</span> <span class="cm">/*tp_name*/</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">noddy_NoddyObject</span><span class="p">),</span> <span class="cm">/*tp_basicsize*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_itemsize*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_dealloc*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_print*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_compare*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_repr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_number*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_sequence*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_mapping*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_hash */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_call*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_str*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_buffer*/</span> <span class="n">Py_TPFLAGS_DEFAULT</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> <span class="s">"Noddy objects"</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> <span class="p">};</span> </pre></div> </div> <p>Now if you go and look up the definition of <a class="reference internal" href="../c-api/type.html#PyTypeObject" title="PyTypeObject"><tt class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></tt></a> in <tt class="file docutils literal"><span class="pre">object.h</span></tt> you’ll see that it has many more fields that the definition above. The remaining fields will be filled with zeros by the C compiler, and it’s common practice to not specify them explicitly unless you need them.</p> <p>This is so important that we’re going to pick the top of it apart still further:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> </pre></div> </div> <p>This line is a bit of a wart; what we’d like to write is:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="o">&</span><span class="n">PyType_Type</span><span class="p">)</span> </pre></div> </div> <p>as the type of a type object is “type”, but this isn’t strictly conforming C and some compilers complain. Fortunately, this member will be filled in for us by <a class="reference internal" href="../c-api/type.html#PyType_Ready" title="PyType_Ready"><tt class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></tt></a>.</p> <div class="highlight-c"><div class="highlight"><pre><span class="mi">0</span><span class="p">,</span> <span class="cm">/* ob_size */</span> </pre></div> </div> <p>The <tt class="xref py py-attr docutils literal"><span class="pre">ob_size</span></tt> field of the header is not used; its presence in the type structure is a historical artifact that is maintained for binary compatibility with extension modules compiled for older versions of Python. Always set this field to zero.</p> <div class="highlight-c"><div class="highlight"><pre><span class="s">"noddy.Noddy"</span><span class="p">,</span> <span class="cm">/* tp_name */</span> </pre></div> </div> <p>The name of our type. This will appear in the default textual representation of our objects and in some error messages, for example:</p> <div class="highlight-c"><div class="highlight"><pre><span class="o">>>></span> <span class="s">""</span> <span class="o">+</span> <span class="n">noddy</span><span class="p">.</span><span class="n">new_noddy</span><span class="p">()</span> <span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">)</span><span class="o">:</span> <span class="n">File</span> <span class="s">"<stdin>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="n">in</span> <span class="o">?</span> <span class="nl">TypeError:</span> <span class="n">cannot</span> <span class="n">add</span> <span class="n">type</span> <span class="s">"noddy.Noddy"</span> <span class="n">to</span> <span class="n">string</span> </pre></div> </div> <p>Note that the name is a dotted name that includes both the module name and the name of the type within the module. The module in this case is <tt class="xref py py-mod docutils literal"><span class="pre">noddy</span></tt> and the type is <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt>, so we set the type name to <tt class="xref py py-class docutils literal"><span class="pre">noddy.Noddy</span></tt>.</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">sizeof</span><span class="p">(</span><span class="n">noddy_NoddyObject</span><span class="p">),</span> <span class="cm">/* tp_basicsize */</span> </pre></div> </div> <p>This is so that Python knows how much memory to allocate when you call <a class="reference internal" href="../c-api/allocation.html#PyObject_New" title="PyObject_New"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_New()</span></tt></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">If you want your type to be subclassable from Python, and your type has the same <tt class="xref py py-attr docutils literal"><span class="pre">tp_basicsize</span></tt> as its base type, you may have problems with multiple inheritance. A Python subclass of your type will have to list your type first in its <tt class="xref py py-attr docutils literal"><span class="pre">__bases__</span></tt>, or else it will not be able to call your type’s <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> method without getting an error. You can avoid this problem by ensuring that your type has a larger value for <tt class="xref py py-attr docutils literal"><span class="pre">tp_basicsize</span></tt> than its base type does. Most of the time, this will be true anyway, because either your base type will be <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a>, or else you will be adding data members to your base type, and therefore increasing its size.</p> </div> <div class="highlight-c"><div class="highlight"><pre><span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_itemsize */</span> </pre></div> </div> <p>This has to do with variable length objects like lists and strings. Ignore this for now.</p> <p>Skipping a number of type methods that we don’t provide, we set the class flags to <a class="reference internal" href="../c-api/typeobj.html#Py_TPFLAGS_DEFAULT" title="Py_TPFLAGS_DEFAULT"><tt class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_DEFAULT</span></tt></a>.</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Py_TPFLAGS_DEFAULT</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> </pre></div> </div> <p>All types should include this constant in their flags. It enables all of the members defined by the current version of Python.</p> <p>We provide a doc string for the type in <tt class="xref py py-attr docutils literal"><span class="pre">tp_doc</span></tt>.</p> <div class="highlight-c"><div class="highlight"><pre><span class="s">"Noddy objects"</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> </pre></div> </div> <p>Now we get into the type methods, the things that make your objects different from the others. We aren’t going to implement any of these in this version of the module. We’ll expand this example later to have more interesting behavior.</p> <p>For now, all we want to be able to do is to create new <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> objects. To enable object creation, we have to provide a <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt> implementation. In this case, we can just use the default implementation provided by the API function <a class="reference internal" href="../c-api/type.html#PyType_GenericNew" title="PyType_GenericNew"><tt class="xref c c-func docutils literal"><span class="pre">PyType_GenericNew()</span></tt></a>. We’d like to just assign this to the <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt> slot, but we can’t, for portability sake, On some platforms or compilers, we can’t statically initialize a structure member with a function defined in another C module, so, instead, we’ll assign the <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt> slot in the module initialization function just before calling <a class="reference internal" href="../c-api/type.html#PyType_Ready" title="PyType_Ready"><tt class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></tt></a>:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">noddy_NoddyType</span><span class="p">.</span><span class="n">tp_new</span> <span class="o">=</span> <span class="n">PyType_GenericNew</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">noddy_NoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> </pre></div> </div> <p>All the other type methods are <em>NULL</em>, so we’ll go over them later — that’s for a later section!</p> <p>Everything else in the file should be familiar, except for some code in <tt class="xref c c-func docutils literal"><span class="pre">initnoddy()</span></tt>:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">noddy_NoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> </pre></div> </div> <p>This initializes the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> type, filing in a number of members, including <tt class="xref py py-attr docutils literal"><span class="pre">ob_type</span></tt> that we initially set to <em>NULL</em>.</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Noddy"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="o">&</span><span class="n">noddy_NoddyType</span><span class="p">);</span> </pre></div> </div> <p>This adds the type to the module dictionary. This allows us to create <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> instances by calling the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> class:</p> <div class="highlight-c"><div class="highlight"><pre><span class="o">>>></span> <span class="n">import</span> <span class="n">noddy</span> <span class="o">>>></span> <span class="n">mynoddy</span> <span class="o">=</span> <span class="n">noddy</span><span class="p">.</span><span class="n">Noddy</span><span class="p">()</span> </pre></div> </div> <p>That’s it! All that remains is to build it; put the above code in a file called <tt class="file docutils literal"><span class="pre">noddy.c</span></tt> and</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">from</span> <span class="n">distutils</span><span class="p">.</span><span class="n">core</span> <span class="n">import</span> <span class="n">setup</span><span class="p">,</span> <span class="n">Extension</span> <span class="n">setup</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"noddy"</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s">"1.0"</span><span class="p">,</span> <span class="n">ext_modules</span><span class="o">=</span><span class="p">[</span><span class="n">Extension</span><span class="p">(</span><span class="s">"noddy"</span><span class="p">,</span> <span class="p">[</span><span class="s">"noddy.c"</span><span class="p">])])</span> </pre></div> </div> <p>in a file called <tt class="file docutils literal"><span class="pre">setup.py</span></tt>; then typing</p> <div class="highlight-c"><pre>$ python setup.py build</pre> </div> <p>at a shell should produce a file <tt class="file docutils literal"><span class="pre">noddy.so</span></tt> in a subdirectory; move to that directory and fire up Python — you should be able to <tt class="docutils literal"><span class="pre">import</span> <span class="pre">noddy</span></tt> and play around with Noddy objects.</p> <p>That wasn’t so hard, was it?</p> <p>Of course, the current Noddy type is pretty uninteresting. It has no data and doesn’t do anything. It can’t even be subclassed.</p> <div class="section" id="adding-data-and-methods-to-the-basic-example"> <h3>2.1.1. Adding data and methods to the Basic example<a class="headerlink" href="#adding-data-and-methods-to-the-basic-example" title="Permalink to this headline">¶</a></h3> <p>Let’s expend the basic example to add some data and methods. Let’s also make the type usable as a base class. We’ll create a new module, <tt class="xref py py-mod docutils literal"><span class="pre">noddy2</span></tt> that adds these capabilities:</p> <div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <Python.h></span> <span class="cp">#include "structmember.h"</span> <span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="p">;</span> <span class="cm">/* first name */</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">last</span><span class="p">;</span> <span class="cm">/* last name */</span> <span class="kt">int</span> <span class="n">number</span><span class="p">;</span> <span class="p">}</span> <span class="n">Noddy</span><span class="p">;</span> <span class="k">static</span> <span class="kt">void</span> <span class="nf">Noddy_dealloc</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">ob_type</span><span class="o">-></span><span class="n">tp_free</span><span class="p">((</span><span class="n">PyObject</span><span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">);</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_new</span><span class="p">(</span><span class="n">PyTypeObject</span> <span class="o">*</span><span class="n">type</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">;</span> <span class="n">self</span> <span class="o">=</span> <span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="p">)</span><span class="n">type</span><span class="o">-></span><span class="n">tp_alloc</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">number</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_init</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">last</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">tmp</span><span class="p">;</span> <span class="k">static</span> <span class="kt">char</span> <span class="o">*</span><span class="n">kwlist</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="s">"last"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">};</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyArg_ParseTupleAndKeywords</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">,</span> <span class="s">"|OOi"</span><span class="p">,</span> <span class="n">kwlist</span><span class="p">,</span> <span class="o">&</span><span class="n">first</span><span class="p">,</span> <span class="o">&</span><span class="n">last</span><span class="p">,</span> <span class="o">&</span><span class="n">self</span><span class="o">-></span><span class="n">number</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">first</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">first</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">last</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">last</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyMemberDef</span> <span class="n">Noddy_members</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="n">T_OBJECT_EX</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">first</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"first name"</span><span class="p">},</span> <span class="p">{</span><span class="s">"last"</span><span class="p">,</span> <span class="n">T_OBJECT_EX</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">last</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"last name"</span><span class="p">},</span> <span class="p">{</span><span class="s">"number"</span><span class="p">,</span> <span class="n">T_INT</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">number</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"noddy number"</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_name</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">format</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">*</span><span class="n">result</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">format</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">"%s %s"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_AttributeError</span><span class="p">,</span> <span class="s">"first"</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_AttributeError</span><span class="p">,</span> <span class="s">"last"</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">args</span> <span class="o">=</span> <span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"OO"</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">args</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">PyString_Format</span><span class="p">(</span><span class="n">format</span><span class="p">,</span> <span class="n">args</span><span class="p">);</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">args</span><span class="p">);</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">Noddy_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"name"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">Noddy_name</span><span class="p">,</span> <span class="n">METH_NOARGS</span><span class="p">,</span> <span class="s">"Return the name, combining the first and last name"</span> <span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyTypeObject</span> <span class="n">NoddyType</span> <span class="o">=</span> <span class="p">{</span> <span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*ob_size*/</span> <span class="s">"noddy.Noddy"</span><span class="p">,</span> <span class="cm">/*tp_name*/</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">),</span> <span class="cm">/*tp_basicsize*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_itemsize*/</span> <span class="p">(</span><span class="n">destructor</span><span class="p">)</span><span class="n">Noddy_dealloc</span><span class="p">,</span> <span class="cm">/*tp_dealloc*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_print*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_compare*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_repr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_number*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_sequence*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_mapping*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_hash */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_call*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_str*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_buffer*/</span> <span class="n">Py_TPFLAGS_DEFAULT</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_BASETYPE</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> <span class="s">"Noddy objects"</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_traverse */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_clear */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_richcompare */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_weaklistoffset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iter */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iternext */</span> <span class="n">Noddy_methods</span><span class="p">,</span> <span class="cm">/* tp_methods */</span> <span class="n">Noddy_members</span><span class="p">,</span> <span class="cm">/* tp_members */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_getset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_base */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dict */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_get */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_set */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dictoffset */</span> <span class="p">(</span><span class="n">initproc</span><span class="p">)</span><span class="n">Noddy_init</span><span class="p">,</span> <span class="cm">/* tp_init */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_alloc */</span> <span class="n">Noddy_new</span><span class="p">,</span> <span class="cm">/* tp_new */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">module_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="cp">#ifndef PyMODINIT_FUNC </span><span class="cm">/* declarations for DLL import/export */</span><span class="cp"></span> <span class="cp">#define PyMODINIT_FUNC void</span> <span class="cp">#endif</span> <span class="n">PyMODINIT_FUNC</span> <span class="nf">initnoddy2</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span><span class="o">*</span> <span class="n">m</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">m</span> <span class="o">=</span> <span class="n">Py_InitModule3</span><span class="p">(</span><span class="s">"noddy2"</span><span class="p">,</span> <span class="n">module_methods</span><span class="p">,</span> <span class="s">"Example module that creates an extension type."</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">);</span> <span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Noddy"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>This version of the module has a number of changes.</p> <p>We’ve added an extra include:</p> <div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <structmember.h></span> </pre></div> </div> <p>This include provides declarations that we use to handle attributes, as described a bit later.</p> <p>The name of the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> object structure has been shortened to <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt>. The type object name has been shortened to <tt class="xref py py-class docutils literal"><span class="pre">NoddyType</span></tt>.</p> <p>The <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> type now has three data attributes, <em>first</em>, <em>last</em>, and <em>number</em>. The <em>first</em> and <em>last</em> variables are Python strings containing first and last names. The <em>number</em> attribute is an integer.</p> <p>The object structure is updated accordingly:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">last</span><span class="p">;</span> <span class="kt">int</span> <span class="n">number</span><span class="p">;</span> <span class="p">}</span> <span class="n">Noddy</span><span class="p">;</span> </pre></div> </div> <p>Because we now have data to manage, we have to be more careful about object allocation and deallocation. At a minimum, we need a deallocation method:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">Noddy_dealloc</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">ob_type</span><span class="o">-></span><span class="n">tp_free</span><span class="p">((</span><span class="n">PyObject</span><span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>which is assigned to the <tt class="xref py py-attr docutils literal"><span class="pre">tp_dealloc</span></tt> member:</p> <div class="highlight-c"><div class="highlight"><pre><span class="p">(</span><span class="n">destructor</span><span class="p">)</span><span class="n">Noddy_dealloc</span><span class="p">,</span> <span class="cm">/*tp_dealloc*/</span> </pre></div> </div> <p>This method decrements the reference counts of the two Python attributes. We use <a class="reference internal" href="../c-api/refcounting.html#Py_XDECREF" title="Py_XDECREF"><tt class="xref c c-func docutils literal"><span class="pre">Py_XDECREF()</span></tt></a> here because the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> members could be <em>NULL</em>. It then calls the <tt class="xref py py-attr docutils literal"><span class="pre">tp_free</span></tt> member of the object’s type to free the object’s memory. Note that the object’s type might not be <tt class="xref py py-class docutils literal"><span class="pre">NoddyType</span></tt>, because the object may be an instance of a subclass.</p> <p>We want to make sure that the first and last names are initialized to empty strings, so we provide a new method:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_new</span><span class="p">(</span><span class="n">PyTypeObject</span> <span class="o">*</span><span class="n">type</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">;</span> <span class="n">self</span> <span class="o">=</span> <span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="p">)</span><span class="n">type</span><span class="o">-></span><span class="n">tp_alloc</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">number</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>and install it in the <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt> member:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Noddy_new</span><span class="p">,</span> <span class="cm">/* tp_new */</span> </pre></div> </div> <p>The new member is responsible for creating (as opposed to initializing) objects of the type. It is exposed in Python as the <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> method. See the paper titled “Unifying types and classes in Python” for a detailed discussion of the <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a> method. One reason to implement a new method is to assure the initial values of instance variables. In this case, we use the new method to make sure that the initial values of the members <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> are not <em>NULL</em>. If we didn’t care whether the initial values were <em>NULL</em>, we could have used <a class="reference internal" href="../c-api/type.html#PyType_GenericNew" title="PyType_GenericNew"><tt class="xref c c-func docutils literal"><span class="pre">PyType_GenericNew()</span></tt></a> as our new method, as we did before. <a class="reference internal" href="../c-api/type.html#PyType_GenericNew" title="PyType_GenericNew"><tt class="xref c c-func docutils literal"><span class="pre">PyType_GenericNew()</span></tt></a> initializes all of the instance variable members to <em>NULL</em>.</p> <p>The new method is a static method that is passed the type being instantiated and any arguments passed when the type was called, and that returns the new object created. New methods always accept positional and keyword arguments, but they often ignore the arguments, leaving the argument handling to initializer methods. Note that if the type supports subclassing, the type passed may not be the type being defined. The new method calls the tp_alloc slot to allocate memory. We don’t fill the <tt class="xref py py-attr docutils literal"><span class="pre">tp_alloc</span></tt> slot ourselves. Rather <a class="reference internal" href="../c-api/type.html#PyType_Ready" title="PyType_Ready"><tt class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></tt></a> fills it for us by inheriting it from our base class, which is <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> by default. Most types use the default allocation.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">If you are creating a co-operative <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt> (one that calls a base type’s <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt> or <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><tt class="xref py py-meth docutils literal"><span class="pre">__new__()</span></tt></a>), you must <em>not</em> try to determine what method to call using method resolution order at runtime. Always statically determine what type you are going to call, and call its <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt> directly, or via <tt class="docutils literal"><span class="pre">type->tp_base->tp_new</span></tt>. If you do not do this, Python subclasses of your type that also inherit from other Python-defined classes may not work correctly. (Specifically, you may not be able to create instances of such subclasses without getting a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.)</p> </div> <p>We provide an initialization function:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_init</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">last</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">tmp</span><span class="p">;</span> <span class="k">static</span> <span class="kt">char</span> <span class="o">*</span><span class="n">kwlist</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="s">"last"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">};</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyArg_ParseTupleAndKeywords</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">,</span> <span class="s">"|OOi"</span><span class="p">,</span> <span class="n">kwlist</span><span class="p">,</span> <span class="o">&</span><span class="n">first</span><span class="p">,</span> <span class="o">&</span><span class="n">last</span><span class="p">,</span> <span class="o">&</span><span class="n">self</span><span class="o">-></span><span class="n">number</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">first</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">first</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">last</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">last</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>by filling the <tt class="xref py py-attr docutils literal"><span class="pre">tp_init</span></tt> slot.</p> <div class="highlight-c"><div class="highlight"><pre><span class="p">(</span><span class="n">initproc</span><span class="p">)</span><span class="n">Noddy_init</span><span class="p">,</span> <span class="cm">/* tp_init */</span> </pre></div> </div> <p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_init</span></tt> slot is exposed in Python as the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method. It is used to initialize an object after it’s created. Unlike the new method, we can’t guarantee that the initializer is called. The initializer isn’t called when unpickling objects and it can be overridden. Our initializer accepts arguments to provide initial values for our instance. Initializers always accept positional and keyword arguments.</p> <p>Initializers can be called multiple times. Anyone can call the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method on our objects. For this reason, we have to be extra careful when assigning the new values. We might be tempted, for example to assign the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> member like this:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">first</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">first</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>But this would be risky. Our type doesn’t restrict the type of the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> member, so it could be any kind of object. It could have a destructor that causes code to be executed that tries to access the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> member. To be paranoid and protect ourselves against this possibility, we almost always reassign members before decrementing their reference counts. When don’t we have to do this?</p> <ul class="simple"> <li>when we absolutely know that the reference count is greater than 1</li> <li>when we know that deallocation of the object <a class="footnote-reference" href="#id7" id="id2">[1]</a> will not cause any calls back into our type’s code</li> <li>when decrementing a reference count in a <tt class="xref py py-attr docutils literal"><span class="pre">tp_dealloc</span></tt> handler when garbage-collections is not supported <a class="footnote-reference" href="#id8" id="id3">[2]</a></li> </ul> <p>We want to expose our instance variables as attributes. There are a number of ways to do that. The simplest way is to define member definitions:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyMemberDef</span> <span class="n">Noddy_members</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="n">T_OBJECT_EX</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">first</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"first name"</span><span class="p">},</span> <span class="p">{</span><span class="s">"last"</span><span class="p">,</span> <span class="n">T_OBJECT_EX</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">last</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"last name"</span><span class="p">},</span> <span class="p">{</span><span class="s">"number"</span><span class="p">,</span> <span class="n">T_INT</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">number</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"noddy number"</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> </pre></div> </div> <p>and put the definitions in the <tt class="xref py py-attr docutils literal"><span class="pre">tp_members</span></tt> slot:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Noddy_members</span><span class="p">,</span> <span class="cm">/* tp_members */</span> </pre></div> </div> <p>Each member definition has a member name, type, offset, access flags and documentation string. See the <a class="reference internal" href="#generic-attribute-management"><em>Generic Attribute Management</em></a> section below for details.</p> <p>A disadvantage of this approach is that it doesn’t provide a way to restrict the types of objects that can be assigned to the Python attributes. We expect the first and last names to be strings, but any Python objects can be assigned. Further, the attributes can be deleted, setting the C pointers to <em>NULL</em>. Even though we can make sure the members are initialized to non-<em>NULL</em> values, the members can be set to <em>NULL</em> if the attributes are deleted.</p> <p>We define a single method, <tt class="xref py py-meth docutils literal"><span class="pre">name()</span></tt>, that outputs the objects name as the concatenation of the first and last names.</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_name</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">format</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">*</span><span class="n">result</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">format</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">"%s %s"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_AttributeError</span><span class="p">,</span> <span class="s">"first"</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_AttributeError</span><span class="p">,</span> <span class="s">"last"</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">args</span> <span class="o">=</span> <span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"OO"</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">args</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">PyString_Format</span><span class="p">(</span><span class="n">format</span><span class="p">,</span> <span class="n">args</span><span class="p">);</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">args</span><span class="p">);</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>The method is implemented as a C function that takes a <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> (or <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> subclass) instance as the first argument. Methods always take an instance as the first argument. Methods often take positional and keyword arguments as well, but in this cased we don’t take any and don’t need to accept a positional argument tuple or keyword argument dictionary. This method is equivalent to the Python method:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">def</span> <span class="n">name</span><span class="p">(</span><span class="n">self</span><span class="p">)</span><span class="o">:</span> <span class="k">return</span> <span class="s">"%s %s"</span> <span class="o">%</span> <span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">first</span><span class="p">,</span> <span class="n">self</span><span class="p">.</span><span class="n">last</span><span class="p">)</span> </pre></div> </div> <p>Note that we have to check for the possibility that our <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> members are <em>NULL</em>. This is because they can be deleted, in which case they are set to <em>NULL</em>. It would be better to prevent deletion of these attributes and to restrict the attribute values to be strings. We’ll see how to do that in the next section.</p> <p>Now that we’ve defined the method, we need to create an array of method definitions:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">Noddy_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"name"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">Noddy_name</span><span class="p">,</span> <span class="n">METH_NOARGS</span><span class="p">,</span> <span class="s">"Return the name, combining the first and last name"</span> <span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> </pre></div> </div> <p>and assign them to the <tt class="xref py py-attr docutils literal"><span class="pre">tp_methods</span></tt> slot:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Noddy_methods</span><span class="p">,</span> <span class="cm">/* tp_methods */</span> </pre></div> </div> <p>Note that we used the <a class="reference internal" href="../c-api/structures.html#METH_NOARGS" title="METH_NOARGS"><tt class="xref py py-const docutils literal"><span class="pre">METH_NOARGS</span></tt></a> flag to indicate that the method is passed no arguments.</p> <p>Finally, we’ll make our type usable as a base class. We’ve written our methods carefully so far so that they don’t make any assumptions about the type of the object being created or used, so all we need to do is to add the <a class="reference internal" href="../c-api/typeobj.html#Py_TPFLAGS_BASETYPE" title="Py_TPFLAGS_BASETYPE"><tt class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_BASETYPE</span></tt></a> to our class flag definition:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Py_TPFLAGS_DEFAULT</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_BASETYPE</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> </pre></div> </div> <p>We rename <tt class="xref c c-func docutils literal"><span class="pre">initnoddy()</span></tt> to <tt class="xref c c-func docutils literal"><span class="pre">initnoddy2()</span></tt> and update the module name passed to <a class="reference internal" href="../c-api/allocation.html#Py_InitModule3" title="Py_InitModule3"><tt class="xref c c-func docutils literal"><span class="pre">Py_InitModule3()</span></tt></a>.</p> <p>Finally, we update our <tt class="file docutils literal"><span class="pre">setup.py</span></tt> file to build the new module:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">from</span> <span class="n">distutils</span><span class="p">.</span><span class="n">core</span> <span class="n">import</span> <span class="n">setup</span><span class="p">,</span> <span class="n">Extension</span> <span class="n">setup</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"noddy"</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s">"1.0"</span><span class="p">,</span> <span class="n">ext_modules</span><span class="o">=</span><span class="p">[</span> <span class="n">Extension</span><span class="p">(</span><span class="s">"noddy"</span><span class="p">,</span> <span class="p">[</span><span class="s">"noddy.c"</span><span class="p">]),</span> <span class="n">Extension</span><span class="p">(</span><span class="s">"noddy2"</span><span class="p">,</span> <span class="p">[</span><span class="s">"noddy2.c"</span><span class="p">]),</span> <span class="p">])</span> </pre></div> </div> </div> <div class="section" id="providing-finer-control-over-data-attributes"> <h3>2.1.2. Providing finer control over data attributes<a class="headerlink" href="#providing-finer-control-over-data-attributes" title="Permalink to this headline">¶</a></h3> <p>In this section, we’ll provide finer control over how the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> attributes are set in the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> example. In the previous version of our module, the instance variables <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> could be set to non-string values or even deleted. We want to make sure that these attributes always contain strings.</p> <div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <Python.h></span> <span class="cp">#include "structmember.h"</span> <span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">last</span><span class="p">;</span> <span class="kt">int</span> <span class="n">number</span><span class="p">;</span> <span class="p">}</span> <span class="n">Noddy</span><span class="p">;</span> <span class="k">static</span> <span class="kt">void</span> <span class="nf">Noddy_dealloc</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">ob_type</span><span class="o">-></span><span class="n">tp_free</span><span class="p">((</span><span class="n">PyObject</span><span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">);</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_new</span><span class="p">(</span><span class="n">PyTypeObject</span> <span class="o">*</span><span class="n">type</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">;</span> <span class="n">self</span> <span class="o">=</span> <span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="p">)</span><span class="n">type</span><span class="o">-></span><span class="n">tp_alloc</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">number</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_init</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">last</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">tmp</span><span class="p">;</span> <span class="k">static</span> <span class="kt">char</span> <span class="o">*</span><span class="n">kwlist</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="s">"last"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">};</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyArg_ParseTupleAndKeywords</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">,</span> <span class="s">"|SSi"</span><span class="p">,</span> <span class="n">kwlist</span><span class="p">,</span> <span class="o">&</span><span class="n">first</span><span class="p">,</span> <span class="o">&</span><span class="n">last</span><span class="p">,</span> <span class="o">&</span><span class="n">self</span><span class="o">-></span><span class="n">number</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">first</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">first</span><span class="p">;</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">last</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">last</span><span class="p">;</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyMemberDef</span> <span class="n">Noddy_members</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"number"</span><span class="p">,</span> <span class="n">T_INT</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">number</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"noddy number"</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_getfirst</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">closure</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="k">return</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_setfirst</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">value</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">closure</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">value</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">"Cannot delete the first attribute"</span><span class="p">);</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyString_Check</span><span class="p">(</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">"The first attribute value must be a string"</span><span class="p">);</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">value</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">value</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_getlast</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">closure</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="k">return</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_setlast</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">value</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">closure</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">value</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">"Cannot delete the last attribute"</span><span class="p">);</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyString_Check</span><span class="p">(</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">"The last attribute value must be a string"</span><span class="p">);</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">value</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">value</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyGetSetDef</span> <span class="n">Noddy_getseters</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="p">(</span><span class="n">getter</span><span class="p">)</span><span class="n">Noddy_getfirst</span><span class="p">,</span> <span class="p">(</span><span class="n">setter</span><span class="p">)</span><span class="n">Noddy_setfirst</span><span class="p">,</span> <span class="s">"first name"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">},</span> <span class="p">{</span><span class="s">"last"</span><span class="p">,</span> <span class="p">(</span><span class="n">getter</span><span class="p">)</span><span class="n">Noddy_getlast</span><span class="p">,</span> <span class="p">(</span><span class="n">setter</span><span class="p">)</span><span class="n">Noddy_setlast</span><span class="p">,</span> <span class="s">"last name"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_name</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">format</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">*</span><span class="n">result</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">format</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">"%s %s"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">args</span> <span class="o">=</span> <span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"OO"</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">args</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">PyString_Format</span><span class="p">(</span><span class="n">format</span><span class="p">,</span> <span class="n">args</span><span class="p">);</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">args</span><span class="p">);</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">Noddy_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"name"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">Noddy_name</span><span class="p">,</span> <span class="n">METH_NOARGS</span><span class="p">,</span> <span class="s">"Return the name, combining the first and last name"</span> <span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyTypeObject</span> <span class="n">NoddyType</span> <span class="o">=</span> <span class="p">{</span> <span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*ob_size*/</span> <span class="s">"noddy.Noddy"</span><span class="p">,</span> <span class="cm">/*tp_name*/</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">),</span> <span class="cm">/*tp_basicsize*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_itemsize*/</span> <span class="p">(</span><span class="n">destructor</span><span class="p">)</span><span class="n">Noddy_dealloc</span><span class="p">,</span> <span class="cm">/*tp_dealloc*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_print*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_compare*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_repr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_number*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_sequence*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_mapping*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_hash */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_call*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_str*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_buffer*/</span> <span class="n">Py_TPFLAGS_DEFAULT</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_BASETYPE</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> <span class="s">"Noddy objects"</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_traverse */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_clear */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_richcompare */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_weaklistoffset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iter */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iternext */</span> <span class="n">Noddy_methods</span><span class="p">,</span> <span class="cm">/* tp_methods */</span> <span class="n">Noddy_members</span><span class="p">,</span> <span class="cm">/* tp_members */</span> <span class="n">Noddy_getseters</span><span class="p">,</span> <span class="cm">/* tp_getset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_base */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dict */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_get */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_set */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dictoffset */</span> <span class="p">(</span><span class="n">initproc</span><span class="p">)</span><span class="n">Noddy_init</span><span class="p">,</span> <span class="cm">/* tp_init */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_alloc */</span> <span class="n">Noddy_new</span><span class="p">,</span> <span class="cm">/* tp_new */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">module_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="cp">#ifndef PyMODINIT_FUNC </span><span class="cm">/* declarations for DLL import/export */</span><span class="cp"></span> <span class="cp">#define PyMODINIT_FUNC void</span> <span class="cp">#endif</span> <span class="n">PyMODINIT_FUNC</span> <span class="nf">initnoddy3</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span><span class="o">*</span> <span class="n">m</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">m</span> <span class="o">=</span> <span class="n">Py_InitModule3</span><span class="p">(</span><span class="s">"noddy3"</span><span class="p">,</span> <span class="n">module_methods</span><span class="p">,</span> <span class="s">"Example module that creates an extension type."</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">);</span> <span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Noddy"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>To provide greater control, over the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> attributes, we’ll use custom getter and setter functions. Here are the functions for getting and setting the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> attribute:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Noddy_getfirst</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">closure</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="k">return</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">int</span> <span class="n">Noddy_setfirst</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">value</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">closure</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">value</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">"Cannot delete the first attribute"</span><span class="p">);</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyString_Check</span><span class="p">(</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">"The first attribute value must be a string"</span><span class="p">);</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">value</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">value</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>The getter function is passed a <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> object and a “closure”, which is void pointer. In this case, the closure is ignored. (The closure supports an advanced usage in which definition data is passed to the getter and setter. This could, for example, be used to allow a single set of getter and setter functions that decide the attribute to get or set based on data in the closure.)</p> <p>The setter function is passed the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> object, the new value, and the closure. The new value may be <em>NULL</em>, in which case the attribute is being deleted. In our setter, we raise an error if the attribute is deleted or if the attribute value is not a string.</p> <p>We create an array of <tt class="xref c c-type docutils literal"><span class="pre">PyGetSetDef</span></tt> structures:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyGetSetDef</span> <span class="n">Noddy_getseters</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="p">(</span><span class="n">getter</span><span class="p">)</span><span class="n">Noddy_getfirst</span><span class="p">,</span> <span class="p">(</span><span class="n">setter</span><span class="p">)</span><span class="n">Noddy_setfirst</span><span class="p">,</span> <span class="s">"first name"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">},</span> <span class="p">{</span><span class="s">"last"</span><span class="p">,</span> <span class="p">(</span><span class="n">getter</span><span class="p">)</span><span class="n">Noddy_getlast</span><span class="p">,</span> <span class="p">(</span><span class="n">setter</span><span class="p">)</span><span class="n">Noddy_setlast</span><span class="p">,</span> <span class="s">"last name"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> </pre></div> </div> <p>and register it in the <tt class="xref py py-attr docutils literal"><span class="pre">tp_getset</span></tt> slot:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Noddy_getseters</span><span class="p">,</span> <span class="cm">/* tp_getset */</span> </pre></div> </div> <p>to register our attribute getters and setters.</p> <p>The last item in a <tt class="xref c c-type docutils literal"><span class="pre">PyGetSetDef</span></tt> structure is the closure mentioned above. In this case, we aren’t using the closure, so we just pass <em>NULL</em>.</p> <p>We also remove the member definitions for these attributes:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyMemberDef</span> <span class="n">Noddy_members</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"number"</span><span class="p">,</span> <span class="n">T_INT</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">number</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"noddy number"</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> </pre></div> </div> <p>We also need to update the <tt class="xref py py-attr docutils literal"><span class="pre">tp_init</span></tt> handler to only allow strings <a class="footnote-reference" href="#id9" id="id4">[3]</a> to be passed:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_init</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">last</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">tmp</span><span class="p">;</span> <span class="k">static</span> <span class="kt">char</span> <span class="o">*</span><span class="n">kwlist</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="s">"last"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">};</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyArg_ParseTupleAndKeywords</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">,</span> <span class="s">"|SSi"</span><span class="p">,</span> <span class="n">kwlist</span><span class="p">,</span> <span class="o">&</span><span class="n">first</span><span class="p">,</span> <span class="o">&</span><span class="n">last</span><span class="p">,</span> <span class="o">&</span><span class="n">self</span><span class="o">-></span><span class="n">number</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">first</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">first</span><span class="p">;</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">last</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">last</span><span class="p">;</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>With these changes, we can assure that the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> members are never <em>NULL</em> so we can remove checks for <em>NULL</em> values in almost all cases. This means that most of the <a class="reference internal" href="../c-api/refcounting.html#Py_XDECREF" title="Py_XDECREF"><tt class="xref c c-func docutils literal"><span class="pre">Py_XDECREF()</span></tt></a> calls can be converted to <a class="reference internal" href="../c-api/refcounting.html#Py_DECREF" title="Py_DECREF"><tt class="xref c c-func docutils literal"><span class="pre">Py_DECREF()</span></tt></a> calls. The only place we can’t change these calls is in the deallocator, where there is the possibility that the initialization of these members failed in the constructor.</p> <p>We also rename the module initialization function and module name in the initialization function, as we did before, and we add an extra definition to the <tt class="file docutils literal"><span class="pre">setup.py</span></tt> file.</p> </div> <div class="section" id="supporting-cyclic-garbage-collection"> <h3>2.1.3. Supporting cyclic garbage collection<a class="headerlink" href="#supporting-cyclic-garbage-collection" title="Permalink to this headline">¶</a></h3> <p>Python has a cyclic-garbage collector that can identify unneeded objects even when their reference counts are not zero. This can happen when objects are involved in cycles. For example, consider:</p> <div class="highlight-c"><div class="highlight"><pre><span class="o">>>></span> <span class="n">l</span> <span class="o">=</span> <span class="p">[]</span> <span class="o">>>></span> <span class="n">l</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">)</span> <span class="o">>>></span> <span class="n">del</span> <span class="n">l</span> </pre></div> </div> <p>In this example, we create a list that contains itself. When we delete it, it still has a reference from itself. Its reference count doesn’t drop to zero. Fortunately, Python’s cyclic-garbage collector will eventually figure out that the list is garbage and free it.</p> <p>In the second version of the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> example, we allowed any kind of object to be stored in the <tt class="xref py py-attr docutils literal"><span class="pre">first</span></tt> or <tt class="xref py py-attr docutils literal"><span class="pre">last</span></tt> attributes. <a class="footnote-reference" href="#id10" id="id5">[4]</a> This means that <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> objects can participate in cycles:</p> <div class="highlight-c"><div class="highlight"><pre><span class="o">>>></span> <span class="n">import</span> <span class="n">noddy2</span> <span class="o">>>></span> <span class="n">n</span> <span class="o">=</span> <span class="n">noddy2</span><span class="p">.</span><span class="n">Noddy</span><span class="p">()</span> <span class="o">>>></span> <span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">>>></span> <span class="n">n</span><span class="p">.</span><span class="n">first</span> <span class="o">=</span> <span class="n">l</span> </pre></div> </div> <p>This is pretty silly, but it gives us an excuse to add support for the cyclic-garbage collector to the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> example. To support cyclic garbage collection, types need to fill two slots and set a class flag that enables these slots:</p> <div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <Python.h></span> <span class="cp">#include "structmember.h"</span> <span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">last</span><span class="p">;</span> <span class="kt">int</span> <span class="n">number</span><span class="p">;</span> <span class="p">}</span> <span class="n">Noddy</span><span class="p">;</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_traverse</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">visitproc</span> <span class="n">visit</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">arg</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">vret</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">vret</span> <span class="o">=</span> <span class="n">visit</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">,</span> <span class="n">arg</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">vret</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="n">vret</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">)</span> <span class="p">{</span> <span class="n">vret</span> <span class="o">=</span> <span class="n">visit</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">,</span> <span class="n">arg</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">vret</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="n">vret</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_clear</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tmp</span><span class="p">;</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">void</span> <span class="nf">Noddy_dealloc</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">Noddy_clear</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">ob_type</span><span class="o">-></span><span class="n">tp_free</span><span class="p">((</span><span class="n">PyObject</span><span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">);</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_new</span><span class="p">(</span><span class="n">PyTypeObject</span> <span class="o">*</span><span class="n">type</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">;</span> <span class="n">self</span> <span class="o">=</span> <span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="p">)</span><span class="n">type</span><span class="o">-></span><span class="n">tp_alloc</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">""</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">self</span><span class="o">-></span><span class="n">number</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_init</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">first</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">last</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="o">*</span><span class="n">tmp</span><span class="p">;</span> <span class="k">static</span> <span class="kt">char</span> <span class="o">*</span><span class="n">kwlist</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="s">"last"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">};</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyArg_ParseTupleAndKeywords</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">,</span> <span class="s">"|OOi"</span><span class="p">,</span> <span class="n">kwlist</span><span class="p">,</span> <span class="o">&</span><span class="n">first</span><span class="p">,</span> <span class="o">&</span><span class="n">last</span><span class="p">,</span> <span class="o">&</span><span class="n">self</span><span class="o">-></span><span class="n">number</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">first</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="n">first</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">last</span><span class="p">)</span> <span class="p">{</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="n">last</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="n">last</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyMemberDef</span> <span class="n">Noddy_members</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"first"</span><span class="p">,</span> <span class="n">T_OBJECT_EX</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">first</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"first name"</span><span class="p">},</span> <span class="p">{</span><span class="s">"last"</span><span class="p">,</span> <span class="n">T_OBJECT_EX</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">last</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"last name"</span><span class="p">},</span> <span class="p">{</span><span class="s">"number"</span><span class="p">,</span> <span class="n">T_INT</span><span class="p">,</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">,</span> <span class="n">number</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"noddy number"</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Noddy_name</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">format</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">*</span><span class="n">result</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">format</span> <span class="o">=</span> <span class="n">PyString_FromString</span><span class="p">(</span><span class="s">"%s %s"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">format</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_AttributeError</span><span class="p">,</span> <span class="s">"first"</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_AttributeError</span><span class="p">,</span> <span class="s">"last"</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">args</span> <span class="o">=</span> <span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"OO"</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">,</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">args</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">PyString_Format</span><span class="p">(</span><span class="n">format</span><span class="p">,</span> <span class="n">args</span><span class="p">);</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">args</span><span class="p">);</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">Noddy_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"name"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">Noddy_name</span><span class="p">,</span> <span class="n">METH_NOARGS</span><span class="p">,</span> <span class="s">"Return the name, combining the first and last name"</span> <span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyTypeObject</span> <span class="n">NoddyType</span> <span class="o">=</span> <span class="p">{</span> <span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*ob_size*/</span> <span class="s">"noddy.Noddy"</span><span class="p">,</span> <span class="cm">/*tp_name*/</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Noddy</span><span class="p">),</span> <span class="cm">/*tp_basicsize*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_itemsize*/</span> <span class="p">(</span><span class="n">destructor</span><span class="p">)</span><span class="n">Noddy_dealloc</span><span class="p">,</span> <span class="cm">/*tp_dealloc*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_print*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_compare*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_repr*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_number*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_sequence*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_mapping*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_hash */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_call*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_str*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_getattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_setattro*/</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/*tp_as_buffer*/</span> <span class="n">Py_TPFLAGS_DEFAULT</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_BASETYPE</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_HAVE_GC</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> <span class="s">"Noddy objects"</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> <span class="p">(</span><span class="n">traverseproc</span><span class="p">)</span><span class="n">Noddy_traverse</span><span class="p">,</span> <span class="cm">/* tp_traverse */</span> <span class="p">(</span><span class="n">inquiry</span><span class="p">)</span><span class="n">Noddy_clear</span><span class="p">,</span> <span class="cm">/* tp_clear */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_richcompare */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_weaklistoffset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iter */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iternext */</span> <span class="n">Noddy_methods</span><span class="p">,</span> <span class="cm">/* tp_methods */</span> <span class="n">Noddy_members</span><span class="p">,</span> <span class="cm">/* tp_members */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_getset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_base */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dict */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_get */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_set */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dictoffset */</span> <span class="p">(</span><span class="n">initproc</span><span class="p">)</span><span class="n">Noddy_init</span><span class="p">,</span> <span class="cm">/* tp_init */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_alloc */</span> <span class="n">Noddy_new</span><span class="p">,</span> <span class="cm">/* tp_new */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">module_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* Sentinel */</span> <span class="p">};</span> <span class="cp">#ifndef PyMODINIT_FUNC </span><span class="cm">/* declarations for DLL import/export */</span><span class="cp"></span> <span class="cp">#define PyMODINIT_FUNC void</span> <span class="cp">#endif</span> <span class="n">PyMODINIT_FUNC</span> <span class="nf">initnoddy4</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span><span class="o">*</span> <span class="n">m</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">m</span> <span class="o">=</span> <span class="n">Py_InitModule3</span><span class="p">(</span><span class="s">"noddy4"</span><span class="p">,</span> <span class="n">module_methods</span><span class="p">,</span> <span class="s">"Example module that creates an extension type."</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">);</span> <span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Noddy"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="o">&</span><span class="n">NoddyType</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>The traversal method provides access to subobjects that could participate in cycles:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_traverse</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">visitproc</span> <span class="n">visit</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">arg</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">vret</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">)</span> <span class="p">{</span> <span class="n">vret</span> <span class="o">=</span> <span class="n">visit</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">,</span> <span class="n">arg</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">vret</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="n">vret</span><span class="p">;</span> <span class="p">}</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">)</span> <span class="p">{</span> <span class="n">vret</span> <span class="o">=</span> <span class="n">visit</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">,</span> <span class="n">arg</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">vret</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="n">vret</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>For each subobject that can participate in cycles, we need to call the <tt class="xref c c-func docutils literal"><span class="pre">visit()</span></tt> function, which is passed to the traversal method. The <tt class="xref c c-func docutils literal"><span class="pre">visit()</span></tt> function takes as arguments the subobject and the extra argument <em>arg</em> passed to the traversal method. It returns an integer value that must be returned if it is non-zero.</p> <p>Python 2.4 and higher provide a <a class="reference internal" href="../c-api/gcsupport.html#Py_VISIT" title="Py_VISIT"><tt class="xref c c-func docutils literal"><span class="pre">Py_VISIT()</span></tt></a> macro that automates calling visit functions. With <a class="reference internal" href="../c-api/gcsupport.html#Py_VISIT" title="Py_VISIT"><tt class="xref c c-func docutils literal"><span class="pre">Py_VISIT()</span></tt></a>, <tt class="xref c c-func docutils literal"><span class="pre">Noddy_traverse()</span></tt> can be simplified:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_traverse</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">visitproc</span> <span class="n">visit</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">arg</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_VISIT</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_VISIT</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Note that the <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> implementation must name its arguments exactly <em>visit</em> and <em>arg</em> in order to use <a class="reference internal" href="../c-api/gcsupport.html#Py_VISIT" title="Py_VISIT"><tt class="xref c c-func docutils literal"><span class="pre">Py_VISIT()</span></tt></a>. This is to encourage uniformity across these boring implementations.</p> </div> <p>We also need to provide a method for clearing any subobjects that can participate in cycles. We implement the method and reimplement the deallocator to use it:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_clear</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tmp</span><span class="p">;</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">;</span> <span class="n">self</span><span class="o">-></span><span class="n">first</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="n">tmp</span> <span class="o">=</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">;</span> <span class="n">self</span><span class="o">-></span><span class="n">last</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">tmp</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="kt">void</span> <span class="nf">Noddy_dealloc</span><span class="p">(</span><span class="n">Noddy</span><span class="o">*</span> <span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">Noddy_clear</span><span class="p">(</span><span class="n">self</span><span class="p">);</span> <span class="n">self</span><span class="o">-></span><span class="n">ob_type</span><span class="o">-></span><span class="n">tp_free</span><span class="p">((</span><span class="n">PyObject</span><span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>Notice the use of a temporary variable in <tt class="xref c c-func docutils literal"><span class="pre">Noddy_clear()</span></tt>. We use the temporary variable so that we can set each member to <em>NULL</em> before decrementing its reference count. We do this because, as was discussed earlier, if the reference count drops to zero, we might cause code to run that calls back into the object. In addition, because we now support garbage collection, we also have to worry about code being run that triggers garbage collection. If garbage collection is run, our <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> handler could get called. We can’t take a chance of having <tt class="xref c c-func docutils literal"><span class="pre">Noddy_traverse()</span></tt> called when a member’s reference count has dropped to zero and its value hasn’t been set to <em>NULL</em>.</p> <p>Python 2.4 and higher provide a <a class="reference internal" href="../c-api/refcounting.html#Py_CLEAR" title="Py_CLEAR"><tt class="xref c c-func docutils literal"><span class="pre">Py_CLEAR()</span></tt></a> that automates the careful decrementing of reference counts. With <a class="reference internal" href="../c-api/refcounting.html#Py_CLEAR" title="Py_CLEAR"><tt class="xref c c-func docutils literal"><span class="pre">Py_CLEAR()</span></tt></a>, the <tt class="xref c c-func docutils literal"><span class="pre">Noddy_clear()</span></tt> function can be simplified:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">Noddy_clear</span><span class="p">(</span><span class="n">Noddy</span> <span class="o">*</span><span class="n">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">Py_CLEAR</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">first</span><span class="p">);</span> <span class="n">Py_CLEAR</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">last</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>Finally, we add the <a class="reference internal" href="../c-api/typeobj.html#Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><tt class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></tt></a> flag to the class flags:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">Py_TPFLAGS_DEFAULT</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_BASETYPE</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_HAVE_GC</span><span class="p">,</span> <span class="cm">/*tp_flags*/</span> </pre></div> </div> <p>That’s pretty much it. If we had written custom <tt class="xref py py-attr docutils literal"><span class="pre">tp_alloc</span></tt> or <tt class="xref py py-attr docutils literal"><span class="pre">tp_free</span></tt> slots, we’d need to modify them for cyclic-garbage collection. Most extensions will use the versions automatically provided.</p> </div> <div class="section" id="subclassing-other-types"> <h3>2.1.4. Subclassing other types<a class="headerlink" href="#subclassing-other-types" title="Permalink to this headline">¶</a></h3> <p>It is possible to create new extension types that are derived from existing types. It is easiest to inherit from the built in types, since an extension can easily use the <tt class="xref py py-class docutils literal"><span class="pre">PyTypeObject</span></tt> it needs. It can be difficult to share these <tt class="xref py py-class docutils literal"><span class="pre">PyTypeObject</span></tt> structures between extension modules.</p> <p>In this example we will create a <tt class="xref py py-class docutils literal"><span class="pre">Shoddy</span></tt> type that inherits from the built-in <a class="reference internal" href="../library/functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> type. The new type will be completely compatible with regular lists, but will have an additional <tt class="xref py py-meth docutils literal"><span class="pre">increment()</span></tt> method that increases an internal counter.</p> <div class="highlight-c"><div class="highlight"><pre><span class="o">>>></span> <span class="n">import</span> <span class="n">shoddy</span> <span class="o">>>></span> <span class="n">s</span> <span class="o">=</span> <span class="n">shoddy</span><span class="p">.</span><span class="n">Shoddy</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span> <span class="o">>>></span> <span class="n">s</span><span class="p">.</span><span class="n">extend</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">>>></span> <span class="n">print</span> <span class="n">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="mi">6</span> <span class="o">>>></span> <span class="n">print</span> <span class="n">s</span><span class="p">.</span><span class="n">increment</span><span class="p">()</span> <span class="mi">1</span> <span class="o">>>></span> <span class="n">print</span> <span class="n">s</span><span class="p">.</span><span class="n">increment</span><span class="p">()</span> <span class="mi">2</span> </pre></div> </div> <div class="highlight-c"><div class="highlight"><pre><span class="cp">#include <Python.h></span> <span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyListObject</span> <span class="n">list</span><span class="p">;</span> <span class="kt">int</span> <span class="n">state</span><span class="p">;</span> <span class="p">}</span> <span class="n">Shoddy</span><span class="p">;</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">Shoddy_increment</span><span class="p">(</span><span class="n">Shoddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">unused</span><span class="p">)</span> <span class="p">{</span> <span class="n">self</span><span class="o">-></span><span class="n">state</span><span class="o">++</span><span class="p">;</span> <span class="k">return</span> <span class="n">PyInt_FromLong</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">state</span><span class="p">);</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">Shoddy_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"increment"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">Shoddy_increment</span><span class="p">,</span> <span class="n">METH_NOARGS</span><span class="p">,</span> <span class="n">PyDoc_STR</span><span class="p">(</span><span class="s">"increment state counter"</span><span class="p">)},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">},</span> <span class="p">};</span> <span class="k">static</span> <span class="kt">int</span> <span class="nf">Shoddy_init</span><span class="p">(</span><span class="n">Shoddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyList_Type</span><span class="p">.</span><span class="n">tp_init</span><span class="p">((</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="n">self</span><span class="o">-></span><span class="n">state</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">static</span> <span class="n">PyTypeObject</span> <span class="n">ShoddyType</span> <span class="o">=</span> <span class="p">{</span> <span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* ob_size */</span> <span class="s">"shoddy.Shoddy"</span><span class="p">,</span> <span class="cm">/* tp_name */</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Shoddy</span><span class="p">),</span> <span class="cm">/* tp_basicsize */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_itemsize */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dealloc */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_print */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_getattr */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_setattr */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_compare */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_repr */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_as_number */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_as_sequence */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_as_mapping */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_hash */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_call */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_str */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_getattro */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_setattro */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_as_buffer */</span> <span class="n">Py_TPFLAGS_DEFAULT</span> <span class="o">|</span> <span class="n">Py_TPFLAGS_BASETYPE</span><span class="p">,</span> <span class="cm">/* tp_flags */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_traverse */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_clear */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_richcompare */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_weaklistoffset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iter */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_iternext */</span> <span class="n">Shoddy_methods</span><span class="p">,</span> <span class="cm">/* tp_methods */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_members */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_getset */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_base */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dict */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_get */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_descr_set */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_dictoffset */</span> <span class="p">(</span><span class="n">initproc</span><span class="p">)</span><span class="n">Shoddy_init</span><span class="p">,</span> <span class="cm">/* tp_init */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_alloc */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_new */</span> <span class="p">};</span> <span class="n">PyMODINIT_FUNC</span> <span class="nf">initshoddy</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">m</span><span class="p">;</span> <span class="n">ShoddyType</span><span class="p">.</span><span class="n">tp_base</span> <span class="o">=</span> <span class="o">&</span><span class="n">PyList_Type</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">ShoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">m</span> <span class="o">=</span> <span class="n">Py_InitModule3</span><span class="p">(</span><span class="s">"shoddy"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="s">"Shoddy module"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="o">&</span><span class="n">ShoddyType</span><span class="p">);</span> <span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Shoddy"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span> <span class="o">&</span><span class="n">ShoddyType</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>As you can see, the source code closely resembles the <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> examples in previous sections. We will break down the main differences between them.</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyListObject</span> <span class="n">list</span><span class="p">;</span> <span class="kt">int</span> <span class="n">state</span><span class="p">;</span> <span class="p">}</span> <span class="n">Shoddy</span><span class="p">;</span> </pre></div> </div> <p>The primary difference for derived type objects is that the base type’s object structure must be the first value. The base type will already include the <a class="reference internal" href="../c-api/structures.html#PyObject_HEAD" title="PyObject_HEAD"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_HEAD()</span></tt></a> at the beginning of its structure.</p> <p>When a Python object is a <tt class="xref py py-class docutils literal"><span class="pre">Shoddy</span></tt> instance, its <em>PyObject*</em> pointer can be safely cast to both <em>PyListObject*</em> and <em>Shoddy*</em>.</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">Shoddy_init</span><span class="p">(</span><span class="n">Shoddy</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">kwds</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyList_Type</span><span class="p">.</span><span class="n">tp_init</span><span class="p">((</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwds</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="n">self</span><span class="o">-></span><span class="n">state</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>In the <tt class="xref py py-attr docutils literal"><span class="pre">__init__</span></tt> method for our type, we can see how to call through to the <tt class="xref py py-attr docutils literal"><span class="pre">__init__</span></tt> method of the base type.</p> <p>This pattern is important when writing a type with custom <a class="reference internal" href="../library/new.html#module-new" title="new: Interface to the creation of runtime implementation objects. (deprecated)"><tt class="xref py py-attr docutils literal"><span class="pre">new</span></tt></a> and <tt class="xref py py-attr docutils literal"><span class="pre">dealloc</span></tt> methods. The <a class="reference internal" href="../library/new.html#module-new" title="new: Interface to the creation of runtime implementation objects. (deprecated)"><tt class="xref py py-attr docutils literal"><span class="pre">new</span></tt></a> method should not actually create the memory for the object with <tt class="xref py py-attr docutils literal"><span class="pre">tp_alloc</span></tt>, that will be handled by the base class when calling its <tt class="xref py py-attr docutils literal"><span class="pre">tp_new</span></tt>.</p> <p>When filling out the <a class="reference internal" href="../c-api/type.html#PyTypeObject" title="PyTypeObject"><tt class="xref c c-func docutils literal"><span class="pre">PyTypeObject()</span></tt></a> for the <tt class="xref py py-class docutils literal"><span class="pre">Shoddy</span></tt> type, you see a slot for <tt class="xref c c-func docutils literal"><span class="pre">tp_base()</span></tt>. Due to cross platform compiler issues, you can’t fill that field directly with the <a class="reference internal" href="../c-api/list.html#PyList_Type" title="PyList_Type"><tt class="xref c c-func docutils literal"><span class="pre">PyList_Type()</span></tt></a>; it can be done later in the module’s <tt class="xref c c-func docutils literal"><span class="pre">init()</span></tt> function.</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">PyMODINIT_FUNC</span> <span class="nf">initshoddy</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">m</span><span class="p">;</span> <span class="n">ShoddyType</span><span class="p">.</span><span class="n">tp_base</span> <span class="o">=</span> <span class="o">&</span><span class="n">PyList_Type</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyType_Ready</span><span class="p">(</span><span class="o">&</span><span class="n">ShoddyType</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">m</span> <span class="o">=</span> <span class="n">Py_InitModule3</span><span class="p">(</span><span class="s">"shoddy"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="s">"Shoddy module"</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span> <span class="n">Py_INCREF</span><span class="p">(</span><span class="o">&</span><span class="n">ShoddyType</span><span class="p">);</span> <span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Shoddy"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span> <span class="o">&</span><span class="n">ShoddyType</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>Before calling <a class="reference internal" href="../c-api/type.html#PyType_Ready" title="PyType_Ready"><tt class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></tt></a>, the type structure must have the <tt class="xref py py-attr docutils literal"><span class="pre">tp_base</span></tt> slot filled in. When we are deriving a new type, it is not necessary to fill out the <tt class="xref py py-attr docutils literal"><span class="pre">tp_alloc</span></tt> slot with <a class="reference internal" href="../c-api/type.html#PyType_GenericNew" title="PyType_GenericNew"><tt class="xref c c-func docutils literal"><span class="pre">PyType_GenericNew()</span></tt></a> – the allocate function from the base type will be inherited.</p> <p>After that, calling <a class="reference internal" href="../c-api/type.html#PyType_Ready" title="PyType_Ready"><tt class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></tt></a> and adding the type object to the module is the same as with the basic <tt class="xref py py-class docutils literal"><span class="pre">Noddy</span></tt> examples.</p> </div> </div> <div class="section" id="type-methods"> <span id="dnt-type-methods"></span><h2>2.2. Type Methods<a class="headerlink" href="#type-methods" title="Permalink to this headline">¶</a></h2> <p>This section aims to give a quick fly-by on the various type methods you can implement and what they do.</p> <p>Here is the definition of <a class="reference internal" href="../c-api/type.html#PyTypeObject" title="PyTypeObject"><tt class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></tt></a>, with some fields only used in debug builds omitted:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="n">_typeobject</span> <span class="p">{</span> <span class="n">PyObject_VAR_HEAD</span> <span class="kt">char</span> <span class="o">*</span><span class="n">tp_name</span><span class="p">;</span> <span class="cm">/* For printing, in format "<module>.<name>" */</span> <span class="kt">int</span> <span class="n">tp_basicsize</span><span class="p">,</span> <span class="n">tp_itemsize</span><span class="p">;</span> <span class="cm">/* For allocation */</span> <span class="cm">/* Methods to implement standard operations */</span> <span class="n">destructor</span> <span class="n">tp_dealloc</span><span class="p">;</span> <span class="n">printfunc</span> <span class="n">tp_print</span><span class="p">;</span> <span class="n">getattrfunc</span> <span class="n">tp_getattr</span><span class="p">;</span> <span class="n">setattrfunc</span> <span class="n">tp_setattr</span><span class="p">;</span> <span class="n">cmpfunc</span> <span class="n">tp_compare</span><span class="p">;</span> <span class="n">reprfunc</span> <span class="n">tp_repr</span><span class="p">;</span> <span class="cm">/* Method suites for standard classes */</span> <span class="n">PyNumberMethods</span> <span class="o">*</span><span class="n">tp_as_number</span><span class="p">;</span> <span class="n">PySequenceMethods</span> <span class="o">*</span><span class="n">tp_as_sequence</span><span class="p">;</span> <span class="n">PyMappingMethods</span> <span class="o">*</span><span class="n">tp_as_mapping</span><span class="p">;</span> <span class="cm">/* More standard operations (here for binary compatibility) */</span> <span class="n">hashfunc</span> <span class="n">tp_hash</span><span class="p">;</span> <span class="n">ternaryfunc</span> <span class="n">tp_call</span><span class="p">;</span> <span class="n">reprfunc</span> <span class="n">tp_str</span><span class="p">;</span> <span class="n">getattrofunc</span> <span class="n">tp_getattro</span><span class="p">;</span> <span class="n">setattrofunc</span> <span class="n">tp_setattro</span><span class="p">;</span> <span class="cm">/* Functions to access object as input/output buffer */</span> <span class="n">PyBufferProcs</span> <span class="o">*</span><span class="n">tp_as_buffer</span><span class="p">;</span> <span class="cm">/* Flags to define presence of optional/expanded features */</span> <span class="kt">long</span> <span class="n">tp_flags</span><span class="p">;</span> <span class="kt">char</span> <span class="o">*</span><span class="n">tp_doc</span><span class="p">;</span> <span class="cm">/* Documentation string */</span> <span class="cm">/* Assigned meaning in release 2.0 */</span> <span class="cm">/* call function for all accessible objects */</span> <span class="n">traverseproc</span> <span class="n">tp_traverse</span><span class="p">;</span> <span class="cm">/* delete references to contained objects */</span> <span class="n">inquiry</span> <span class="n">tp_clear</span><span class="p">;</span> <span class="cm">/* Assigned meaning in release 2.1 */</span> <span class="cm">/* rich comparisons */</span> <span class="n">richcmpfunc</span> <span class="n">tp_richcompare</span><span class="p">;</span> <span class="cm">/* weak reference enabler */</span> <span class="kt">long</span> <span class="n">tp_weaklistoffset</span><span class="p">;</span> <span class="cm">/* Added in release 2.2 */</span> <span class="cm">/* Iterators */</span> <span class="n">getiterfunc</span> <span class="n">tp_iter</span><span class="p">;</span> <span class="n">iternextfunc</span> <span class="n">tp_iternext</span><span class="p">;</span> <span class="cm">/* Attribute descriptor and subclassing stuff */</span> <span class="k">struct</span> <span class="n">PyMethodDef</span> <span class="o">*</span><span class="n">tp_methods</span><span class="p">;</span> <span class="k">struct</span> <span class="n">PyMemberDef</span> <span class="o">*</span><span class="n">tp_members</span><span class="p">;</span> <span class="k">struct</span> <span class="n">PyGetSetDef</span> <span class="o">*</span><span class="n">tp_getset</span><span class="p">;</span> <span class="k">struct</span> <span class="n">_typeobject</span> <span class="o">*</span><span class="n">tp_base</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_dict</span><span class="p">;</span> <span class="n">descrgetfunc</span> <span class="n">tp_descr_get</span><span class="p">;</span> <span class="n">descrsetfunc</span> <span class="n">tp_descr_set</span><span class="p">;</span> <span class="kt">long</span> <span class="n">tp_dictoffset</span><span class="p">;</span> <span class="n">initproc</span> <span class="n">tp_init</span><span class="p">;</span> <span class="n">allocfunc</span> <span class="n">tp_alloc</span><span class="p">;</span> <span class="n">newfunc</span> <span class="n">tp_new</span><span class="p">;</span> <span class="n">freefunc</span> <span class="n">tp_free</span><span class="p">;</span> <span class="cm">/* Low-level free-memory routine */</span> <span class="n">inquiry</span> <span class="n">tp_is_gc</span><span class="p">;</span> <span class="cm">/* For PyObject_IS_GC */</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_bases</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_mro</span><span class="p">;</span> <span class="cm">/* method resolution order */</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_cache</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_subclasses</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">tp_weaklist</span><span class="p">;</span> <span class="p">}</span> <span class="n">PyTypeObject</span><span class="p">;</span> </pre></div> </div> <p>Now that’s a <em>lot</em> of methods. Don’t worry too much though - if you have a type you want to define, the chances are very good that you will only implement a handful of these.</p> <p>As you probably expect by now, we’re going to go over this and give more information about the various handlers. We won’t go in the order they are defined in the structure, because there is a lot of historical baggage that impacts the ordering of the fields; be sure your type initialization keeps the fields in the right order! It’s often easiest to find an example that includes all the fields you need (even if they’re initialized to <tt class="docutils literal"><span class="pre">0</span></tt>) and then change the values to suit your new type.</p> <div class="highlight-c"><div class="highlight"><pre><span class="kt">char</span> <span class="o">*</span><span class="n">tp_name</span><span class="p">;</span> <span class="cm">/* For printing */</span> </pre></div> </div> <p>The name of the type - as mentioned in the last section, this will appear in various places, almost entirely for diagnostic purposes. Try to choose something that will be helpful in such a situation!</p> <div class="highlight-c"><div class="highlight"><pre><span class="kt">int</span> <span class="n">tp_basicsize</span><span class="p">,</span> <span class="n">tp_itemsize</span><span class="p">;</span> <span class="cm">/* For allocation */</span> </pre></div> </div> <p>These fields tell the runtime how much memory to allocate when new objects of this type are created. Python has some built-in support for variable length structures (think: strings, lists) which is where the <tt class="xref py py-attr docutils literal"><span class="pre">tp_itemsize</span></tt> field comes in. This will be dealt with later.</p> <div class="highlight-c"><div class="highlight"><pre><span class="kt">char</span> <span class="o">*</span><span class="n">tp_doc</span><span class="p">;</span> </pre></div> </div> <p>Here you can put a string (or its address) that you want returned when the Python script references <tt class="docutils literal"><span class="pre">obj.__doc__</span></tt> to retrieve the doc string.</p> <p>Now we come to the basic type methods—the ones most extension types will implement.</p> <div class="section" id="finalization-and-de-allocation"> <h3>2.2.1. Finalization and De-allocation<a class="headerlink" href="#finalization-and-de-allocation" title="Permalink to this headline">¶</a></h3> <div class="highlight-c" id="index-0"><div class="highlight"><pre><span class="n">destructor</span> <span class="n">tp_dealloc</span><span class="p">;</span> </pre></div> </div> <p>This function is called when the reference count of the instance of your type is reduced to zero and the Python interpreter wants to reclaim it. If your type has memory to free or other clean-up to perform, put it here. The object itself needs to be freed here as well. Here is an example of this function:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">newdatatype_dealloc</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span> <span class="n">obj</span><span class="p">)</span> <span class="p">{</span> <span class="n">free</span><span class="p">(</span><span class="n">obj</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="p">);</span> <span class="n">obj</span><span class="o">-></span><span class="n">ob_type</span><span class="o">-></span><span class="n">tp_free</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p id="index-1">One important requirement of the deallocator function is that it leaves any pending exceptions alone. This is important since deallocators are frequently called as the interpreter unwinds the Python stack; when the stack is unwound due to an exception (rather than normal returns), nothing is done to protect the deallocators from seeing that an exception has already been set. Any actions which a deallocator performs which may cause additional Python code to be executed may detect that an exception has been set. This can lead to misleading errors from the interpreter. The proper way to protect against this is to save a pending exception before performing the unsafe action, and restoring it when done. This can be done using the <a class="reference internal" href="../c-api/exceptions.html#PyErr_Fetch" title="PyErr_Fetch"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Fetch()</span></tt></a> and <a class="reference internal" href="../c-api/exceptions.html#PyErr_Restore" title="PyErr_Restore"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Restore()</span></tt></a> functions:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">my_dealloc</span><span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="n">obj</span><span class="p">)</span> <span class="p">{</span> <span class="n">MyObject</span> <span class="o">*</span><span class="n">self</span> <span class="o">=</span> <span class="p">(</span><span class="n">MyObject</span> <span class="o">*</span><span class="p">)</span> <span class="n">obj</span><span class="p">;</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">cbresult</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">my_callback</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">err_type</span><span class="p">,</span> <span class="o">*</span><span class="n">err_value</span><span class="p">,</span> <span class="o">*</span><span class="n">err_traceback</span><span class="p">;</span> <span class="kt">int</span> <span class="n">have_error</span> <span class="o">=</span> <span class="n">PyErr_Occurred</span><span class="p">()</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="mi">0</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">have_error</span><span class="p">)</span> <span class="n">PyErr_Fetch</span><span class="p">(</span><span class="o">&</span><span class="n">err_type</span><span class="p">,</span> <span class="o">&</span><span class="n">err_value</span><span class="p">,</span> <span class="o">&</span><span class="n">err_traceback</span><span class="p">);</span> <span class="n">cbresult</span> <span class="o">=</span> <span class="n">PyObject_CallObject</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">my_callback</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">cbresult</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="n">PyErr_WriteUnraisable</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">my_callback</span><span class="p">);</span> <span class="k">else</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">cbresult</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">have_error</span><span class="p">)</span> <span class="n">PyErr_Restore</span><span class="p">(</span><span class="n">err_type</span><span class="p">,</span> <span class="n">err_value</span><span class="p">,</span> <span class="n">err_traceback</span><span class="p">);</span> <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">my_callback</span><span class="p">);</span> <span class="p">}</span> <span class="n">obj</span><span class="o">-></span><span class="n">ob_type</span><span class="o">-></span><span class="n">tp_free</span><span class="p">((</span><span class="n">PyObject</span><span class="o">*</span><span class="p">)</span><span class="n">self</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> </div> <div class="section" id="object-presentation"> <h3>2.2.2. Object Presentation<a class="headerlink" href="#object-presentation" title="Permalink to this headline">¶</a></h3> <p id="index-2">In Python, there are three ways to generate a textual representation of an object: the <a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> function (or equivalent back-tick syntax), the <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> function, and the <a class="reference internal" href="../reference/simple_stmts.html#print"><tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt></a> statement. For most objects, the <a class="reference internal" href="../reference/simple_stmts.html#print"><tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt></a> statement is equivalent to the <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> function, but it is possible to special-case printing to a <tt class="xref c c-type docutils literal"><span class="pre">FILE*</span></tt> if necessary; this should only be done if efficiency is identified as a problem and profiling suggests that creating a temporary string object to be written to a file is too expensive.</p> <p>These handlers are all optional, and most types at most need to implement the <tt class="xref py py-attr docutils literal"><span class="pre">tp_str</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> handlers.</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">reprfunc</span> <span class="n">tp_repr</span><span class="p">;</span> <span class="n">reprfunc</span> <span class="n">tp_str</span><span class="p">;</span> <span class="n">printfunc</span> <span class="n">tp_print</span><span class="p">;</span> </pre></div> </div> <p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> handler should return a string object containing a representation of the instance for which it is called. Here is a simple example:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">newdatatype_repr</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span> <span class="n">obj</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">PyString_FromFormat</span><span class="p">(</span><span class="s">"Repr-ified_newdatatype{{size:\%d}}"</span><span class="p">,</span> <span class="n">obj</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>If no <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> handler is specified, the interpreter will supply a representation that uses the type’s <tt class="xref py py-attr docutils literal"><span class="pre">tp_name</span></tt> and a uniquely-identifying value for the object.</p> <p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_str</span></tt> handler is to <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> what the <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> handler described above is to <a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a>; that is, it is called when Python code calls <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> on an instance of your object. Its implementation is very similar to the <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> function, but the resulting string is intended for human consumption. If <tt class="xref py py-attr docutils literal"><span class="pre">tp_str</span></tt> is not specified, the <tt class="xref py py-attr docutils literal"><span class="pre">tp_repr</span></tt> handler is used instead.</p> <p>Here is a simple example:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">newdatatype_str</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span> <span class="n">obj</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">PyString_FromFormat</span><span class="p">(</span><span class="s">"Stringified_newdatatype{{size:\%d}}"</span><span class="p">,</span> <span class="n">obj</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>The print function will be called whenever Python needs to “print” an instance of the type. For example, if ‘node’ is an instance of type TreeNode, then the print function is called when Python code calls:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">print</span> <span class="n">node</span> </pre></div> </div> <p>There is a flags argument and one flag, <tt class="xref py py-const docutils literal"><span class="pre">Py_PRINT_RAW</span></tt>, and it suggests that you print without string quotes and possibly without interpreting escape sequences.</p> <p>The print function receives a file object as an argument. You will likely want to write to that file object.</p> <p>Here is a sample print function:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">newdatatype_print</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span><span class="p">,</span> <span class="kt">int</span> <span class="n">flags</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">flags</span> <span class="o">&</span> <span class="n">Py_PRINT_RAW</span><span class="p">)</span> <span class="p">{</span> <span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"<{newdatatype object--size: %d}>"</span><span class="p">,</span> <span class="n">obj</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"</span><span class="se">\"</span><span class="s"><{newdatatype object--size: %d}></span><span class="se">\"</span><span class="s">"</span><span class="p">,</span> <span class="n">obj</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> </div> <div class="section" id="attribute-management"> <h3>2.2.3. Attribute Management<a class="headerlink" href="#attribute-management" title="Permalink to this headline">¶</a></h3> <p>For every object which can support attributes, the corresponding type must provide the functions that control how the attributes are resolved. There needs to be a function which can retrieve attributes (if any are defined), and another to set attributes (if setting attributes is allowed). Removing an attribute is a special case, for which the new value passed to the handler is <em>NULL</em>.</p> <p>Python supports two pairs of attribute handlers; a type that supports attributes only needs to implement the functions for one pair. The difference is that one pair takes the name of the attribute as a <tt class="xref c c-type docutils literal"><span class="pre">char*</span></tt>, while the other accepts a <a class="reference internal" href="../c-api/structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a>. Each type can use whichever pair makes more sense for the implementation’s convenience.</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">getattrfunc</span> <span class="n">tp_getattr</span><span class="p">;</span> <span class="cm">/* char * version */</span> <span class="n">setattrfunc</span> <span class="n">tp_setattr</span><span class="p">;</span> <span class="cm">/* ... */</span> <span class="n">getattrofunc</span> <span class="n">tp_getattrofunc</span><span class="p">;</span> <span class="cm">/* PyObject * version */</span> <span class="n">setattrofunc</span> <span class="n">tp_setattrofunc</span><span class="p">;</span> </pre></div> </div> <p>If accessing attributes of an object is always a simple operation (this will be explained shortly), there are generic implementations which can be used to provide the <a class="reference internal" href="../c-api/structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a> version of the attribute management functions. The actual need for type-specific attribute handlers almost completely disappeared starting with Python 2.2, though there are many examples which have not been updated to use some of the new generic mechanism that is available.</p> <div class="section" id="generic-attribute-management"> <span id="id6"></span><h4>2.2.3.1. Generic Attribute Management<a class="headerlink" href="#generic-attribute-management" title="Permalink to this headline">¶</a></h4> <p class="versionadded"> <span class="versionmodified">New in version 2.2.</span></p> <p>Most extension types only use <em>simple</em> attributes. So, what makes the attributes simple? There are only a couple of conditions that must be met:</p> <ol class="arabic simple"> <li>The name of the attributes must be known when <a class="reference internal" href="../c-api/type.html#PyType_Ready" title="PyType_Ready"><tt class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></tt></a> is called.</li> <li>No special processing is needed to record that an attribute was looked up or set, nor do actions need to be taken based on the value.</li> </ol> <p>Note that this list does not place any restrictions on the values of the attributes, when the values are computed, or how relevant data is stored.</p> <p>When <a class="reference internal" href="../c-api/type.html#PyType_Ready" title="PyType_Ready"><tt class="xref c c-func docutils literal"><span class="pre">PyType_Ready()</span></tt></a> is called, it uses three tables referenced by the type object to create <a class="reference internal" href="../glossary.html#term-descriptor"><em class="xref std std-term">descriptor</em></a>s which are placed in the dictionary of the type object. Each descriptor controls access to one attribute of the instance object. Each of the tables is optional; if all three are <em>NULL</em>, instances of the type will only have attributes that are inherited from their base type, and should leave the <tt class="xref py py-attr docutils literal"><span class="pre">tp_getattro</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">tp_setattro</span></tt> fields <em>NULL</em> as well, allowing the base type to handle attributes.</p> <p>The tables are declared as three fields of the type object:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">struct</span> <span class="n">PyMethodDef</span> <span class="o">*</span><span class="n">tp_methods</span><span class="p">;</span> <span class="k">struct</span> <span class="n">PyMemberDef</span> <span class="o">*</span><span class="n">tp_members</span><span class="p">;</span> <span class="k">struct</span> <span class="n">PyGetSetDef</span> <span class="o">*</span><span class="n">tp_getset</span><span class="p">;</span> </pre></div> </div> <p>If <tt class="xref py py-attr docutils literal"><span class="pre">tp_methods</span></tt> is not <em>NULL</em>, it must refer to an array of <a class="reference internal" href="../c-api/structures.html#PyMethodDef" title="PyMethodDef"><tt class="xref c c-type docutils literal"><span class="pre">PyMethodDef</span></tt></a> structures. Each entry in the table is an instance of this structure:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="n">PyMethodDef</span> <span class="p">{</span> <span class="kt">char</span> <span class="o">*</span><span class="n">ml_name</span><span class="p">;</span> <span class="cm">/* method name */</span> <span class="n">PyCFunction</span> <span class="n">ml_meth</span><span class="p">;</span> <span class="cm">/* implementation function */</span> <span class="kt">int</span> <span class="n">ml_flags</span><span class="p">;</span> <span class="cm">/* flags */</span> <span class="kt">char</span> <span class="o">*</span><span class="n">ml_doc</span><span class="p">;</span> <span class="cm">/* docstring */</span> <span class="p">}</span> <span class="n">PyMethodDef</span><span class="p">;</span> </pre></div> </div> <p>One entry should be defined for each method provided by the type; no entries are needed for methods inherited from a base type. One additional entry is needed at the end; it is a sentinel that marks the end of the array. The <tt class="xref py py-attr docutils literal"><span class="pre">ml_name</span></tt> field of the sentinel must be <em>NULL</em>.</p> <p>XXX Need to refer to some unified discussion of the structure fields, shared with the next section.</p> <p>The second table is used to define attributes which map directly to data stored in the instance. A variety of primitive C types are supported, and access may be read-only or read-write. The structures in the table are defined as:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="n">PyMemberDef</span> <span class="p">{</span> <span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">;</span> <span class="kt">int</span> <span class="n">type</span><span class="p">;</span> <span class="kt">int</span> <span class="n">offset</span><span class="p">;</span> <span class="kt">int</span> <span class="n">flags</span><span class="p">;</span> <span class="kt">char</span> <span class="o">*</span><span class="n">doc</span><span class="p">;</span> <span class="p">}</span> <span class="n">PyMemberDef</span><span class="p">;</span> </pre></div> </div> <p>For each entry in the table, a <a class="reference internal" href="../glossary.html#term-descriptor"><em class="xref std std-term">descriptor</em></a> will be constructed and added to the type which will be able to extract a value from the instance structure. The <a class="reference internal" href="../library/functions.html#type" title="type"><tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt></a> field should contain one of the type codes defined in the <tt class="file docutils literal"><span class="pre">structmember.h</span></tt> header; the value will be used to determine how to convert Python values to and from C values. The <tt class="xref py py-attr docutils literal"><span class="pre">flags</span></tt> field is used to store flags which control how the attribute can be accessed.</p> <p>XXX Need to move some of this to a shared section!</p> <p>The following flag constants are defined in <tt class="file docutils literal"><span class="pre">structmember.h</span></tt>; they may be combined using bitwise-OR.</p> <table border="1" class="docutils"> <colgroup> <col width="37%" /> <col width="63%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Constant</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="xref py py-const docutils literal"><span class="pre">READONLY</span></tt></td> <td>Never writable.</td> </tr> <tr class="row-odd"><td><tt class="xref py py-const docutils literal"><span class="pre">RO</span></tt></td> <td>Shorthand for <tt class="xref py py-const docutils literal"><span class="pre">READONLY</span></tt>.</td> </tr> <tr class="row-even"><td><tt class="xref py py-const docutils literal"><span class="pre">READ_RESTRICTED</span></tt></td> <td>Not readable in restricted mode.</td> </tr> <tr class="row-odd"><td><tt class="xref py py-const docutils literal"><span class="pre">WRITE_RESTRICTED</span></tt></td> <td>Not writable in restricted mode.</td> </tr> <tr class="row-even"><td><tt class="xref py py-const docutils literal"><span class="pre">RESTRICTED</span></tt></td> <td>Not readable or writable in restricted mode.</td> </tr> </tbody> </table> <p id="index-3">An interesting advantage of using the <tt class="xref py py-attr docutils literal"><span class="pre">tp_members</span></tt> table to build descriptors that are used at runtime is that any attribute defined this way can have an associated doc string simply by providing the text in the table. An application can use the introspection API to retrieve the descriptor from the class object, and get the doc string using its <tt class="xref py py-attr docutils literal"><span class="pre">__doc__</span></tt> attribute.</p> <p>As with the <tt class="xref py py-attr docutils literal"><span class="pre">tp_methods</span></tt> table, a sentinel entry with a <tt class="xref py py-attr docutils literal"><span class="pre">name</span></tt> value of <em>NULL</em> is required.</p> </div> <div class="section" id="type-specific-attribute-management"> <h4>2.2.3.2. Type-specific Attribute Management<a class="headerlink" href="#type-specific-attribute-management" title="Permalink to this headline">¶</a></h4> <p>For simplicity, only the <tt class="xref c c-type docutils literal"><span class="pre">char*</span></tt> version will be demonstrated here; the type of the name parameter is the only difference between the <tt class="xref c c-type docutils literal"><span class="pre">char*</span></tt> and <a class="reference internal" href="../c-api/structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a> flavors of the interface. This example effectively does the same thing as the generic example above, but does not use the generic support added in Python 2.2. The value in showing this is two-fold: it demonstrates how basic attribute management can be done in a way that is portable to older versions of Python, and explains how the handler functions are called, so that if you do need to extend their functionality, you’ll understand what needs to be done.</p> <p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_getattr</span></tt> handler is called when the object requires an attribute look-up. It is called in the same situations where the <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> method of a class would be called.</p> <p>A likely way to handle this is (1) to implement a set of functions (such as <tt class="xref c c-func docutils literal"><span class="pre">newdatatype_getSize()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">newdatatype_setSize()</span></tt> in the example below), (2) provide a method table listing these functions, and (3) provide a getattr function that returns the result of a lookup in that table. The method table uses the same structure as the <tt class="xref py py-attr docutils literal"><span class="pre">tp_methods</span></tt> field of the type object.</p> <p>Here is an example:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">newdatatype_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="s">"getSize"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">newdatatype_getSize</span><span class="p">,</span> <span class="n">METH_VARARGS</span><span class="p">,</span> <span class="s">"Return the current size."</span><span class="p">},</span> <span class="p">{</span><span class="s">"setSize"</span><span class="p">,</span> <span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">newdatatype_setSize</span><span class="p">,</span> <span class="n">METH_VARARGS</span><span class="p">,</span> <span class="s">"Set the size."</span><span class="p">},</span> <span class="p">{</span><span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* sentinel */</span> <span class="p">};</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">newdatatype_getattr</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Py_FindMethod</span><span class="p">(</span><span class="n">newdatatype_methods</span><span class="p">,</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_setattr</span></tt> handler is called when the <a class="reference internal" href="../reference/datamodel.html#object.__setattr__" title="object.__setattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__setattr__()</span></tt></a> or <a class="reference internal" href="../reference/datamodel.html#object.__delattr__" title="object.__delattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__delattr__()</span></tt></a> method of a class instance would be called. When an attribute should be deleted, the third parameter will be <em>NULL</em>. Here is an example that simply raises an exception; if this were really all you wanted, the <tt class="xref py py-attr docutils literal"><span class="pre">tp_setattr</span></tt> handler should be set to <em>NULL</em>.</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">newdatatype_setattr</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">v</span><span class="p">)</span> <span class="p">{</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">PyErr_Format</span><span class="p">(</span><span class="n">PyExc_RuntimeError</span><span class="p">,</span> <span class="s">"Read-only attribute: \%s"</span><span class="p">,</span> <span class="n">name</span><span class="p">);</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> </div> </div> <div class="section" id="object-comparison"> <h3>2.2.4. Object Comparison<a class="headerlink" href="#object-comparison" title="Permalink to this headline">¶</a></h3> <div class="highlight-c"><div class="highlight"><pre><span class="n">cmpfunc</span> <span class="n">tp_compare</span><span class="p">;</span> </pre></div> </div> <p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_compare</span></tt> handler is called when comparisons are needed and the object does not implement the specific rich comparison method which matches the requested comparison. (It is always used if defined and the <a class="reference internal" href="../c-api/object.html#PyObject_Compare" title="PyObject_Compare"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_Compare()</span></tt></a> or <a class="reference internal" href="../c-api/object.html#PyObject_Cmp" title="PyObject_Cmp"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_Cmp()</span></tt></a> functions are used, or if <a class="reference internal" href="../library/functions.html#cmp" title="cmp"><tt class="xref py py-func docutils literal"><span class="pre">cmp()</span></tt></a> is used from Python.) It is analogous to the <a class="reference internal" href="../reference/datamodel.html#object.__cmp__" title="object.__cmp__"><tt class="xref py py-meth docutils literal"><span class="pre">__cmp__()</span></tt></a> method. This function should return <tt class="docutils literal"><span class="pre">-1</span></tt> if <em>obj1</em> is less than <em>obj2</em>, <tt class="docutils literal"><span class="pre">0</span></tt> if they are equal, and <tt class="docutils literal"><span class="pre">1</span></tt> if <em>obj1</em> is greater than <em>obj2</em>. (It was previously allowed to return arbitrary negative or positive integers for less than and greater than, respectively; as of Python 2.2, this is no longer allowed. In the future, other return values may be assigned a different meaning.)</p> <p>A <tt class="xref py py-attr docutils literal"><span class="pre">tp_compare</span></tt> handler may raise an exception. In this case it should return a negative value. The caller has to test for the exception using <a class="reference internal" href="../c-api/exceptions.html#PyErr_Occurred" title="PyErr_Occurred"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Occurred()</span></tt></a>.</p> <p>Here is a sample implementation:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">int</span> <span class="nf">newdatatype_compare</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span> <span class="n">obj1</span><span class="p">,</span> <span class="n">newdatatypeobject</span> <span class="o">*</span> <span class="n">obj2</span><span class="p">)</span> <span class="p">{</span> <span class="kt">long</span> <span class="n">result</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">obj1</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span> <span class="o"><</span> <span class="n">obj2</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">obj1</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span> <span class="o">></span> <span class="n">obj2</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">result</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> </div> <div class="section" id="abstract-protocol-support"> <h3>2.2.5. Abstract Protocol Support<a class="headerlink" href="#abstract-protocol-support" title="Permalink to this headline">¶</a></h3> <p>Python supports a variety of <em>abstract</em> ‘protocols;’ the specific interfaces provided to use these interfaces are documented in <a class="reference internal" href="../c-api/abstract.html#abstract"><em>Abstract Objects Layer</em></a>.</p> <p>A number of these abstract interfaces were defined early in the development of the Python implementation. In particular, the number, mapping, and sequence protocols have been part of Python since the beginning. Other protocols have been added over time. For protocols which depend on several handler routines from the type implementation, the older protocols have been defined as optional blocks of handlers referenced by the type object. For newer protocols there are additional slots in the main type object, with a flag bit being set to indicate that the slots are present and should be checked by the interpreter. (The flag bit does not indicate that the slot values are non-<em>NULL</em>. The flag may be set to indicate the presence of a slot, but a slot may still be unfilled.)</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">PyNumberMethods</span> <span class="o">*</span><span class="n">tp_as_number</span><span class="p">;</span> <span class="n">PySequenceMethods</span> <span class="o">*</span><span class="n">tp_as_sequence</span><span class="p">;</span> <span class="n">PyMappingMethods</span> <span class="o">*</span><span class="n">tp_as_mapping</span><span class="p">;</span> </pre></div> </div> <p>If you wish your object to be able to act like a number, a sequence, or a mapping object, then you place the address of a structure that implements the C type <a class="reference internal" href="../c-api/typeobj.html#PyNumberMethods" title="PyNumberMethods"><tt class="xref c c-type docutils literal"><span class="pre">PyNumberMethods</span></tt></a>, <a class="reference internal" href="../c-api/typeobj.html#PySequenceMethods" title="PySequenceMethods"><tt class="xref c c-type docutils literal"><span class="pre">PySequenceMethods</span></tt></a>, or <a class="reference internal" href="../c-api/typeobj.html#PyMappingMethods" title="PyMappingMethods"><tt class="xref c c-type docutils literal"><span class="pre">PyMappingMethods</span></tt></a>, respectively. It is up to you to fill in this structure with appropriate values. You can find examples of the use of each of these in the <tt class="file docutils literal"><span class="pre">Objects</span></tt> directory of the Python source distribution.</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">hashfunc</span> <span class="n">tp_hash</span><span class="p">;</span> </pre></div> </div> <p>This function, if you choose to provide it, should return a hash number for an instance of your data type. Here is a moderately pointless example:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">long</span> <span class="nf">newdatatype_hash</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span><span class="n">obj</span><span class="p">)</span> <span class="p">{</span> <span class="kt">long</span> <span class="n">result</span><span class="p">;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">obj</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">*</span> <span class="mi">3</span><span class="p">;</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <div class="highlight-c"><div class="highlight"><pre><span class="n">ternaryfunc</span> <span class="n">tp_call</span><span class="p">;</span> </pre></div> </div> <p>This function is called when an instance of your data type is “called”, for example, if <tt class="docutils literal"><span class="pre">obj1</span></tt> is an instance of your data type and the Python script contains <tt class="docutils literal"><span class="pre">obj1('hello')</span></tt>, the <tt class="xref py py-attr docutils literal"><span class="pre">tp_call</span></tt> handler is invoked.</p> <p>This function takes three arguments:</p> <ol class="arabic simple"> <li><em>arg1</em> is the instance of the data type which is the subject of the call. If the call is <tt class="docutils literal"><span class="pre">obj1('hello')</span></tt>, then <em>arg1</em> is <tt class="docutils literal"><span class="pre">obj1</span></tt>.</li> <li><em>arg2</em> is a tuple containing the arguments to the call. You can use <a class="reference internal" href="../c-api/arg.html#PyArg_ParseTuple" title="PyArg_ParseTuple"><tt class="xref c c-func docutils literal"><span class="pre">PyArg_ParseTuple()</span></tt></a> to extract the arguments.</li> <li><em>arg3</em> is a dictionary of keyword arguments that were passed. If this is non-<em>NULL</em> and you support keyword arguments, use <a class="reference internal" href="../c-api/arg.html#PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><tt class="xref c c-func docutils literal"><span class="pre">PyArg_ParseTupleAndKeywords()</span></tt></a> to extract the arguments. If you do not want to support keyword arguments and this is non-<em>NULL</em>, raise a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> with a message saying that keyword arguments are not supported.</li> </ol> <p>Here is a desultory example of the implementation of the call function.</p> <div class="highlight-c"><div class="highlight"><pre><span class="cm">/* Implement the call function.</span> <span class="cm"> * obj1 is the instance receiving the call.</span> <span class="cm"> * obj2 is a tuple containing the arguments to the call, in this</span> <span class="cm"> * case 3 strings.</span> <span class="cm"> */</span> <span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">newdatatype_call</span><span class="p">(</span><span class="n">newdatatypeobject</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">other</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">result</span><span class="p">;</span> <span class="kt">char</span> <span class="o">*</span><span class="n">arg1</span><span class="p">;</span> <span class="kt">char</span> <span class="o">*</span><span class="n">arg2</span><span class="p">;</span> <span class="kt">char</span> <span class="o">*</span><span class="n">arg3</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">PyArg_ParseTuple</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s">"sss:call"</span><span class="p">,</span> <span class="o">&</span><span class="n">arg1</span><span class="p">,</span> <span class="o">&</span><span class="n">arg2</span><span class="p">,</span> <span class="o">&</span><span class="n">arg3</span><span class="p">))</span> <span class="p">{</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> <span class="n">result</span> <span class="o">=</span> <span class="n">PyString_FromFormat</span><span class="p">(</span> <span class="s">"Returning -- value: [\%d] arg1: [\%s] arg2: [\%s] arg3: [\%s]</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">obj</span><span class="o">-></span><span class="n">obj_UnderlyingDatatypePtr</span><span class="o">-></span><span class="n">size</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">);</span> <span class="n">printf</span><span class="p">(</span><span class="s">"\%s"</span><span class="p">,</span> <span class="n">PyString_AS_STRING</span><span class="p">(</span><span class="n">result</span><span class="p">));</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>XXX some fields need to be added here...</p> <div class="highlight-c"><div class="highlight"><pre><span class="cm">/* Added in release 2.2 */</span> <span class="cm">/* Iterators */</span> <span class="n">getiterfunc</span> <span class="n">tp_iter</span><span class="p">;</span> <span class="n">iternextfunc</span> <span class="n">tp_iternext</span><span class="p">;</span> </pre></div> </div> <p>These functions provide support for the iterator protocol. Any object which wishes to support iteration over its contents (which may be generated during iteration) must implement the <tt class="docutils literal"><span class="pre">tp_iter</span></tt> handler. Objects which are returned by a <tt class="docutils literal"><span class="pre">tp_iter</span></tt> handler must implement both the <tt class="docutils literal"><span class="pre">tp_iter</span></tt> and <tt class="docutils literal"><span class="pre">tp_iternext</span></tt> handlers. Both handlers take exactly one parameter, the instance for which they are being called, and return a new reference. In the case of an error, they should set an exception and return <em>NULL</em>.</p> <p>For an object which represents an iterable collection, the <tt class="docutils literal"><span class="pre">tp_iter</span></tt> handler must return an iterator object. The iterator object is responsible for maintaining the state of the iteration. For collections which can support multiple iterators which do not interfere with each other (as lists and tuples do), a new iterator should be created and returned. Objects which can only be iterated over once (usually due to side effects of iteration) should implement this handler by returning a new reference to themselves, and should also implement the <tt class="docutils literal"><span class="pre">tp_iternext</span></tt> handler. File objects are an example of such an iterator.</p> <p>Iterator objects should implement both handlers. The <tt class="docutils literal"><span class="pre">tp_iter</span></tt> handler should return a new reference to the iterator (this is the same as the <tt class="docutils literal"><span class="pre">tp_iter</span></tt> handler for objects which can only be iterated over destructively). The <tt class="docutils literal"><span class="pre">tp_iternext</span></tt> handler should return a new reference to the next object in the iteration if there is one. If the iteration has reached the end, it may return <em>NULL</em> without setting an exception or it may set <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a>; avoiding the exception can yield slightly better performance. If an actual error occurs, it should set an exception and return <em>NULL</em>.</p> </div> <div class="section" id="weak-reference-support"> <span id="weakref-support"></span><h3>2.2.6. Weak Reference Support<a class="headerlink" href="#weak-reference-support" title="Permalink to this headline">¶</a></h3> <p>One of the goals of Python’s weak-reference implementation is to allow any type to participate in the weak reference mechanism without incurring the overhead on those objects which do not benefit by weak referencing (such as numbers).</p> <p>For an object to be weakly referencable, the extension must include a <a class="reference internal" href="../c-api/structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject*</span></tt></a> field in the instance structure for the use of the weak reference mechanism; it must be initialized to <em>NULL</em> by the object’s constructor. It must also set the <tt class="xref py py-attr docutils literal"><span class="pre">tp_weaklistoffset</span></tt> field of the corresponding type object to the offset of the field. For example, the instance type is defined with the following structure:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> <span class="n">PyObject_HEAD</span> <span class="n">PyClassObject</span> <span class="o">*</span><span class="n">in_class</span><span class="p">;</span> <span class="cm">/* The class object */</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">in_dict</span><span class="p">;</span> <span class="cm">/* A dictionary */</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">in_weakreflist</span><span class="p">;</span> <span class="cm">/* List of weak references */</span> <span class="p">}</span> <span class="n">PyInstanceObject</span><span class="p">;</span> </pre></div> </div> <p>The statically-declared type object for instances is defined this way:</p> <div class="highlight-c"><div class="highlight"><pre><span class="n">PyTypeObject</span> <span class="n">PyInstance_Type</span> <span class="o">=</span> <span class="p">{</span> <span class="n">PyObject_HEAD_INIT</span><span class="p">(</span><span class="o">&</span><span class="n">PyType_Type</span><span class="p">)</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"module.instance"</span><span class="p">,</span> <span class="cm">/* Lots of stuff omitted for brevity... */</span> <span class="n">Py_TPFLAGS_DEFAULT</span><span class="p">,</span> <span class="cm">/* tp_flags */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_doc */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_traverse */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_clear */</span> <span class="mi">0</span><span class="p">,</span> <span class="cm">/* tp_richcompare */</span> <span class="n">offsetof</span><span class="p">(</span><span class="n">PyInstanceObject</span><span class="p">,</span> <span class="n">in_weakreflist</span><span class="p">),</span> <span class="cm">/* tp_weaklistoffset */</span> <span class="p">};</span> </pre></div> </div> <p>The type constructor is responsible for initializing the weak reference list to <em>NULL</em>:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span> <span class="nf">instance_new</span><span class="p">()</span> <span class="p">{</span> <span class="cm">/* Other initialization stuff omitted for brevity */</span> <span class="n">self</span><span class="o">-></span><span class="n">in_weakreflist</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="k">return</span> <span class="p">(</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span> <span class="n">self</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p>The only further addition is that the destructor needs to call the weak reference manager to clear any weak references. This is only required if the weak reference list is non-<em>NULL</em>:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">instance_dealloc</span><span class="p">(</span><span class="n">PyInstanceObject</span> <span class="o">*</span><span class="n">inst</span><span class="p">)</span> <span class="p">{</span> <span class="cm">/* Allocate temporaries if needed, but do not begin</span> <span class="cm"> destruction just yet.</span> <span class="cm"> */</span> <span class="k">if</span> <span class="p">(</span><span class="n">inst</span><span class="o">-></span><span class="n">in_weakreflist</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="n">PyObject_ClearWeakRefs</span><span class="p">((</span><span class="n">PyObject</span> <span class="o">*</span><span class="p">)</span> <span class="n">inst</span><span class="p">);</span> <span class="cm">/* Proceed with object destruction normally. */</span> <span class="p">}</span> </pre></div> </div> </div> <div class="section" id="more-suggestions"> <h3>2.2.7. More Suggestions<a class="headerlink" href="#more-suggestions" title="Permalink to this headline">¶</a></h3> <p>Remember that you can omit most of these functions, in which case you provide <tt class="docutils literal"><span class="pre">0</span></tt> as a value. There are type definitions for each of the functions you must provide. They are in <tt class="file docutils literal"><span class="pre">object.h</span></tt> in the Python include directory that comes with the source distribution of Python.</p> <p>In order to learn how to implement any specific method for your new data type, do the following: Download and unpack the Python source distribution. Go the <tt class="file docutils literal"><span class="pre">Objects</span></tt> directory, then search the C source files for <tt class="docutils literal"><span class="pre">tp_</span></tt> plus the function you want (for example, <tt class="docutils literal"><span class="pre">tp_print</span></tt> or <tt class="docutils literal"><span class="pre">tp_compare</span></tt>). You will find examples of the function you want to implement.</p> <p>When you need to verify that an object is an instance of the type you are implementing, use the <a class="reference internal" href="../c-api/object.html#PyObject_TypeCheck" title="PyObject_TypeCheck"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_TypeCheck()</span></tt></a> function. A sample of its use might be something like the following:</p> <div class="highlight-c"><div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="n">PyObject_TypeCheck</span><span class="p">(</span><span class="n">some_object</span><span class="p">,</span> <span class="o">&</span><span class="n">MyType</span><span class="p">))</span> <span class="p">{</span> <span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_TypeError</span><span class="p">,</span> <span class="s">"arg #1 not a mything"</span><span class="p">);</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id7" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id2">[1]</a></td><td>This is true when we know that the object is a basic type, like a string or a float.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id8" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id3">[2]</a></td><td>We relied on this in the <tt class="xref py py-attr docutils literal"><span class="pre">tp_dealloc</span></tt> handler in this example, because our type doesn’t support garbage collection. Even if a type supports garbage collection, there are calls that can be made to “untrack” the object from garbage collection, however, these calls are advanced and not covered here.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id9" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id4">[3]</a></td><td>We now know that the first and last members are strings, so perhaps we could be less careful about decrementing their reference counts, however, we accept instances of string subclasses. Even though deallocating normal strings won’t call back into our objects, we can’t guarantee that deallocating an instance of a string subclass won’t call back into our objects.</td></tr> </tbody> </table> <table class="docutils footnote" frame="void" id="id10" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id5">[4]</a></td><td>Even in the third version, we aren’t guaranteed to avoid cycles. Instances of string subclasses are allowed and string subclasses could allow cycles even if normal strings don’t.</td></tr> </tbody> </table> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">2. Defining New Types</a><ul> <li><a class="reference internal" href="#the-basics">2.1. The Basics</a><ul> <li><a class="reference internal" href="#adding-data-and-methods-to-the-basic-example">2.1.1. Adding data and methods to the Basic example</a></li> <li><a class="reference internal" href="#providing-finer-control-over-data-attributes">2.1.2. Providing finer control over data attributes</a></li> <li><a class="reference internal" href="#supporting-cyclic-garbage-collection">2.1.3. Supporting cyclic garbage collection</a></li> <li><a class="reference internal" href="#subclassing-other-types">2.1.4. Subclassing other types</a></li> </ul> </li> <li><a class="reference internal" href="#type-methods">2.2. Type Methods</a><ul> <li><a class="reference internal" href="#finalization-and-de-allocation">2.2.1. Finalization and De-allocation</a></li> <li><a class="reference internal" href="#object-presentation">2.2.2. Object Presentation</a></li> <li><a class="reference internal" href="#attribute-management">2.2.3. Attribute Management</a><ul> <li><a class="reference internal" href="#generic-attribute-management">2.2.3.1. Generic Attribute Management</a></li> <li><a class="reference internal" href="#type-specific-attribute-management">2.2.3.2. Type-specific Attribute Management</a></li> </ul> </li> <li><a class="reference internal" href="#object-comparison">2.2.4. Object Comparison</a></li> <li><a class="reference internal" href="#abstract-protocol-support">2.2.5. Abstract Protocol Support</a></li> <li><a class="reference internal" href="#weak-reference-support">2.2.6. Weak Reference Support</a></li> <li><a class="reference internal" href="#more-suggestions">2.2.7. More Suggestions</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="extending.html" title="previous chapter">1. Extending Python with C or C++</a></p> <h4>Next topic</h4> <p class="topless"><a href="building.html" title="next chapter">3. Building C and C++ Extensions with distutils</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/extending/newtypes.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="building.html" title="3. Building C and C++ Extensions with distutils" >next</a> |</li> <li class="right" > <a href="extending.html" title="1. Extending Python with C or C++" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >Extending and Embedding the Python Interpreter</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close