From: Mark Wooding Date: Mon, 15 Jan 2024 14:16:20 +0000 (+0000) Subject: gremlin/gremlin.in: Use `locale.getpreferredencoding'. X-Git-Url: https://git.distorted.org.uk/~mdw/autoys/commitdiff_plain/17fc1e1b2ac32dfff8133db5e9b648cdedc66839 gremlin/gremlin.in: Use `locale.getpreferredencoding'. Inexplicably, Python doesn't attach the locale encoding to `stdout' by default. --- diff --git a/gremlin/gremlin.in b/gremlin/gremlin.in index f4ff738..522ca9b 100644 --- a/gremlin/gremlin.in +++ b/gremlin/gremlin.in @@ -32,6 +32,7 @@ from __future__ import with_statement ## Standard Python libraries. import errno as E import fnmatch as FN +import locale as LC import optparse as OP import os as OS import re as RX @@ -71,6 +72,8 @@ G.threads_init() ###-------------------------------------------------------------------------- ### Eyecandy progress reports. +DEFAULT_ENCODING = None + def charwidth(s): """ Return the width of S, in characters. @@ -83,12 +86,14 @@ def charwidth(s): None of this handles tab characters in any kind of useful way. Sorry. """ - ## If there's no encoding for stdout then we're doing something stupid. - if SYS.stdout.encoding is None: return len(s) + global DEFAULT_ENCODING + + ## Figure out the default encoding. + if DEFAULT_ENCODING is None: DEFAULT_ENCODING = LC.getpreferredencoding() ## Turn the string into Unicode so we can hack on it properly. Maybe that ## won't work out, in which case fall back to being stupid. - try: u = s.decode(SYS.stdout.encoding) + try: u = s.decode(DEFAULT_ENCODING) except UnicodeError: return len(s) ## Our main problem is combining characters, but we should also try to