[C++-sig] opaque_pointer_converter being used correctly?

David Chiang chiang at ISI.EDU
Thu May 15 19:34:38 CEST 2008


Greetings, I am trying to use the opaque pointer converter to pass a 
pointer from C++ to a Python function (code below). When I pass the 
pointer directly, it compiles and I get:

$ test
0x12345
Segmentation fault

When I pass a pointer to a pointer, I get:

$ test
0x12345
f(<foo_t* object at 0x2aaaaafa00d8>)
0x12345

Should the first option have worked, or should it at least have 
generated a compile error or exception? Is there a third correct way to 
do this that I've missed? Thanks in advance.

David

----------------- test.cpp ------------------

#include <iostream>
#include <boost/python.hpp>

using namespace std;
using namespace boost::python;

struct foo_t;

BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(foo_t);

int main (int argc, char *argv[]) {
  Py_Initialize();
  opaque<foo_t>();
  foo_t *pfoo = reinterpret_cast<foo_t*>(0x12345);
  cout << hex << pfoo << endl;
  object module = import("test");
  object f = module.attr("f");
  try {
    //pfoo = extract<foo_t*>(f(pfoo)); // this doesn't work
    pfoo = extract<foo_t*>(f(&pfoo)); // is this right?
  } catch (error_already_set) {
    PyErr_Print();
  }
  cout << hex << pfoo << endl;
}

------------------- test.py -----------------------

def f(foo):
    print "f(%s)" % foo
    return foo

   
   




More information about the Cplusplus-sig mailing list