title: Creating an inheritable Python type with PyCxx
body: <p>A friend and I have been toying around with various Python C++ wrappers lately, trying to find one that meets the needs of both some professional and hobby projects. We've both honed in on <a href="http://cxx.sourceforge.net/" rel="nofollow noreferrer">PyCxx</a> as a good balance between being lightweight and easy to interface with while hiding away some of the ugliest bits of the Python C api. PyCxx is not terribly robust when it comes to exposing types, however (ie: it instructs you to create type factories rather than implement constructors), and we have been working on filling in the gaps in order to expose our types in a more functional manner. In order to fill these gaps we turn to the C api.</p>
  
  <p>This has left us with some questions, however, that the api documentation doesn't seem to cover in much depth (and when it does, the answers are occasionally contradictory). The basic overarching question is simply this: What must be defined for a Python type to function as a base type? We've found that for the PyCxx class to function as a type we need to define tp_new and tp_dealloc explicitly and set the type as a module attribute, and that we need to have Py_TPFLAGS_BASETYPE set on [our type]->tp_flags, but beyond that we're still groping in the dark.</p>
  
  <p>Here is our code thus far:</p>
  
  <pre><code>class kitty : public Py::PythonExtension&lt;kitty&gt; {
  public:
      kitty() : Py::PythonExtension&lt;kitty&gt;() {}
      virtual ~kitty() {}
  
      static void init_type() {
          behaviors().name("kitty");
          add_varargs_method("speak", &amp;kitty::speak);
      }
  
      static PyObject* tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) {
          return static_cast&lt;PyObject*&gt;(new kitty());
      }
  
      static void tp_dealloc(PyObject *obj) {
          kitty* k = static_cast&lt;kitty*&gt;(obj);
          delete k;
      }
  
  private:
      Py::Object speak(const Py::Tuple &amp;args) {
          cout &lt;&lt; "Meow!" &lt;&lt; endl;
          return Py::None();
      }
  };
  
  // cat Module
  class cat_module : public Py::ExtensionModule&lt;cat_module&gt; {
  public: 
      cat_module() : Py::ExtensionModule&lt;cat_module&gt;("cat") {
  
          kitty::init_type();
  
          // Set up additional properties on the kitty type object
          PyTypeObject* kittyType = kitty::type_object();
          kittyType-&gt;tp_new = &amp;kitty::tp_new;
          kittyType-&gt;tp_dealloc = &amp;kitty::tp_dealloc;
          kittyType-&gt;tp_flags |= Py_TPFLAGS_BASETYPE;
  
          // Expose the kitty type through the module
          module().setAttr("kitty", Py::Object((PyObject*)kittyType));
  
          initialize();
      }
      virtual ~cat_module() {}
  };
  
  extern "C" void initcat() {
      static cat_module* cat = new cat_module();
  }
  </code></pre>
  
  <p>And our Python test code looks like this:</p>
  
  <pre><code>import cat
  
  class meanKitty(cat.kitty):
      def scratch(self):
          print "hiss! *scratch*"
  
  myKitty = cat.kitty()
  myKitty.speak()
  
  meanKitty = meanKitty()
  meanKitty.speak()
  meanKitty.scratch()
  </code></pre>
  
  <p>The curious bit is that if you comment all the meanKitty bits out, the script runs and the cat meows just fine, but if you uncomment the meanKitty class suddenly Python gives us this:</p>
  
  <pre><code>AttributeError: 'kitty' object has no attribute 'speak'
  </code></pre>
  
  <p>Which confuses the crap out of me. It's as if inheriting from it hides the base class entirely! If anyone could provide some insight into what we are missing, it would be appreciated! Thanks!</p>
  
  <hr>
  
  <p>EDIT: Okay, so about five seconds after posting this I recalled something that we had wanted to try earlier. I added the following code to kitty -</p>
  
  <pre><code>virtual Py::Object getattr( const char *name ) {
      return getattr_methods( name );
  }
  </code></pre>
  
  <p>And now we're meowing on both kitties in Python! still not fully there, however, because now I get this:</p>
  
  <pre><code>Traceback (most recent call last):
      File "d:\Development\Junk Projects\PythonCxx\Toji.py", line 12, in &lt;module&gt;
      meanKitty.scratch()
  AttributeError: scratch
  </code></pre>
  
  <p>So still looking for some help! Thanks!</p>