embedding a new method

J jstier at cs.uvic.ca
Fri Jul 8 18:48:55 EDT 2005


Hi everyone,


I am fairly new to python (3rd day), but i am fairly keen on
replacing javascript. I want to externalize some of the mehtod
in my App, an allow user to imp

    char* lBuffer = "def handler(color):\n"
                    " print 12"

    PyObject* lCode = Py_CompileString(lBuffer, "<Object>",
Py_file_input);
    if (lCode)
    {
        mFunction = PyFunction_New(lCode, ScnGlobal::sDictionary);
        Py_XDECREF(lCode);
    }
    else
    {
        PyErr_Print();
    }

So far, there is no error, but I am wondering if that function object
knows about the signature of the function at this point.
Next I am defining an class like


    PyType_Ready(&PyType);
    Py_INCREF(&PyType);
    PyModule_AddObject(ScnGlobal::sModule, "Place",
(PyObject*)&PyType);

where PyType is pretty much taken out of the "embedding tutorial",
and ScnGlobal::sModule is the result of the call to
PyImport_ImportModule ("__main__"). I am installing tp_init and tp_new
in PyType.

Next, I instantiate an object of PyType like

    PyObject* new_args = PyTuple_New(0);
    mPyObject = (PyStruct*) PyObject_Call((PyObject*)&PyType, new_args,
0);

This also works and tp_init and tp_new get called. Now, I want to add
the new mehtod to this instance, and not the class, so I call


    PyObject* lParam = PyMethod_New(mFunction, (PyObject*)mPyObject,
(PyObject*)&PyType);
    if (lParam)
    {
        Py_XDECREF(lParam);
    }
    else
    {
        PyErr_Print();
    }

This call also returns an object. The problem starts when I try to
call the new method like so

        PyObject* lObject = PyObject_CallMethod((PyObject*)mPyObject,
"handler", "", NULL);

        if (lObject)
        {
            Py_DECREF(lObject);
        }
        else
        {
            PyErr_Print();
        }

This when I get an error saying "AtrributeError:handler". I would
really appreciate some help on this. Here is also what the PyType
structure looks like


PyTypeObject CmdPlace::PyType =
{
    PyObject_HEAD_INIT(NULL)
    0,                              /*ob_size*/
    "Place",                        /*tp_name*/
    sizeof(CmdPlace::PyStruct),     /*tp_basicsize*/
    0,                              /*tp_itemsize*/
    0,                              /*tp_dealloc*/
    0,                              /*tp_print*/
    0,                              /*tp_getattr*/
    0,                              /*tp_setattr*/
    0,                              /*tp_compare*/
    0,                              /*tp_repr*/
    0,                              /*tp_as_number*/
    0,                              /*tp_as_sequence*/
    0,                              /*tp_as_mapping*/
    0,                              /*tp_hash */
    0,                              /*tp_call*/
    0,                              /*tp_str*/
    0,                              /*tp_getattro*/
    0,                              /*tp_setattro*/
    0,                              /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "CmdPlace",                     /* tp_doc */
    0,		                        /* tp_traverse */
    0,		                        /* tp_clear */
    0,		                        /* tp_richcompare */
    0,		                        /* tp_weaklistoffset */
    0,		                        /* tp_iter */
    0,		                        /* tp_iternext */
    CmdPlace::sPyMethods,           /* tp_methods */
    0,                              /* tp_members */
    CmdPlace::sPyGetSeters,         /* tp_getset */
    0,                              /* tp_base */
    0,                              /* tp_dict */
    0,                              /* tp_descr_get */
    0,                              /* tp_descr_set */
    0,                              /* tp_dictoffset */
    (initproc)CmdPlace::sPyInit,    /* tp_init */
    0,                              /* tp_alloc */
    CmdPlace::sPyNew,               /* tp_new */
};


I hopesome has an answer. I have been stuck with this for a while.


Cheers
Jochen




More information about the Python-list mailing list