Skip to content

Commit

Permalink
Separate the parts of test_safearray that depend on pythoncom int…
Browse files Browse the repository at this point in the history
…o a different module. (#684)

* Update `test_safearray_pywin32`.

* Add `unittest.SkipTest` to `test_safearray_pywin32`.

* Revert temporary renaming.

* Remove the `pythoncom` section from `test_safearray`.
  • Loading branch information
junkmd authored Dec 7, 2024
1 parent d87a27d commit 7295d40
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 360 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import array
import datetime
import unittest
from ctypes import POINTER, PyDLL, byref, c_long, py_object
from ctypes.wintypes import BOOL
from ctypes import POINTER, c_long
from decimal import Decimal

from comtypes import BSTR, IUnknown
Expand All @@ -19,7 +18,6 @@
_midlSAFEARRAY,
)
from comtypes.safearray import safearray_as_ndarray
from comtypes.test import is_resource_enabled
from comtypes.test.find_memleak import find_memleak


Expand Down Expand Up @@ -247,69 +245,5 @@ def doit():
self.assertFalse(bytes, "Leaks %d bytes" % bytes)


if is_resource_enabled("pythoncom"):
try:
import pythoncom
except ImportError:
# pywin32 not installed...
pass
else:
# pywin32 is available. The pythoncom dll contains two handy
# exported functions that allow to create a VARIANT from a Python
# object, also a function that unpacks a VARIANT into a Python
# object.
#
# This allows us to create und unpack SAFEARRAY instances
# contained in VARIANTs, and check for consistency with the
# comtypes code.

_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

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

class PyWinTest(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()
Loading

0 comments on commit 7295d40

Please sign in to comment.