-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-126384: Add tests to verify the behavior of basic COM methods. #126610
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff06232
Add tests.
junkmd 8049c1a
Add comments.
junkmd c85bd93
Add the platform bridge.
junkmd f77b18d
Update `IsEqualGUID`.
junkmd c43dfa4
Improve naming.
junkmd be31c06
Add comments.
junkmd c09a6a1
Add comments for typing.
junkmd f96a1ee
Improve platform bridges.
junkmd c90ae51
`ProtoComMethod` -> `create_proto_com_method`
junkmd cc07c44
`WindowsError` -> `OSError`
junkmd 6c60512
Add `if __name__ == '__main__'`
junkmd f0d3ede
Merge branch 'main' into add_com_method_tests
junkmd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import ctypes | ||
import gc | ||
import sys | ||
import unittest | ||
from ctypes import POINTER, byref, c_void_p | ||
from ctypes.wintypes import BYTE, DWORD, WORD | ||
|
||
if sys.platform != "win32": | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise unittest.SkipTest("Windows-specific test") | ||
|
||
|
||
from _ctypes import COMError | ||
from ctypes import HRESULT | ||
|
||
|
||
COINIT_APARTMENTTHREADED = 0x2 | ||
CLSCTX_SERVER = 5 | ||
S_OK = 0 | ||
OUT = 2 | ||
TRUE = 1 | ||
E_NOINTERFACE = -2147467262 | ||
|
||
|
||
class GUID(ctypes.Structure): | ||
# https://learn.microsoft.com/en-us/windows/win32/api/guiddef/ns-guiddef-guid | ||
_fields_ = [ | ||
("Data1", DWORD), | ||
("Data2", WORD), | ||
("Data3", WORD), | ||
("Data4", BYTE * 8), | ||
] | ||
|
||
|
||
def create_proto_com_method(name, index, restype, *argtypes): | ||
proto = ctypes.WINFUNCTYPE(restype, *argtypes) | ||
|
||
def make_method(*args): | ||
foreign_func = proto(index, name, *args) | ||
|
||
def call(self, *args, **kwargs): | ||
return foreign_func(self, *args, **kwargs) | ||
|
||
return call | ||
|
||
return make_method | ||
|
||
|
||
def create_guid(name): | ||
guid = GUID() | ||
# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-clsidfromstring | ||
ole32.CLSIDFromString(name, byref(guid)) | ||
return guid | ||
|
||
|
||
def is_equal_guid(guid1, guid2): | ||
# https://learn.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-isequalguid | ||
return ole32.IsEqualGUID(byref(guid1), byref(guid2)) | ||
|
||
|
||
ole32 = ctypes.oledll.ole32 | ||
|
||
IID_IUnknown = create_guid("{00000000-0000-0000-C000-000000000046}") | ||
IID_IStream = create_guid("{0000000C-0000-0000-C000-000000000046}") | ||
IID_IPersist = create_guid("{0000010C-0000-0000-C000-000000000046}") | ||
CLSID_ShellLink = create_guid("{00021401-0000-0000-C000-000000000046}") | ||
|
||
# https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void) | ||
proto_query_interface = create_proto_com_method( | ||
"QueryInterface", 0, HRESULT, POINTER(GUID), POINTER(c_void_p) | ||
) | ||
# https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-addref | ||
proto_add_ref = create_proto_com_method("AddRef", 1, ctypes.c_long) | ||
# https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release | ||
proto_release = create_proto_com_method("Release", 2, ctypes.c_long) | ||
# https://learn.microsoft.com/en-us/windows/win32/api/objidl/nf-objidl-ipersist-getclassid | ||
proto_get_class_id = create_proto_com_method( | ||
"GetClassID", 3, HRESULT, POINTER(GUID) | ||
) | ||
|
||
|
||
class ForeignFunctionsThatWillCallComMethodsTests(unittest.TestCase): | ||
def setUp(self): | ||
# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex | ||
ole32.CoInitializeEx(None, COINIT_APARTMENTTHREADED) | ||
|
||
def tearDown(self): | ||
# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize | ||
ole32.CoUninitialize() | ||
gc.collect() | ||
|
||
@staticmethod | ||
def create_shelllink_persist(typ): | ||
ppst = typ() | ||
# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance | ||
ole32.CoCreateInstance( | ||
byref(CLSID_ShellLink), | ||
None, | ||
CLSCTX_SERVER, | ||
byref(IID_IPersist), | ||
byref(ppst), | ||
) | ||
return ppst | ||
|
||
def test_without_paramflags_and_iid(self): | ||
class IUnknown(c_void_p): | ||
QueryInterface = proto_query_interface() | ||
AddRef = proto_add_ref() | ||
Release = proto_release() | ||
|
||
class IPersist(IUnknown): | ||
GetClassID = proto_get_class_id() | ||
|
||
ppst = self.create_shelllink_persist(IPersist) | ||
|
||
clsid = GUID() | ||
hr_getclsid = ppst.GetClassID(byref(clsid)) | ||
self.assertEqual(S_OK, hr_getclsid) | ||
self.assertEqual(TRUE, is_equal_guid(CLSID_ShellLink, clsid)) | ||
|
||
self.assertEqual(2, ppst.AddRef()) | ||
self.assertEqual(3, ppst.AddRef()) | ||
|
||
punk = IUnknown() | ||
hr_qi = ppst.QueryInterface(IID_IUnknown, punk) | ||
self.assertEqual(S_OK, hr_qi) | ||
self.assertEqual(3, punk.Release()) | ||
|
||
with self.assertRaises(OSError) as e: | ||
punk.QueryInterface(IID_IStream, IUnknown()) | ||
self.assertEqual(E_NOINTERFACE, e.exception.winerror) | ||
|
||
self.assertEqual(2, ppst.Release()) | ||
self.assertEqual(1, ppst.Release()) | ||
self.assertEqual(0, ppst.Release()) | ||
|
||
def test_with_paramflags_and_without_iid(self): | ||
class IUnknown(c_void_p): | ||
QueryInterface = proto_query_interface(None) | ||
AddRef = proto_add_ref() | ||
Release = proto_release() | ||
|
||
class IPersist(IUnknown): | ||
GetClassID = proto_get_class_id(((OUT, "pClassID"),)) | ||
|
||
ppst = self.create_shelllink_persist(IPersist) | ||
|
||
clsid = ppst.GetClassID() | ||
self.assertEqual(TRUE, is_equal_guid(CLSID_ShellLink, clsid)) | ||
|
||
punk = IUnknown() | ||
hr_qi = ppst.QueryInterface(IID_IUnknown, punk) | ||
self.assertEqual(S_OK, hr_qi) | ||
self.assertEqual(1, punk.Release()) | ||
|
||
with self.assertRaises(OSError) as e: | ||
ppst.QueryInterface(IID_IStream, IUnknown()) | ||
self.assertEqual(E_NOINTERFACE, e.exception.winerror) | ||
|
||
self.assertEqual(0, ppst.Release()) | ||
|
||
def test_with_paramflags_and_iid(self): | ||
class IUnknown(c_void_p): | ||
QueryInterface = proto_query_interface(None, IID_IUnknown) | ||
AddRef = proto_add_ref() | ||
Release = proto_release() | ||
|
||
class IPersist(IUnknown): | ||
GetClassID = proto_get_class_id(((OUT, "pClassID"),), IID_IPersist) | ||
|
||
ppst = self.create_shelllink_persist(IPersist) | ||
|
||
clsid = ppst.GetClassID() | ||
self.assertEqual(TRUE, is_equal_guid(CLSID_ShellLink, clsid)) | ||
|
||
punk = IUnknown() | ||
hr_qi = ppst.QueryInterface(IID_IUnknown, punk) | ||
self.assertEqual(S_OK, hr_qi) | ||
self.assertEqual(1, punk.Release()) | ||
|
||
with self.assertRaises(COMError) as e: | ||
ppst.QueryInterface(IID_IStream, IUnknown()) | ||
self.assertEqual(E_NOINTERFACE, e.exception.hresult) | ||
|
||
self.assertEqual(0, ppst.Release()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.