GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-126384: Add tests to verify the behavior of basic COM methods. by junkmd · Pull Request #126610 · python/cpython

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.

Sign up for GitHub

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
encukou merged 12 commits into python:main from junkmd:add_com_method_tests
Nov 22, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-126384: Add tests to verify the behavior of basic COM methods. #126610

Changes from 1 commit
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
ff06232
Add tests.
junkmd Nov 9, 2024
8049c1a
Add comments.
junkmd Nov 9, 2024
c85bd93
Add the platform bridge.
junkmd Nov 9, 2024
f77b18d
Update `IsEqualGUID`.
junkmd Nov 9, 2024
c43dfa4
Improve naming.
junkmd Nov 10, 2024
be31c06
Add comments.
junkmd Nov 10, 2024
c09a6a1
Add comments for typing.
junkmd Nov 10, 2024
f96a1ee
Improve platform bridges.
junkmd Nov 18, 2024
c90ae51
`ProtoComMethod` -> `create_proto_com_method`
junkmd Nov 18, 2024
cc07c44
`WindowsError` -> `OSError`
junkmd Nov 18, 2024
6c60512
Add `if __name__ == '__main__'`
junkmd Nov 21, 2024
f0d3ede
Merge branch 'main' into add_com_method_tests
junkmd Nov 22, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
Add tests.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@junkmd
junkmd committed Nov 9, 2024
commit ff0623213a3e042a0bc526cd9463ebcdfc0f8601

There are no files selected for viewing

182 changes: 182 additions & 0 deletions 182 Lib/test/test_ctypes/test_win32_com_foreign_func.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import sys
import unittest


def setUpModule():
if sys.platform != "win32":
raise unittest.SkipTest("Windows-specific test")


import ctypes
import gc
from _ctypes import COMError
from ctypes import HRESULT, POINTER, byref, c_void_p
from ctypes.wintypes import BOOL, BYTE, DWORD, WORD

ole32 = ctypes.oledll.ole32

Comment thread
junkmd marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

COINIT_APARTMENTTHREADED = 0x2
CLSCTX_SERVER = 5
S_OK = 0
OUT = 2
TRUE = 1
E_NOINTERFACE = -2147467262

PyInstanceMethod_New = ctypes.pythonapi.PyInstanceMethod_New
PyInstanceMethod_New.argtypes = [ctypes.py_object]
PyInstanceMethod_New.restype = ctypes.py_object
PyInstanceMethod_Type = type(PyInstanceMethod_New(id))


class GUID(ctypes.Structure):
_fields_ = [
("Data1", DWORD),
("Data2", WORD),
("Data3", WORD),
("Data4", BYTE * 8),
]


class ProtoComMethod:
"""Utilities for tests that define COM methods."""

def __init__(self, index, restype, *argtypes):
self.index = index
self.proto = ctypes.WINFUNCTYPE(restype, *argtypes)

def __set_name__(self, owner, name):
foreign_func = self.proto(self.index, name, *self.args)
self.mth = PyInstanceMethod_Type(foreign_func)

def __call__(self, *args):
self.args = args
return self

def __get__(self, instance, owner=None):
return self.mth.__get__(instance)


def CLSIDFromString(name):
guid = GUID()
ole32.CLSIDFromString(name, byref(guid))
return guid


IsEqualGUID = ole32.IsEqualGUID
IsEqualGUID.argtypes = (GUID, GUID)
IsEqualGUID.restype = BOOL


IID_IUnknown = CLSIDFromString("{00000000-0000-0000-C000-000000000046}")
IID_IStream = CLSIDFromString("{0000000C-0000-0000-C000-000000000046}")
IID_IPersist = CLSIDFromString("{0000010C-0000-0000-C000-000000000046}")
CLSID_ShellLink = CLSIDFromString("{00021401-0000-0000-C000-000000000046}")


proto_qi = ProtoComMethod(0, HRESULT, POINTER(GUID), POINTER(c_void_p))
proto_addref = ProtoComMethod(1, ctypes.c_long)
proto_release = ProtoComMethod(2, ctypes.c_long)
proto_get_class_id = ProtoComMethod(3, HRESULT, POINTER(GUID))


class ForeignFunctionsThatWillCallComMethodsTests(unittest.TestCase):
def setUp(self):
ole32.CoInitializeEx(None, COINIT_APARTMENTTHREADED)

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

@staticmethod
def create_shelllink_persist(typ):
ppst = typ()
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_qi()
AddRef = proto_addref()
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, IsEqualGUID(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(WindowsError) 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_qi(None)
AddRef = proto_addref()
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, IsEqualGUID(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(WindowsError) 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_qi(None, IID_IUnknown)
AddRef = proto_addref()
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, IsEqualGUID(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())
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.