Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > mixing SWIG generated and Python-level usertype?

Thread Tools

mixing SWIG generated and Python-level usertype?

Bo Peng
Posts: n/a
Dear list,

My SWIG generated module (myModule) needs an array-like object (carray)
to work. Carray objects are created both internally (in C++ level) and
through Python so I have to load it when myModule initializes.

carray is modified from arraymodule.c and is quite simple:

static PyMethodDef a_methods[] =
{"carray", a_array, METH_VARARGS, a_array_doc},
{ /* sentinel */

Currently, I load a_methods directly using code (error checking ignored)

PyObject* mm = PyImport_AddModule("__main__");
PyObject* dict = PyModule_GetDict(mm);
PyObject* v = PyCFunction_New(a_methods, NULL);
PyDict_SetItemString(dict, a_methods->ml_name, v);

There are several problems with this approach:

1. use of __main__? carray can not be accessed directly within other
libraries. ('from myModule import *' DOES NOT import carray!) I tried to
use __builtins__ but it does not work for some reason out of my
understanding of Python. I am not sure how to add carray to myModule

2. No type object? I am not sure what is the purpose of ArrayType but
the usual init_module should be
m = Py_InitModule3("carray", a_methods, module_doc);
d = PyModule_GetDict(m);
PyDict_SetItemString(dict, "ArrayType", (PyObject *)&Arraytype);

When I add ArrayType to __main__ , access to ArrayType leads to a quick
core dump.

I do not feel comfortable with my current approach. Could anyone tell me
some better (more standard) way?

Many thanks in advance.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Fail import with SWIG generated imp.load_module ? MRAB Python 0 12-14-2010 06:10 PM
How to use list type generated by SWIG? Bryan Python 0 01-10-2010 08:21 PM
swig %typemap generated list typeerror Python 0 05-14-2007 09:30 PM
SWIG generated... TypeError julia Python 0 09-27-2004 03:39 AM
calling a swig generated function Michael Hale Ruby 3 08-21-2004 04:19 PM