@@ -214,26 +214,38 @@ def test_both_are_null(self):
214214 self .assertIsNone (src .value )
215215 self .assertIsNone (dst .value )
216216
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.
218220 src = create_shelllink_persist (self .IPersist )
219- dst = create_shelllink_persist ( self .IPersist )
221+ dst = self .IPersist ( )
220222
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.
222225 hr = CopyComPointer (src , byref (dst ))
226+
223227 self .assertEqual (S_OK , hr )
224228 self .assertEqual (src .value , dst .value )
225229
230+ # This indicates that the refcount was 2 before the `Release` call.
226231 self .assertEqual (1 , src .Release ())
227232
228233 clsid = dst .GetClassID ()
229234 self .assertEqual (TRUE , is_equal_guid (CLSID_ShellLink , clsid ))
230235
231236 self .assertEqual (0 , dst .Release ())
232237
233- def test_dest_is_nonnull (self ):
238+ def test_src_is_null_and_dest_is_nonnull (self ):
234239 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 ))
236247
248+ # This does NOT affects the refcount of `dst_orig`.
237249 hr = CopyComPointer (src , byref (dst ))
238250
239251 self .assertEqual (S_OK , hr )
@@ -242,21 +254,32 @@ def test_dest_is_nonnull(self):
242254 with self .assertRaises (ValueError ):
243255 dst .GetClassID () # NULL COM pointer access
244256
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 ):
246261 src = create_shelllink_persist (self .IPersist )
262+ dst_orig = create_shelllink_persist (self .IPersist )
247263 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 )
248269
249270 hr = CopyComPointer (src , byref (dst ))
250271
251272 self .assertEqual (S_OK , hr )
252273 self .assertEqual (src .value , dst .value )
274+ self .assertNotEqual (dst .value , dst_orig .value )
253275
254276 self .assertEqual (1 , src .Release ())
255277
256278 clsid = dst .GetClassID ()
257279 self .assertEqual (TRUE , is_equal_guid (CLSID_ShellLink , clsid ))
258280
259281 self .assertEqual (0 , dst .Release ())
282+ self .assertEqual (0 , dst_orig .Release ())
260283
261284
262285if __name__ == '__main__' :
0 commit comments