[C++-sig] Re: Newbie : build & return a new list

David Abrahams dave at boost-consulting.com
Fri Nov 21 13:33:31 CET 2003


"Benjamin Golinvaux" <Benjamin.Golinvaux at euresys.com> writes:

> Hello
>
> I am creating a function like this :
>
> PyObject* testCreateList()
> {
> 	...
> 	PyObject* newList = PyList_New(...);
> 	...
> 	return newList;
> }

Don't do that; use boost::python::list instead:

      #include <boost/python/list>
      using namespace boost::python;

      list testCreateList()
      {
          list newList;
          ...
          return newList;
      };

> can I assume that if I wrap it that way :
>
> 	def("testCreateList", testCreateList)
>
> Then will it behave correctly wrt references, 
> i.e. is doing this :
>
> myList = myTestModule.testCreateList()
>
> the same as 
>
> myList = [ .... ]
>
> ?
>
> or do I need to use the manage_new_object call policy ?
> I must say I don't understand when manage_new_object needs
> to be used.

It's used when you are returning some object you've allocated with
operator new and which would otherwise be leaked.

> Also, and this is unrelated, is there a nicer way to create
> a list using boost ? 	

See above ;-)

> I hope I'm not bothering you with my ultrabasic questions. 
> If there is some example code I can read to help me going 
> from the very simple tutorial to something more complex, 
> I'd be glad to read it !

http://www.boost.org/libs/python/test/list.cpp
http://www.boost.org/libs/python/test/list.py

might help a bit.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list