This helps ! Got it to work ! Thanks guys. Code attached for people who
could need it.

Franck

Le jeu. 30 janv. 2020 à 15:46, stefan <ste...@seefeld.name> a écrit :

>
> On 2020-01-29 4:01 p.m., HOUSSEN Franck wrote:
>
> With boost python tuple, how to loop over tuple items? In dummy.cpp
> attached, I'd like to get line 8 to work. Afterwards, I noticed line 7 does
> not even compile. How to get the length of a tuple (line 8), and, why there
> could be some ambiguity (line 7). Any help / clue is appreciated.
>
> Hi Frank,
>
> there are a few minor issues with your code, let me address them
> one-by-one to allow you to compile and run your example:
>
> * you don't need to link your extension module with the Python library.
> Symbols from that library are provided by the interpreter itself. (You only
> need to link to that when you embed the interpreter into your C++ app.)
>
> * you should not call `Py_Initialize()` during your module's
> initialization. Again, that function only needs to be called when you embed
> (and initialize) the interpreter itself.
>
> * use `boost::python::len(t)` to report the length of your tuple
>
> * the call `t[0]` itself is fine, but please be aware that it returns a
> `boost::python::object` instance. You can convert to a (Python) string and
> then extract the C++ string from that using
>
>     std::string s = bpl::extract<std::string>(bpl::str(t[0]));
>
> and print that.
>
> Hope this helps,
> [image: Stefan]
>
> --
>
>       ...ich hab' noch einen Koffer in Berlin...
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>


-- 
Bonne journée,

Franck HOUSSEN
#include <iostream>

#include <boost/python.hpp>

void doStuffs(boost::python::tuple & t) {
  std::cout << "tuple " << std::endl;
  for (size_t i = 0; i < len(t); i++) {
    std::string s = boost::python::extract<std::string>(boost::python::str(t[i]));
    std::cout << s << std::endl;
  }
}

BOOST_PYTHON_MODULE(dummy) {
  def("doStuffs", doStuffs);
}

Attachment: Makefile
Description: Binary data

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to