ANN: psutil 5.3.0 with full unicode support is out

eryk sun eryksun at gmail.com
Mon Sep 4 02:54:57 EDT 2017


On Sun, Sep 3, 2017 at 11:09 PM, Giampaolo Rodola' <g.rodola at gmail.com> wrote:
>
> This is an example which filters processes with a funky name which works
> with both Python 2
> and 3:
>
>     import psutil, sys
>
>     PY3 = sys.version_info[0] == 2
>     LOOKFOR = u"ƒőő.exe"
>     for proc in psutil.process_iter(attrs=['name']):
>         name = proc.info['name']
>         if not PY3:
>             name = unicode(name, sys.getdefaultencoding(), errors="replace")
>         if LOOKFOR == name:
>              print("process %s found" % p)
>
> This is IMO the best compromise for a lib which aims to work on both Python
> 2 and 3. It's either that or returning Unicode all over the place in Python
> 2, but that's something I considered and rejected because most of the times
> the string is not supposed to have funky characters, so "practicality beats
> purity" in this case.

The encoded name for ANSI codepage 1252 is "\x83oo.exe". Python 2's
default encoding is ASCII, so the decoded result is u'\ufffdoo.exe'.
Even if it should be the filesystem encoding ('mbcs'), the result is
u"ƒoo.exe". Neither equals u"ƒőő.exe".

Instead, shouldn't it encode LOOKFOR as "mbcs" with errors="replace"
before comparing it to proc.info['name']?



More information about the Python-list mailing list