[python-win32] wmi query error

Tim Golden mail at timgolden.me.uk
Wed Oct 21 10:19:34 CEST 2009


Nick Moszer wrote:
> Hello,
> 
> I'm occasionally receiving the following error when doing a set of queries:
> 
> 
> Traceback (most recent call last):
>   File "psg5220_demo.py", line 236, in find_hdd_drives
>     partitions = services.query(query)
>   File "...path..\wmi.py", line 889, in query
>     return [ _wmi_object (obj, instance_of, fields) for obj in
> self._raw_query(w
> ql) ]
>   File "C:\Python26\lib\site-packages\win32com\client\util.py", line 84,
> in next
> 
>     return _get_good_object_(self._iter_.next(), resultCLSID =
> self.resultCLSID)
> 
> pywintypes.com_error: (-2147217406, 'OLE error 0x80041002', None, None)

To make talking about it easier, I've slightly reworked
your code to make it run completely (ie I've added imports and
the initialisation of the data structures) and 
to take advantage of the wmi module's built-in features.
Hopefully it's perfectly clear what's going on; I've just avoided
some of the more "raw" wmi in your code.

I've also turned off find_classes as you don't really want this
in running code; it's more for use the interpreter. The latest
version of the module 1.4 turns this off by default which makes
the startup time much faster.

<code>
import wmi

services = wmi.WMI(privileges=["security"], find_classes=False)

serials = {}
serials_reversed = {}
drive_letters = {}

for disk in services.Win32_PhysicalMedia():
  serials[disk.Tag] = disk.SerialNumber
  serials_reversed[disk.Tag] = disk.SerialNumber

for physical_disk in services.Win32_DiskDrive():
  for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"):
    for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
      drive_letters[logical_disk.DeviceID.rstrip(":")] = physical_disk.DeviceID

print serials
print serials_reversed
print drive_letters

</code>

FWIW, the physical-logical drive stuff is straight out of the
Cookbook page:

http://timgolden.me.uk/python/wmi/cookbook.html#show-disk-partitions

> This is on Windows XP and Vista both.  It sometimes happens once then
> not again for 50 tries.  Sometimes it happens 5 times in a row.  It
> appears that an "OLE Error 0x80041002" is an "object not found error".
> So the query is running and line 86 in util.py is trying to get the next
> result from the query and is bombing out?  For some reason it things
> there are more results then there actually are?  This code is a direct
> port from working Perl code.  Anyone have any ideas?

I've managed to produce this error by having the script running
continuously and then inserting a USB stick -- watch the serials
dictionary grow -- and then "ejecting" it, at which point, the
WMI query obviously gets caught out between what it thinks is
there and what is there when it comes to query. It's essentially
a race-condition, I suppose.

Given that this can happen, I can only suggest you catch the
exception, look for the specific 80041002 error code and
ignore it / log it. This is assuming that the circumstances
in which you're running it make it likely that the insertion
or removal of another physical disk is a plausible cause.

TJG


More information about the python-win32 mailing list