From 73db250c4bfcd25433216a2ec3936f90b6662ff3 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Thu, 6 Oct 2016 12:41:07 -0700 Subject: [PATCH] Add support for vector[bool].at(...) --- Cython/Compiler/PyrexTypes.py | 13 +++++++++++++ tests/run/cpp_stl_vector.pyx | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 4ade1c1ee25..e82a74b383f 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -3478,6 +3478,19 @@ def specialize(self, values): if self.namespace is not None: specialized.namespace = self.namespace.specialize(values) specialized.scope = self.scope.specialize(values, specialized) + if self.cname == 'std::vector': + # vector is special cased in the C++ standard, and its + # accessors do not necessarily return references to the underlying + # elements (which may be bit-packed). + # http://www.cplusplus.com/reference/vector/vector-bool/ + # Here we pretend that the various methods return bool values + # (as the actual returned values are coercable to such, and + # we don't support call expressions as lvalues). + T = values[self.templates[0]] + if T.empty_declaration_code() == 'bool': + for bit_ref_returner in ('at', 'back', 'front'): + if bit_ref_returner in specialized.scope.entries: + specialized.scope.entries[bit_ref_returner].type.return_type = T return specialized def deduce_template_params(self, actual): diff --git a/tests/run/cpp_stl_vector.pyx b/tests/run/cpp_stl_vector.pyx index 7b9af484727..9d6d78b6b19 100644 --- a/tests/run/cpp_stl_vector.pyx +++ b/tests/run/cpp_stl_vector.pyx @@ -172,7 +172,8 @@ def test_bool_vector_get_set(): # Test access. assert not v[0], v assert v[1], v -# assert v.at(0) + assert not v.at(0), v + assert v.at(1), v v[0] = True v[1] = False assert v == [True, False, True, True, True]