[C++-sig] Stuck on accessing numpy.ndarray in c extension

Philip Austin paustin at eos.ubc.ca
Mon Sep 25 20:11:14 CEST 2006


Qinfeng(Javen) Shi  writes:
 > My boost::python exporting code is as following:
 > 
 > // Boost Includes
 > ==============================================================
 > #include <boost/python.hpp>
 > #include <boost/cstdint.hpp>
 > 
 > // Includes
 > ====================================================================
 > #include "hello.c"
 > // Using
 > =======================================================================
 > using namespace boost::python;
 > // Declarations
 > ================================================================
 > // Module
 > ======================================================================
 > BOOST_PYTHON_MODULE(hello)
 > {
 >     def("main", &main);
 >     def("testArray", &testArray);
 > }
 > 

You still have to initialize the function pointer array for the numpy
interface using import_array(), just as you do with a C extension.
For example, (using the boost::python::numeric):

namespace ar=boost::python::numeric;
namespace bp=boost::python;

BOOST_PYTHON_MODULE(simple_ext)
{ 
  import_array();
  ar::array::set_module_and_type("numpy", "ndarray");
  //global doc string
  bp::scope().attr("RCSID") = rcsid;
  std::string str1;
  str1="docstring for module\n";
  bp::scope().attr("__doc__") = str1.c_str();
  str1 = "Doc string for testToPython\n";
  def("testToPython", testToPython,str1.c_str());
  str1 = "Doc string for testFromPython\n";
  def("testFromPython", testFromPython,str1.c_str());
}




More information about the Cplusplus-sig mailing list