summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Bradshaw <robertwb@gmail.com>2016-11-15 20:56:59 -0800
committerRobert Bradshaw <robertwb@gmail.com>2016-11-15 20:56:59 -0800
commitff0fc55b6af6641fa67d43d6238a52770676c8fd (patch)
tree1270066d2afaddfe9168791ed73e7f7dd7255448
parentf8c223d7b6a21380b3db46af05ce8e3ed03a0c8a (diff)
downloadcython-ff0fc55b6af6641fa67d43d6238a52770676c8fd.tar.gz
Allow taking address of fake references.
This fixes #1519.
-rw-r--r--Cython/Utility/ModuleSetupCode.c1
-rw-r--r--tests/wrappers/cpp_references.pyx11
2 files changed, 12 insertions, 0 deletions
diff --git a/Cython/Utility/ModuleSetupCode.c b/Cython/Utility/ModuleSetupCode.c
index 34d336d97..502bcad55 100644
--- a/Cython/Utility/ModuleSetupCode.c
+++ b/Cython/Utility/ModuleSetupCode.c
@@ -466,6 +466,7 @@ class __Pyx_FakeReference {
// Const version needed as Cython doesn't know about const overloads (e.g. for stl containers).
__Pyx_FakeReference(const T& ref) : ptr(const_cast<T*>(&ref)) { }
T *operator->() { return ptr; }
+ T *operator&() { return ptr; }
operator T&() { return *ptr; }
// TODO(robertwb): Delegate all operators (or auto-generate unwrapping code where needed).
template<typename U> bool operator ==(U other) { return *ptr == other; }
diff --git a/tests/wrappers/cpp_references.pyx b/tests/wrappers/cpp_references.pyx
index cc8a0b901..10b8bc416 100644
--- a/tests/wrappers/cpp_references.pyx
+++ b/tests/wrappers/cpp_references.pyx
@@ -5,6 +5,7 @@ cimport cython
cdef extern from "cpp_references_helper.h":
cdef int& ref_func(int&)
+ cdef int& except_ref_func "ref_func" (int&) except +
cdef int ref_var_value
cdef int& ref_var
@@ -29,6 +30,16 @@ def test_ref_func_address(int x):
cdef int* i_ptr = &ref_func(x)
return i_ptr[0]
+def test_except_ref_func_address(int x):
+ """
+ >>> test_except_ref_func_address(5)
+ 5
+ >>> test_except_ref_func_address(7)
+ 7
+ """
+ cdef int* i_ptr = &except_ref_func(x)
+ return i_ptr[0]
+
def test_ref_var(int x):
"""
>>> test_ref_func(11)