Skip to content

Commit

Permalink
Move stuff from test_safearray_pywin32 to test_win32com_interop.
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd committed Dec 7, 2024
1 parent eb1f3c7 commit bab556b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 101 deletions.
73 changes: 0 additions & 73 deletions comtypes/test/test_safearray_pywin32.py

This file was deleted.

99 changes: 71 additions & 28 deletions comtypes/test/test_win32com_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,49 @@
from ctypes.wintypes import BOOL

from comtypes import IUnknown
from comtypes.automation import IDispatch
from comtypes.automation import IDispatch, VARIANT
from comtypes.client import CreateObject

try:
import pythoncom
import win32com.client

skip = False
# We use the PyCom_PyObjectFromIUnknown function in pythoncom25.dll to
# convert a comtypes COM pointer into a pythoncom COM pointer.
# Fortunately this function is exported by the dll...
#
# This is the C prototype; we must pass 'True' as third argument:
#
# PyObject *PyCom_PyObjectFromIUnknown(IUnknown *punk, REFIID riid, BOOL bAddRef)

_PyCom_PyObjectFromIUnknown = PyDLL(pythoncom.__file__).PyCom_PyObjectFromIUnknown
_PyCom_PyObjectFromIUnknown.restype = py_object
_PyCom_PyObjectFromIUnknown.argtypes = (POINTER(IUnknown), c_void_p, BOOL)
except ImportError:
# this test depends on pythoncom but it is not available. Maybe we should just skip it even
# if it is available since pythoncom is not a project dependency and adding tests depending
# on the vagaries of various testing environments is not deterministic.
# TODO: Evaluate if we should just remove this test or what.
skip = True


def setUpModule():
if skip:
raise unittest.SkipTest(
"This test requires the pythoncom library installed. If this is "
"important tests then we need to add dev dependencies to the project that include pythoncom."
)
raise unittest.SkipTest(
"This test requires the pythoncom library installed. If this is "
"important tests then we need to add dev dependencies to the project that include pythoncom."
)


_dll = PyDLL(pythoncom.__file__)

# c:/sf/pywin32/com/win32com/src/oleargs.cpp 213
# PyObject *PyCom_PyObjectFromVariant(const VARIANT *var)
unpack = _dll.PyCom_PyObjectFromVariant
unpack.restype = py_object
unpack.argtypes = (POINTER(VARIANT),)

# c:/sf/pywin32/com/win32com/src/oleargs.cpp 54
# BOOL PyCom_VariantFromPyObject(PyObject *obj, VARIANT *var)
_pack = _dll.PyCom_VariantFromPyObject
_pack.argtypes = py_object, POINTER(VARIANT)
_pack.restype = BOOL

# We use the PyCom_PyObjectFromIUnknown function in pythoncom25.dll to
# convert a comtypes COM pointer into a pythoncom COM pointer.
# Fortunately this function is exported by the dll...
#
# This is the C prototype; we must pass 'True' as third argument:
#
# PyObject *PyCom_PyObjectFromIUnknown(IUnknown *punk, REFIID riid, BOOL bAddRef)
_PyCom_PyObjectFromIUnknown = PyDLL(pythoncom.__file__).PyCom_PyObjectFromIUnknown
_PyCom_PyObjectFromIUnknown.restype = py_object
_PyCom_PyObjectFromIUnknown.argtypes = (POINTER(IUnknown), c_void_p, BOOL)


def pack(obj):
var = VARIANT()
_pack(obj, byref(var))
return var


def comtypes2pywin(ptr, interface=None):
Expand Down Expand Up @@ -77,7 +87,8 @@ class MyComObject(COMObject):
################################################################


class Test(unittest.TestCase):
@unittest.skip("This depends on IE.")
class Test_refcount(unittest.TestCase):
def tearDown(self):
if hasattr(self, "ie"):
self.ie.Quit()
Expand Down Expand Up @@ -112,5 +123,37 @@ def test_ie(self):
self.assertEqual(comtypes_get_refcount(ie), 1)


################################################################


class TestSafeArray(unittest.TestCase):
def test_1dim(self):
data = (1, 2, 3)
variant = pack(data)
self.assertEqual(variant.value, data)
self.assertEqual(unpack(variant), data)

def test_2dim(self):
data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
variant = pack(data)
self.assertEqual(variant.value, data)
self.assertEqual(unpack(variant), data)

def test_3dim(self):
data = (((1, 2), (3, 4), (5, 6)), ((7, 8), (9, 10), (11, 12)))
variant = pack(data)
self.assertEqual(variant.value, data)
self.assertEqual(unpack(variant), data)

def test_4dim(self):
data = (
(((1, 2), (3, 4)), ((5, 6), (7, 8))),
(((9, 10), (11, 12)), ((13, 14), (15, 16))),
)
variant = pack(data)
self.assertEqual(variant.value, data)
self.assertEqual(unpack(variant), data)


if __name__ == "__main__":
unittest.main()

0 comments on commit bab556b

Please sign in to comment.