Skip to content

Commit

Permalink
Modernize test_wmi (#579)
Browse files Browse the repository at this point in the history
* remove bridges for old Python and unused imports

* delint comment

* add type annotations
  • Loading branch information
junkmd authored Jul 7, 2024
1 parent b3a5ed7 commit e92bcfe
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions comtypes/test/test_wmi.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import sys
import unittest as ut
from ctypes import POINTER

from comtypes.client import CoGetObject
from comtypes.test import requires

requires("time")

if sys.version_info >= (3, 0):
base_text_type = str
text_type = str
else:
base_text_type = basestring
text_type = unicode

# WMI has dual interfaces.
# Some methods/properties have "[out] POINTER(VARIANT)" parameters.
# This test checks that these parameters are returned as strings:
# that's what VARIANT.__ctypes_from_outparam__ does.
class Test(ut.TestCase):
def test_wmi(self):
wmi = CoGetObject("winmgmts:")
wmi: "WbemScripting.ISWbemServices" = CoGetObject("winmgmts:")
disks = wmi.InstancesOf("Win32_LogicalDisk")

# There are different typelibs installed for WMI on win2k and winXP.
Expand All @@ -28,34 +21,34 @@ def test_wmi(self):
# import comtypes.gen._565783C6_CB41_11D1_8B02_00600806D9B6_0_1_1 as mod
# WinXP:
# import comtypes.gen._565783C6_CB41_11D1_8B02_00600806D9B6_0_1_2 as mod
# So, the one that's referenced onm WbemScripting will be used, whether the actual
# typelib is available or not. XXX
# So, the one that's referenced onm WbemScripting will be used, whether the
# actual typelib is available or not. XXX
from comtypes.gen import WbemScripting

WbemScripting.wbemPrivilegeCreateToken

for item in disks:
# obj[index] is forwarded to obj.Item(index)
# .Value is a property with "[out] POINTER(VARIANT)" parameter.
item: "WbemScripting.ISWbemObject"
a = item.Properties_["Caption"].Value
b = item.Properties_.Item("Caption").Value
c = item.Properties_("Caption").Value
self.assertEqual(a, b)
self.assertEqual(a, c)
self.assertTrue(isinstance(a, base_text_type))
self.assertTrue(isinstance(b, base_text_type))
self.assertTrue(isinstance(c, base_text_type))
self.assertTrue(isinstance(a, str))
self.assertTrue(isinstance(b, str))
self.assertTrue(isinstance(c, str))
result = {}
for prop in item.Properties_:
self.assertTrue(isinstance(prop.Name, base_text_type))
prop: "WbemScripting.ISWbemProperty"
self.assertTrue(isinstance(prop.Name, str))
prop.Value
result[prop.Name] = prop.Value
# print "\t", (prop.Name, prop.Value)
self.assertEqual(len(item.Properties_), item.Properties_.Count)
self.assertEqual(len(item.Properties_), len(result))
self.assertTrue(
isinstance(item.Properties_["Description"].Value, text_type)
)
self.assertTrue(isinstance(item.Properties_["Description"].Value, str))
# len(obj) is forwared to obj.Count
self.assertEqual(len(disks), disks.Count)

Expand Down

0 comments on commit e92bcfe

Please sign in to comment.