tags:

views:

201

answers:

1

I am trying to create a simple Python extension using [PyCXX]. And I'm compiling against my Python 2.5 installation.

My goal is to be able to do the following in Python:

import Cats

kitty = Cats.Kitty()

if type(kitty) == Cats.Kitty:
    kitty.Speak()

But every time I try, this is the error that I get:

TypeError: cannot create 'Kitty' instances

It does see Cats.Kitty as a type object, but I can't create instances of the Kitty class, any ideas?

Here is my current source:

#include "CXX/Objects.hxx"
#include "CXX/Extensions.hxx"
#include <iostream>

using namespace Py;
using namespace std;

class Kitty : public Py::PythonExtension<Kitty>
{
    public:
        Kitty()
        {
        }

        virtual ~Kitty()
        {
        }

        static void init_type(void)
        {
            behaviors().name("Kitty");
            behaviors().supportGetattr();

            add_varargs_method("Speak", &Kitty::Speak);
        }

        virtual Py::Object getattr( const char *name )
        {
            return getattr_methods( name );
        }

        Py::Object Speak( const Py::Tuple &args )
        {
            cout << "Meow!" << endl;
            return Py::None();
        }
};

class Cats : public ExtensionModule<Cats>
{
    public:
        Cats()
            : ExtensionModule<Cats>("Cats")
        {
            Kitty::init_type();

            initialize();

            Dict d(moduleDictionary());
            d["Kitty"] = Type((PyObject*)Kitty::type_object());
        }

        virtual ~Cats()
        {
        }

        Py::Object factory_Kitty( const Py::Tuple &rargs )
        {
            return Py::asObject( new Kitty );
        }
};

void init_Cats()
{
    static Cats* cats = new Cats;
}


int main(int argc, char* argv[])
{
    Py_Initialize();

    init_Cats();

    return Py_Main(argc, argv);

    return 0;
}
+1  A: 

I do'nt see it in the code, but sort of thing normally means it can't create an instance, which means it can't find a ctor. Are you sure you've got a ctor that exactly matches the expected signature?

Charlie Martin
You are right I think. He has to "register" the name Kitty as a type somewhere
happy_emi