Boost logo

Boost :

From: rwgk_at_[hidden]
Date: 2000-12-08 19:47:46


> What mechanism are you referring to?

The one that is implemented in

http://cci.lbl.gov/~rwgk/cvs/cvsweb.cgi/bpl_tru64/import_constructor.c
pp?rev=1.1.1.1&content-type=text/x-cvsweb-markup

and used in

http://cci.lbl.gov/~rwgk/cvs/cvsweb.cgi/bpl_tru64/ivect.cpp?
rev=1.1.1.1&content-type=text/x-cvsweb-markup

  PyObject *ivect_as_dvect_hack(const vects::ivect& iv)
  {
    PyObject *dvect_constructor = import_constructor
("dvect", "dvect");
    python::tuple args(1);
    args.set_item(0, iv.as_tuple());
    PyObject *dvect = PyObject_CallObject(dvect_constructor, args.get
());
    Py_DECREF(dvect_constructor);
    return dvect;
  }

import_constructor("dvect", "dvect") imports a pointer to a
callable Python object. PyObject_CallObject() uses this to
call the constructor that actually lives in dvect.so.

Using a PyCObject, one can export/import any type of pointer.
In fact, this is how, e.g., the NumPy CAPI is implemented.
See, e.g., the bottom quarter of this file:

http://cvs.sourceforge.net/cgi-
bin/cvsweb.cgi/Numerical/Include/arrayobject.h?rev=1.7&content-
type=text/x-cvsweb-markup&cvsroot=numpy

The #define import_array() at the very bottom is essentially
the same as my import_constructor().

Note that all functions of the array CAPI are accessed via #defines,
e.g.:

#define PyArray_PutMask \
  (*(PyArray_PutMask_RET (*)PyArray_PutMask_PROTO) \
   PyArray_API[PyArray_PutMask_NUM])

Admittedly, this looks quite ugly, but it is portable and at
the user level very convenient.

My hope is that this portable export/import mechanism including
instantiation of appropriate from_python/to_python templates
could be hidden from the BPL user.

Ralf


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk