@@ -105,3 +105,43 @@ def test_mixed_args_and_kwargs(msg):
105
105
106
106
Invoked with: 1, 2; kwargs: j=1
107
107
""" # noqa: E501 line too long
108
+
109
+
110
+ def test_args_refcount ():
111
+ """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
112
+ arguments"""
113
+ refcount = m .arg_refcount_h
114
+
115
+ myval = 54321
116
+ expected = refcount (myval )
117
+ assert m .arg_refcount_h (myval ) == expected
118
+ assert m .arg_refcount_o (myval ) == expected + 1
119
+ assert m .arg_refcount_h (myval ) == expected
120
+ assert refcount (myval ) == expected
121
+
122
+ assert m .mixed_plus_args (1 , 2.0 , "a" , myval ) == (1 , 2.0 , ("a" , myval ))
123
+ assert refcount (myval ) == expected
124
+
125
+ assert m .mixed_plus_kwargs (3 , 4.0 , a = 1 , b = myval ) == (3 , 4.0 , {"a" : 1 , "b" : myval })
126
+ assert refcount (myval ) == expected
127
+
128
+ assert m .args_function (- 1 , myval ) == (- 1 , myval )
129
+ assert refcount (myval ) == expected
130
+
131
+ assert m .mixed_plus_args_kwargs (5 , 6.0 , myval , a = myval ) == (5 , 6.0 , (myval ,), {"a" : myval })
132
+ assert refcount (myval ) == expected
133
+
134
+ assert m .args_kwargs_function (7 , 8 , myval , a = 1 , b = myval ) == \
135
+ ((7 , 8 , myval ), {"a" : 1 , "b" : myval })
136
+ assert refcount (myval ) == expected
137
+
138
+ exp3 = refcount (myval , myval , myval )
139
+ assert m .args_refcount (myval , myval , myval ) == (exp3 , exp3 , exp3 )
140
+ assert refcount (myval ) == expected
141
+
142
+ # This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the
143
+ # previous case, when we have both positional and `py::args` we need to construct a new tuple
144
+ # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
145
+ # tuple without having to inc_ref the individual elements, but here we can't, hence the extra
146
+ # refs.
147
+ assert m .mixed_args_refcount (myval , myval , myval ) == (exp3 + 3 , exp3 + 3 , exp3 + 3 )
0 commit comments