STL containers of extension types

30 views
Skip to first unread message

Ian Bell

unread,
Nov 9, 2012, 6:54:33 AM11/9/12
to cython...@googlegroups.com
I would like to layout a very tightly structured structure of extension types and put them into STL containers so that when I iterate over them the type is encoded into the c-code rather than being introspected at runtime.  This works fine with c-types (string, int, double, etc.).  But I can't get this to work with Python extension types.  I understand they are not actually c datatypes, but for instance you can pass extension types into functions, I guess you just cant do this with python extension types.  Or am I just missing something?

Kind Regards,
Ian

Robert Bradshaw

unread,
Nov 9, 2012, 11:56:22 AM11/9/12
to cython...@googlegroups.com
Python types (including extension types) are reference counted, which
is why you don't have to do manual memory management in Python.
However, as the C++ layer doesn't know about Python's memory
management, it doesn't do the right thing when you try to put them
into a C++ STL container. There are two standard options here:

(1) Cast them to a PyObject* and keep track of the reference counting
(Py_INCREF/Py_DECREF) manually, just as you would if you were not
using Cython.

(2) Write a "smart pyobject pointer" class to encapsulate python
objects and do reference counting automatically. There's been talk of
providing this automatically in Cython.

- Robert
Reply all
Reply to author
Forward
0 new messages