From e00ee0a222a8eea16876f652a10394bf079d601d Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Fri, 29 Jun 2012 09:23:16 -0700 Subject: [PATCH] Allow nesting. --- Cython/Compiler/PyrexTypes.py | 9 ++++++--- Cython/Includes/libcpp/string.pxd | 3 +++ Cython/Utility/CppConvert.pyx | 29 ++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 9bfad24..50b95be 100755 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -30,7 +30,6 @@ class BaseType(object): all.append(c) else: all.append('_%x_' % ord(c)) - print self.declaration_code(""), ''.join(all) return ''.join(all) def base_declaration_code(self, base_code, entity_code): @@ -3026,9 +3025,12 @@ class CppClassType(CType): if not T.create_from_py_utility_code(env): return False tags.append(T.specialization_name()) - context["T%s" % ix] = T.declaration_code("", for_display=True) + # TODO: exception values + context["T%s" % ix] = T.declaration_code("") + context["T%s_from_py" % ix] = T.from_py_function cls = self.cname[5:] cname = "__pyx_convert_%s_from_py_%s" % (cls, "____".join(tags)) + print cls, "->", cname context['cname'] = cname from UtilityCode import CythonUtilityCode env.use_utility_code(CythonUtilityCode.load(cls + ".from_py", "CppConvert.pyx", context=context)) @@ -3045,7 +3047,8 @@ class CppClassType(CType): if not T.create_to_py_utility_code(env): return False tags.append(T.specialization_name()) - context["T%s" % ix] = T.declaration_code("", for_display=True) + context["T%s" % ix] = T.declaration_code("") + context["T%s_to_py" % ix] = T.to_py_function cls = self.cname[5:] cname = "__pyx_convert_%s_to_py_%s" % (cls, "____".join(tags)) context['cname'] = cname diff --git a/Cython/Includes/libcpp/string.pxd b/Cython/Includes/libcpp/string.pxd index e89794c..b2f8712 100644 --- a/Cython/Includes/libcpp/string.pxd +++ b/Cython/Includes/libcpp/string.pxd @@ -94,6 +94,9 @@ cdef extern from "" namespace "std": #string& operator= (char*) #string& operator= (char) + string operator+ (string& rhs) nogil + string operator+ (char* rhs) nogil + bint operator==(string&) nogil bint operator==(char*) nogil diff --git a/Cython/Utility/CppConvert.pyx b/Cython/Utility/CppConvert.pyx index e468aa2..39cadbb 100644 --- a/Cython/Utility/CppConvert.pyx +++ b/Cython/Utility/CppConvert.pyx @@ -153,6 +153,14 @@ cdef object {{cname}}(pair[{{T0}},{{T1}}]& p): #################### map.from_py #################### cdef extern from *: + ctypedef struct X "{{T0}}": + pass + ctypedef struct Y "{{T1}}": + pass + cdef X X_from_py "{{T0_from_py}}" (object) except * + cdef Y Y_from_py "{{T1_from_py}}" (object) except * + +cdef extern from *: cdef cppclass pair "std::pair" [T, U]: pair(T&, U&) cdef cppclass map "std::map" [T, U]: @@ -165,11 +173,11 @@ cdef extern from *: @cname("{{cname}}") -cdef map[{{T0}},{{T1}}] {{cname}}(object o) except *: +cdef map[X,Y] {{cname}}(object o) except *: cdef dict d = o - cdef map[{{T0}},{{T1}}] m + cdef map[X,Y] m for key, value in d.iteritems(): - m.insert(pair[{{T0}},{{T1}}](<{{T0}}>key, <{{T1}}>value)) + m.insert(pair[X,Y](X_from_py(key), Y_from_py(value))) return m @@ -179,6 +187,13 @@ cdef map[{{T0}},{{T1}}] {{cname}}(object o) except *: cimport cython +cdef extern from *: + ctypedef struct X "{{T0}}": + pass + ctypedef struct Y "{{T1}}": + pass + cdef object X_to_py "{{T0_to_py}}" (X) + cdef object Y_to_py "{{T1_to_py}}" (Y) cdef extern from *: cdef cppclass map "std::map" [T, U]: @@ -193,12 +208,12 @@ cdef extern from *: iterator end() @cname("{{cname}}") -cdef object {{cname}}(map[{{T0}},{{T1}}] s): +cdef object {{cname}}(map[X,Y] s): o = {} - cdef map[{{T0}},{{T1}}].value_type *key_value - cdef map[{{T0}},{{T1}}].iterator iter = s.begin() + cdef map[X,Y].value_type *key_value + cdef map[X,Y].iterator iter = s.begin() while iter != s.end(): key_value = &cython.operator.dereference(iter) - o[key_value.first] = key_value.second + o[X_to_py(key_value.first)] = Y_to_py(key_value.second) cython.operator.preincrement(iter) return o -- 2.7.4