Mmm thanks for pointing this out. I don't have a Windows machine to test this against right now but it seems you're right and there's something wrong in my example (which is what I recommend in the official doc BTW, so it needs to be fixed).
That aside, do you think the rest of my reasoning makes sense? I mean returning str all the time and provide a strategy to convert the string to unicode in Python 2? On Mon, Sep 4, 2017 at 2:54 PM, eryk sun <eryk...@gmail.com> wrote: > On Sun, Sep 3, 2017 at 11:09 PM, Giampaolo Rodola' <g.rod...@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']? > -- Giampaolo - http://grodola.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list