From b9cecd602878334173aa9f6ed635d48739bfa2b1 Mon Sep 17 00:00:00 2001 From: da-woods Date: Wed, 29 Jul 2020 06:32:17 +0100 Subject: Fixed reference types being passed to getitemint (GH-3755) --- Cython/Compiler/ExprNodes.py | 2 ++ tests/run/lvalue_refs.pyx | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 9c3dd3952..3cf0d8623 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -3739,6 +3739,8 @@ class IndexNode(_IndexingBaseNode): if not base_type.is_cfunction: self.index = self.index.analyse_types(env) self.original_index_type = self.index.type + if self.original_index_type.is_reference: + self.original_index_type = self.original_index_type.ref_base_type if base_type.is_unicode_char: # we infer Py_UNICODE/Py_UCS4 for unicode strings in some diff --git a/tests/run/lvalue_refs.pyx b/tests/run/lvalue_refs.pyx index d42f2407e..6bd0d88bc 100644 --- a/tests/run/lvalue_refs.pyx +++ b/tests/run/lvalue_refs.pyx @@ -28,3 +28,15 @@ def test_lvalue_ref_assignment(): assert bar[0] == &baz[0][0] assert bar[0][0] == bongle + +# not *strictly* lvalue refs but this file seems the closest applicable place for it. +# GH 3754 - std::vector operator[] returns a reference, and this causes problems if +# the reference is passed into Cython __Pyx_GetItemInt +def test_ref_used_for_indexing(): + """ + >>> test_ref_used_for_indexing() + 'looked up correctly' + """ + cdef vector[int] idx = [1,2,3] + d = {1: "looked up correctly", 2:"oops"} + return d[idx[0]] -- cgit v1.2.1