@@ -214,26 +214,38 @@ def test_both_are_null(self):
214
214
self .assertIsNone (src .value )
215
215
self .assertIsNone (dst .value )
216
216
217
- def test_both_are_nonnull (self ):
217
+ def test_src_is_nonnull_and_dest_is_null (self ):
218
+ # The reference count of the COM pointer created by `CoCreateInstance`
219
+ # is initially 1.
218
220
src = create_shelllink_persist (self .IPersist )
219
- dst = create_shelllink_persist ( self .IPersist )
221
+ dst = self .IPersist ( )
220
222
221
- self .assertNotEqual (src .value , dst .value )
223
+ # `CopyComPointer` calls `AddRef` explicitly in the C implementation.
224
+ # The refcount of `src` is incremented from 1 to 2 here.
222
225
hr = CopyComPointer (src , byref (dst ))
226
+
223
227
self .assertEqual (S_OK , hr )
224
228
self .assertEqual (src .value , dst .value )
225
229
230
+ # This indicates that the refcount was 2 before the `Release` call.
226
231
self .assertEqual (1 , src .Release ())
227
232
228
233
clsid = dst .GetClassID ()
229
234
self .assertEqual (TRUE , is_equal_guid (CLSID_ShellLink , clsid ))
230
235
231
236
self .assertEqual (0 , dst .Release ())
232
237
233
- def test_dest_is_nonnull (self ):
238
+ def test_src_is_null_and_dest_is_nonnull (self ):
234
239
src = self .IPersist ()
235
- dst = create_shelllink_persist (self .IPersist )
240
+ dst_orig = create_shelllink_persist (self .IPersist )
241
+ dst = self .IPersist ()
242
+ CopyComPointer (dst_orig , byref (dst ))
243
+ dst_orig .Release () # The refcount of `dst_orig` is 1 here.
244
+
245
+ clsid = dst .GetClassID ()
246
+ self .assertEqual (TRUE , is_equal_guid (CLSID_ShellLink , clsid ))
236
247
248
+ # This does NOT affects the refcount of `dst_orig`.
237
249
hr = CopyComPointer (src , byref (dst ))
238
250
239
251
self .assertEqual (S_OK , hr )
@@ -242,21 +254,32 @@ def test_dest_is_nonnull(self):
242
254
with self .assertRaises (ValueError ):
243
255
dst .GetClassID () # NULL COM pointer access
244
256
245
- def test_src_is_nonnull (self ):
257
+ # This indicates that the refcount was 1 before the `Release` call.
258
+ self .assertEqual (0 , dst_orig .Release ())
259
+
260
+ def test_both_are_nonnull (self ):
246
261
src = create_shelllink_persist (self .IPersist )
262
+ dst_orig = create_shelllink_persist (self .IPersist )
247
263
dst = self .IPersist ()
264
+ CopyComPointer (dst_orig , byref (dst ))
265
+ dst_orig .Release ()
266
+
267
+ self .assertEqual (dst .value , dst_orig .value )
268
+ self .assertNotEqual (src .value , dst .value )
248
269
249
270
hr = CopyComPointer (src , byref (dst ))
250
271
251
272
self .assertEqual (S_OK , hr )
252
273
self .assertEqual (src .value , dst .value )
274
+ self .assertNotEqual (dst .value , dst_orig .value )
253
275
254
276
self .assertEqual (1 , src .Release ())
255
277
256
278
clsid = dst .GetClassID ()
257
279
self .assertEqual (TRUE , is_equal_guid (CLSID_ShellLink , clsid ))
258
280
259
281
self .assertEqual (0 , dst .Release ())
282
+ self .assertEqual (0 , dst_orig .Release ())
260
283
261
284
262
285
if __name__ == '__main__' :
0 commit comments