gremlin/gremlin.in: Use `locale.getpreferredencoding'. master
authorMark Wooding <mdw@distorted.org.uk>
Mon, 15 Jan 2024 14:16:20 +0000 (14:16 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 15 Jan 2024 14:16:20 +0000 (14:16 +0000)
Inexplicably, Python doesn't attach the locale encoding to `stdout' by
default.

gremlin/gremlin.in

index f4ff738..522ca9b 100644 (file)
@@ -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