[Git][NTPsec/ntpsec][master] ntpq: force utf-8 output in Python3.

Gary E. Miller gitlab at mg.gitlab.com
Thu Sep 28 21:55:39 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
11daaa20 by Gary E. Miller at 2017-09-28T14:54:06-07:00
ntpq: force utf-8 output in Python3.

python3 forces output codec to ASCII when piping.  So this used
to fail:

- - - - -


1 changed file:

- ntpclients/ntpq


Changes:

=====================================
ntpclients/ntpq
=====================================
--- a/ntpclients/ntpq
+++ b/ntpclients/ntpq
@@ -41,25 +41,24 @@ version = ntp.util.stdversion()
 # This code uses the following strategy to allow it to run on both Python 2
 # and Python 3:
 #
-# - Use binary I/O to read/write data from/to files and subprocesses;
-#   where the exact bytes are important (such as in checking for
-#   modified files), use the binary data directly
-#
 # - Use latin-1 encoding to transform binary data to/from Unicode when
 #   necessary for operations where Python 3 expects Unicode; the
 #   polystr and polybytes functions are used to do this so that
 #   when running on Python 2, the byte string data is used unchanged.
 #
-# - Construct custom stdin, stdout, and stderr streams when running
-#   on Python 3 that force latin-1 encoding, and wrap them around the
+# - Construct custom stdout and stderr streams when running
+#   on Python 3 that force UTF-8 encoding, and wrap them around the
 #   underlying binary buffers (in Python 2, the streams are binary
 #   and are used unchanged); this ensures that the same transformation
 #   is done on data from/to the standard streams, as is done on binary
 #   data from/to files and subprocesses; the make_std_wrapper function
-#   does this. This is only done if the stream encoding is ascii, which
-#   is true on Mac OS X.
+#   does this.
+#
+# anyone that changes this needs to test with all combinations of
+# python2, python3, LC_ALL=ascii, LC_ALL=latin-1, LC_ALL=en_US.utf8, and
+# piping output to a file.  While looking at the UTF-8 in the output.
 
-master_encoding = 'latin-1'
+forced_utf8 = False
 
 if str is bytes:  # Python 2
     polystr = unicode
@@ -102,14 +101,18 @@ else:  # Python 3
         # bytes to Unicode in polystr above
         # line_buffering=True ensures that interactive command sessions
         # work as expected
-        return io.TextIOWrapper(stream.buffer, encoding=master_encoding,
+        return io.TextIOWrapper(stream.buffer, encoding="utf-8",
                                 newline="\n", line_buffering=True)
 
-    if sys.stdout.encoding == "ascii":
+    if "UTF-8" != sys.stdout.encoding:
+        forced_utf8 = True
         sys.stdout = make_std_wrapper(sys.stdout)
-    if sys.stderr.encoding == "ascii":
+    if "UTF-8" != sys.stderr.encoding:
+        forced_utf8 = True
         sys.stderr = make_std_wrapper(sys.stderr)
 
+print(sys.stdout.encoding)
+
 # NTP-specific parts resume here
 
 # Flags for forming descriptors.
@@ -1666,6 +1669,9 @@ if __name__ == '__main__':
 
     session.logfp = interpreter.logfp = logfp
 
+    if forced_utf8 and interpreter.debug:
+        interpreter.warn("\nforced UTF-8 output\n")
+
     if keyfile is not None:  # Have a -k, setup the auth
         credentials = None
         try:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/11daaa20e651cad284a065feb1f9c7a9ffa0686a

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/11daaa20e651cad284a065feb1f9c7a9ffa0686a
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170928/6b1e36d2/attachment.html>


More information about the vc mailing list