Don't forget std::list.
authorRobert Bradshaw <robertwb@gmail.com>
Tue, 3 Jul 2012 06:37:06 +0000 (23:37 -0700)
committerRobert Bradshaw <robertwb@gmail.com>
Tue, 3 Jul 2012 08:26:45 +0000 (01:26 -0700)
Cython/Compiler/PyrexTypes.py
Cython/Utility/CppConvert.pyx
tests/run/cpp_stl_conversion.pyx

index 2087a7d..a8fc0dc 100755 (executable)
@@ -2987,7 +2987,7 @@ class CStructOrUnionType(CType):
         return super(CStructOrUnionType, self).cast_code(expr_code)
 
 
-builtin_cpp_conversions = ("std::string", "std::vector", "std::set", "std::map", "std::pair")
+builtin_cpp_conversions = ("std::string", "std::vector", "std::list", "std::set", "std::map", "std::pair")
 
 class CppClassType(CType):
     #  name          string
index a59236e..bcf1851 100644 (file)
@@ -74,8 +74,8 @@ cdef extern from *:
         void push_back(T&)
 
 @cname("{{cname}}")
-cdef cpp_list[{{T0}}] {{cname}}(object o) except *:
-    cdef cpp_list[{{T0}}] l
+cdef cpp_list[X] {{cname}}(object o) except *:
+    cdef cpp_list[X] l
     for item in o:
         l.push_back(X_from_py(item))
     return l
@@ -83,6 +83,8 @@ cdef cpp_list[{{T0}}] {{cname}}(object o) except *:
 
 #################### list.to_py ####################
 
+cimport cython
+
 cdef extern from *:
     ctypedef struct X "{{T0}}":
         pass
@@ -96,13 +98,14 @@ cdef extern from *:
             bint operator!=(const_iterator)
         const_iterator begin()
         const_iterator end()
-    cdef cppclass const_cpp_list "const std::list" [T] (cpp_list)
+    cdef cppclass const_cpp_list "const std::list" [T] (cpp_list):
+        pass
 
 @cname("{{cname}}")
 cdef object {{cname}}(const_cpp_list[X]& v):
     o = []
-    cdef cpp_list[X].const_iterator iter = s.begin()
-    while iter != s.end():
+    cdef cpp_list[X].const_iterator iter = v.begin()
+    while iter != v.end():
         o.append(X_to_py(cython.operator.dereference(iter)))
         cython.operator.preincrement(iter)
     return o
index 395d7a2..126ffb2 100644 (file)
@@ -6,6 +6,7 @@ from libcpp.set cimport set as cpp_set
 from libcpp.string cimport string
 from libcpp.pair cimport pair
 from libcpp.vector cimport vector
+from libcpp.list cimport list as cpp_list
 
 py_set = set
 
@@ -67,6 +68,14 @@ def test_pair(o):
     cdef pair[long, double] p = o
     return p
 
+def test_list(o):
+    """
+    >>> test_list([1, 2, 3])
+    [1, 2, 3]
+    """
+    cdef cpp_list[int] l = o
+    return l
+
 def test_set(o):
     """
     >>> sorted(test_set([1, 2, 3]))