8000 gh-127183: Add `_ctypes.CopyComPointer` tests by junkmd · Pull Request #127184 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127183: Add _ctypes.CopyComPointer tests #127184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more tests.
  • Loading branch information
junkmd committed Nov 24, 2024
commit 79f7055fcf221d8eb593493158cc2f64dbe9cfa7
62 changes: 49 additions & 13 deletions Lib/test/test_ctypes/test_win32_com_foreign_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ class CopyComPointerTests(unittest.TestCase):
def setUp(self):
ole32.CoInitializeEx(None, COINIT_APARTMENTTHREADED)

def tearDown(self):
ole32.CoUninitialize()
gc.collect()

def test_copy_com_pointer(self):
class IUnknown(c_void_p):
QueryInterface = proto_query_interface(None, IID_IUnknown)
AddRef = proto_add_ref()
Expand All @@ -201,26 +196,67 @@ class IUnknown(c_void_p):
class IPersist(IUnknown):
GetClassID = proto_get_class_id(((OUT, "pClassID"),), IID_IPersist)

src = create_shelllink_persist(IPersist)
dst = IPersist()
self.IUnknown = IUnknown
self.IPersist = IPersist

def tearDown(self):
ole32.CoUninitialize()
gc.collect()

def test_both_are_null(self):
src = self.IPersist()
dst = self.IPersist()

hr = CopyComPointer(src, byref(dst))

self.assertEqual(S_OK, hr)

self.assertIsNone(src.value)
self.assertIsNone(dst.value)

def test_both_are_nonnull(self):
src = create_shelllink_persist(self.IPersist)
dst = create_shelllink_persist(self.IPersist)

self.assertNotEqual(src.value, dst.value)
hr = CopyComPointer(src, byref(dst))
self.assertEqual(S_OK, hr)
self.assertEqual(src.value, dst.value)

self.assertEqual(1, src.Release())

clsid = dst.GetClassID()
self.assertEqual(TRUE, is_equal_guid(CLSID_ShellLink, clsid))

self.assertEqual(0, dst.Release())

def test_dest_is_nonnull(self):
src = self.IPersist()
dst = create_shelllink_persist(self.IPersist)

hr = CopyComPointer(src, byref(dst))

self.assertEqual(S_OK, hr)
self.assertIsNone(dst.value)

with self.assertRaises(ValueError):
dst.GetClassID() # NULL COM pointer access

def test_src_is_nonnull(self):
src = create_shelllink_persist(self.IPersist)
dst = self.IPersist()

hr = CopyComPointer(src, byref(dst))

self.assertEqual(S_OK, hr)
self.assertEqual(dst.value, src.value)
self.assertEqual(src.value, dst.value)

self.assertEqual(1, src.Release())

clsid = dst.GetClassID()
self.assertEqual(TRUE, is_equal_guid(CLSID_ShellLink, clsid))

# The refcount of a COM pointer created by `CoCreateInstance` is 1.
# `CopyComPointer` calls `AddRef` internally (thus, +1 to the refcount).
# Here, the refcount is decremented from 2 to 1.
self.assertEqual(1, dst.Release())
self.assertEqual(0, src.Release())
self.assertEqual(0, dst.Release())


if __name__ == '__main__':
Expand Down
0