[Git][NTPsec/ntpsec][master] Fixed python 3 detection to use standard str is bytes method
Ian Bruene
gitlab at mg.gitlab.com
Thu Dec 14 23:23:55 UTC 2017
Ian Bruene pushed to branch master at NTPsec / ntpsec
Commits:
c1ba98f5 by Ian Bruene at 2017-12-14T17:22:41-06:00
Fixed python 3 detection to use standard str is bytes method
- - - - -
2 changed files:
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -18,16 +18,6 @@ import ntp.magic
import ntp.control
-if "get_terminal_size" not in dir(shutil):
- # used by termsize() on python 2.x systems
- import fcntl
- import termios
- import struct
- PY3 = False
-else:
- PY3 = True
-
-
# Old CTL_PST defines for version 2.
OLD_CTL_PST_CONFIG = 0x80
OLD_CTL_PST_AUTHENABLE = 0x40
@@ -587,13 +577,25 @@ def canonicalize_dns(inhost, family=socket.AF_UNSPEC):
TermSize = collections.namedtuple("TermSize", ["width", "height"])
+# Python 2.x does not have the shutil.get_terminal_size function.
+# This conditional import is only needed by termsize() and should be kept
+# near it. It is not inside the function because the unit tests need to be
+# able to splice in a jig.
+if str is bytes: # We are on python 2.x
+ import fcntl
+ import termios
+ import struct
+
+
def termsize():
"Return the current terminal size."
# Alternatives at http://stackoverflow.com/questions/566746
# The way this is used makes it not a big deal if the default is wrong.
size = (80, 24)
if os.isatty(1):
- if PY3 is True:
+ if str is not bytes:
+ # str is bytes means we are >py3.0, but this will still fail
+ # on versions <3.3. We do not support those anyway.
(w, h) = shutil.get_terminal_size((80, 24))
size = (w, h)
else:
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -560,7 +560,7 @@ class TestPylibUtilMethods(unittest.TestCase):
self.assertEqual(fakeosmod.isatty_calls, [1])
# termsize takes different code paths for different
# versions of Python
- if "get_terminal_size" in dir(shutil):
+ if str is not bytes:
# Python 3 version
try:
shutiltemp = ntp.util.shutil
@@ -573,7 +573,8 @@ class TestPylibUtilMethods(unittest.TestCase):
else:
# Python 2.x version
try:
- fcntltemp = ntp.util.fcntl
+ import fcntl
+ fcntltemp = fcntl
ntp.util.fcntl = fakefcntlmod
fakeosmod.isatty_returns = [True]
data = ["\x11\x11\x22\x22\x33\x33\x44\x44"]
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/c1ba98f51ec17a0900f2320b3f7cca709767d03e
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/c1ba98f51ec17a0900f2320b3f7cca709767d03e
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/20171214/fed64280/attachment.html>
More information about the vc
mailing list