Python embedding question: how to expose a new "builtin"?

Rodrigo B. de Oliveira rodrigobamboo at hotmail.com
Wed Nov 20 17:34:19 EST 2002


Ok. I tried to figure out what's wrong with my code but...

Here's the definition of isapy_Application (_pyapplication's type):

<snip language="C++">
static PyTypeObject isapy_ApplicationType =
{
    PyObject_HEAD_INIT(NULL)
    0,
    "isapy.Application",
    sizeof(isapy_Application),
    0,
    isapy_Application_dealloc, /*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, //ternaryfunc tp_call;
    0, //reprfunc tp_str;
    0, //getattrofunc tp_getattro;
    0, //setattrofunc tp_setattro;

    /* Functions to access object as input/output buffer */
    0, //PyBufferProcs *tp_as_buffer;

    /* Flags to define presence of optional/expanded features */
    Py_TPFLAGS_HAVE_CLASS, //long tp_flags;

    "ISAPY Application Wrapper", //char *tp_doc; /* Documentation
string */

    /* Assigned meaning in release 2.0 */
    /* call function for all accessible objects */
    0, //traverseproc tp_traverse;

    /* delete references to contained objects */
    0, //inquiry tp_clear;

    /* Assigned meaning in release 2.1 */
    /* rich comparisons */
    0, //richcmpfunc tp_richcompare;

    /* weak reference enabler */
    0, //long tp_weaklistoffset;

    /* Added in release 2.2 */
    /* Iterators */
    0, //getiterfunc tp_iter;
    0, //iternextfunc tp_iternext;

    /* Attribute descriptor and subclassing stuff */
    isapy_Application_methods, //struct PyMethodDef *tp_methods;
    0, //struct PyMemberDef *tp_members;
    isapy_Application_getsetlist, //struct PyGetSetDef *tp_getset;
    0, //struct _typeobject *tp_base;
    0, //PyObject *tp_dict;
    0, //descrgetfunc tp_descr_get;
    0, //descrsetfunc tp_descr_set;
    0, //long tp_dictoffset;
    0, //initproc tp_init;
    0, //allocfunc tp_alloc;
    0, //newfunc tp_new;
    0, //freefunc tp_free; /* Low-level free-memory routine */
    0, //inquiry tp_is_gc; /* For PyObject_IS_GC */
    0, //PyObject *tp_bases;
    0, //PyObject *tp_mro; /* method resolution order */
    0, //PyObject *tp_cache;
    0, //PyObject *tp_subclasses;
    0 //PyObject *tp_weaklist;
};
</snip>

Here's how I create an isapy_Application instance:
<snip language="C++">
isapy_Application* newApplication(PythonApplication* application)
{
	assert(application);
	isapy_Application* pyapp = PyObject_New(isapy_Application,
&isapy_ApplicationType);
	pyapp->__init__(application);
	return pyapp;
}
</snip>

As you can see, isapy_Application is a wrapper for the
PythonApplication C++ class.

Here's how I use the definitions above:
<snip language="C++">
void PythonApplication::installApplicationBuiltin()
{
	_pyapplication = newApplication(this);

	PyObject* __builtin__ = PyImport_ImportModule("__builtin__");			
	PyObject_SetAttrString(__builtin__, "application",
(PyObject*)_pyapplication);
	Py_DECREF(__builtin__);	
}
<snip>

Anything wrong so far?

And here's the module initialization routine:
<snip language="C++">
void initisapy() 
{
	isapy_ExtensionControlBlockType.ob_type = &PyType_Type;
	isapy_ApplicationType.ob_type = &PyType_Type;

    if (PyType_Ready(&isapy_ExtensionControlBlockType) &&
		PyType_Ready(&isapy_ApplicationType))
	{
        return;
	}

	PyObject* module = Py_InitModule3("isapy", isapy_methods, "ISAPY
module");

	Py_INCREF((PyObject *)&isapy_ExtensionControlBlockType);
	PyModule_AddObject(module, "ExtensionControlBlock", (PyObject
*)&isapy_ExtensionControlBlockType);
	Py_INCREF((PyObject *)&isapy_ApplicationType);
	PyModule_AddObject(module, "Application", (PyObject
*)&isapy_ApplicationType);
}
</snip>


I won't give up because I really want to use python for my web
development (and right now I'm tied to customers who want/need IIS).

Thanks again,
Rodrigo



More information about the Python-list mailing list