# Must manage refcounts. Decref what is already there
# and incref what we put in.
ptr = code.funcstate.allocate_temp(self.buffer_type.buffer_ptr_type, manage_ref=False)
- if rhs.is_temp:
- rhs_code = code.funcstate.allocate_temp(rhs.type, manage_ref=False)
- else:
- rhs_code = rhs.result()
+ rhs_code = rhs.result()
code.putln("%s = %s;" % (ptr, ptrexpr))
code.putln("Py_DECREF(*%s); Py_INCREF(%s);" % (
ptr, rhs_code
))
code.putln("*%s %s= %s;" % (ptr, op, rhs_code))
- if rhs.is_temp:
- code.funcstate.release_temp(rhs_code)
code.funcstate.release_temp(ptr)
else:
# Simple case
"""
buf[idx] = obj
+@testcase
+def assign_temporary_to_object(object[object] buf):
+ """
+ See comments on printbuf_object above.
+
+ >>> a, b = [1, 2, 3], {4:23}
+ >>> get_refcount(a)
+ 2
+ >>> addref(a)
+ >>> A = ObjectMockBuffer(None, [b, a])
+ >>> get_refcount(a)
+ 3
+ >>> assign_temporary_to_object(A)
+ >>> get_refcount(a)
+ 2
+
+ >>> printbuf_object(A, (2,))
+ {4: 23} 2
+ {1: 8} 2
+
+ To avoid leaking a reference in our testcase we need to
+ replace the temporary with something we can manually decref :-)
+ >>> assign_to_object(A, 1, a)
+ >>> decref(a)
+ """
+ buf[1] = {3-2: 2+(2*4)-2}
+
#
# cast option
#