About wmi

patrol sunp1028 at gmail.com
Thu Jul 17 02:50:40 CEST 2008


On 7月17日, 上午3时20分, Tim Golden <m... at timgolden.me.uk> wrote:
> patrol wrote:
> > On 7月17日, 上午12时16分, Tim Golden <m... at timgolden.me.uk> wrote:
> >> Assuming that the error comes back in the sys.stdout encoding, the following version *should* work ok. I still haven't got a non-English set up to test it on, but it certainly does return a Unicode error message.
>
> >>http://timgolden.me.uk/wmi-project/wmi.py
>
> >> The usual test case, if you wouldn't mind:
>
> >> <code>
> >> import wmi
>
> >> wmi.WMI ("non-existent computer")
>
> >> </code>
>
> >> should give a (language-specific) error message, not an UnicodeDecodeError
>
> >> TJG
> > --------------------------------------------------------------------------------------
> >>>> import wmi
> >>>> wmi.WMI('non-existent computer')
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "C:\Python25\lib\wmi.py", line 1199, in connect
> >     handle_com_error (error_info)
> >   File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
> >     exception_string = [u"%s - %s" % (hex (hresult_code),
> > hresult_name)]
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
> > 4: ordinal
> > not in range(128)
> > --------------------------------------------------------------------------------------
> > yup,error_info contains the Chinese encoded string. All of the Simple
> > Chinese Windows use the CP936.Every Chinese word utilizes two
> > bytes.Maybe you can fix this bug by modifying  handle_com_error.
>
> Can you confirm that that last bit of
> code was run with the version of wmi.py
> currently at:
>
> http://timgolden.me.uk/wmi-project/wmi.py
>
> That version should already be decoding the
> string correctly.
>
> TJG
----------------------------------------------------------------------------------------
>>> import wmi
>>> wmi.WMI('non-existent computer')

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    wmi.WMI('non-existent computer')
  File "C:\Python25\lib\wmi.py", line 1199, in connect
    handle_com_error (error_info)
  File "C:\Python25\lib\wmi.py", line 189, in handle_com_error
    raise x_wmi, u"\n".join (exception_string)
x_wmi: <unprintable x_wmi object>
------------------------------------------------------------------------------------------
# -*- coding:utf-8 -*-
import wmi
from time import sleep

c = wmi.WMI ()
process_watcher = c.Win32_Process.watch_for("creation")
while True:
        new_process = process_watcher()
        if new_process.Caption == 'notepad.exe':
                print "start killing.."
                sleep(5)
                result = new_process.Terminate()
                print "killed"
When I kill the notepad.exe manually,it also results in errors.
Traceback (most recent call last):
  File "C:\Documents and Settings\patrol\桌面\test.py", line 12, in
<module>
    result = new_process.Terminate()
  File "C:\Python25\lib\wmi.py", line 396, in __call__
    handle_com_error (error_info)
  File "C:\Python25\lib\wmi.py", line 188, in handle_com_error
    exception_string.append (u"  %s - %s" % (hex (scode),
(error_description or
u"").decode (sys.stdout.encoding).strip ()))
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-2: ordin
al not in range(128)
-----------------------------------------------------------------------------------------------
Patrol



More information about the Python-list mailing list