mixing SWIG generated and Python-level usertype?

Discussion in 'Python' started by Bo Peng, Feb 2, 2005.

  1. Bo Peng

    Bo Peng Guest

    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 */
    NULL, NULL
    }
    };

    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
    dictionary.

    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.
    Bo
     
    Bo Peng, Feb 2, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. julia

    SWIG generated... TypeError

    julia, Sep 27, 2004, in forum: Python
    Replies:
    0
    Views:
    434
    julia
    Sep 27, 2004
  2. pabbu
    Replies:
    8
    Views:
    733
    Marc Boyer
    Nov 7, 2005
  3. Replies:
    0
    Views:
    337
  4. Bryan
    Replies:
    0
    Views:
    281
    Bryan
    Jan 10, 2010
  5. MRAB
    Replies:
    0
    Views:
    418
Loading...

Share This Page