[Git][NTPsec/ntpsec][master] Deal with the fact that unqualified except catches SystemExit.

Eric S. Raymond gitlab at mg.gitlab.com
Fri Aug 11 19:31:11 UTC 2017


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
fe13be89 by Eric S. Raymond at 2017-08-11T15:30:50-04:00
Deal with the fact that unqualified except catches SystemExit.

- - - - -


1 changed file:

- ntpclients/ntpwait


Changes:

=====================================
ntpclients/ntpwait
=====================================
--- a/ntpclients/ntpwait
+++ b/ntpclients/ntpwait
@@ -136,86 +136,82 @@ if __name__ == "__main__":
     except getopt.GetoptError as err:
         sys.stderr.write(str(err) + "\n")
         raise SystemExit(2)
-    try:
-        tries = 100
-        sleep = 6
-        verbose = 0
-        for (switch, val) in options:
-            if switch in ("-n", "--tries"):
-                errmsg = "Error: -n parameter '%s' not a number\n"
-                tries = ntp.util.safeargcast(val, int, errmsg, __doc__)
-            elif switch in ("-s", "--sleep"):
-                errmsg = "Error: -s parameter '%s' not a number\n"
-                sleep = ntp.util.safeargcast(val, int, errmsg, __doc__)
-            elif switch in ("-v", "--verbose"):
-                verbose += 1
-            elif switch in ("-h", "--help"):
-                sys.stdout.write(__doc__)
-                raise SystemExit(0)
-
-        # Autoflush stdout
-        sys.stdout = Unbuffered(sys.stdout)
-
-        if verbose:
-            sys.stdout.write("Waiting for ntpd to synchronize...  ")
-
-        for i in range(1, tries):
-            session = ntp.packet.ControlSession()
-            # session.debug = 4
-            if not session.openhost("localhost"):
-                if verbose:
-                    sys.stdout.write("\bntpd is not running!\n")
-                continue
-
-            msg = None
-            try:
-                msg = session.doquery(2)     # Request system variables
-            except ntp.packet.ControlException as e:
-                sys.stderr.write("localhost: timed out, nothing received\n")
-                sys.stderr.write(e.message)
-            except socket.error:
-                if verbose:
-                    sys.stdout.write("\b" + "*+:."[i % 4])
-                time.sleep(sleep)
-                continue
-
-            if verbose >= 2:
-                sys.stderr.write(repr(session.response) + "\n")
-
-            if msg and msg.startswith("***"):
-                if verbose:
-                    sys.stdout.write("\b" + msg + "\n")
-                sys.exit(1)
-
-            m = re.search(r"leap=([^,]*),", session.response)
-            if m:
-                leap = int(m.group(1))
-            else:
-                sys.stdout.write("\bLeap status not available\n")
-                sys.exit(1)
-
-            if leap == ntp.magic.LEAP_NOTINSYNC:
-                if verbose:
-                    sys.stdout.write("\b" + "*+:."[i % 4])
-                if i < tries:
-                    time.sleep(sleep)
-                continue
-
-            if leap in (ntp.magic.LEAP_NOWARNING, ntp.magic.LEAP_ADDSECOND,
-                        ntp.magic.LEAP_DELSECOND):
-                # We could check "sync" here to make sure we like the source...
-                if verbose:
-                    sys.stdout.write("\bOK!\n")
-                sys.exit(0)
-
-            sys.stdout.write("\bUnexpected 'leap' status <%s>\n" % leap)
+    tries = 100
+    sleep = 6
+    verbose = 0
+    for (switch, val) in options:
+        if switch in ("-n", "--tries"):
+            errmsg = "Error: -n parameter '%s' not a number\n"
+            tries = ntp.util.safeargcast(val, int, errmsg, __doc__)
+        elif switch in ("-s", "--sleep"):
+            errmsg = "Error: -s parameter '%s' not a number\n"
+            sleep = ntp.util.safeargcast(val, int, errmsg, __doc__)
+        elif switch in ("-v", "--verbose"):
+            verbose += 1
+        elif switch in ("-h", "--help"):
+            sys.stdout.write(__doc__)
+            raise SystemExit(0)
+
+    # Autoflush stdout
+    sys.stdout = Unbuffered(sys.stdout)
+
+    if verbose:
+        sys.stdout.write("Waiting for ntpd to synchronize...  ")
+
+    for i in range(1, tries):
+        session = ntp.packet.ControlSession()
+        # session.debug = 4
+        if not session.openhost("localhost"):
+            if verbose:
+                sys.stdout.write("\bntpd is not running!\n")
+            continue
+
+        msg = None
+        try:
+            msg = session.doquery(2)     # Request system variables
+        except ntp.packet.ControlException as e:
+            sys.stderr.write("localhost: timed out, nothing received\n")
+            sys.stderr.write(e.message)
+        except socket.error:
+            if verbose:
+                sys.stdout.write("\b" + "*+:."[i % 4])
+            time.sleep(sleep)
+            continue
+
+        if verbose >= 2:
+            sys.stderr.write(repr(session.response) + "\n")
+
+        if msg and msg.startswith("***"):
+            if verbose:
+                sys.stdout.write("\b" + msg + "\n")
+            sys.exit(1)
+
+        m = re.search(r"leap=([^,]*),", session.response)
+        if m:
+            leap = int(m.group(1))
+        else:
+            sys.stdout.write("\bLeap status not available\n")
             sys.exit(1)
 
-        if verbose:
-            sys.stdout.write("\bNo!\nntpd did not synchronize.\n")
+        if leap == ntp.magic.LEAP_NOTINSYNC:
+            if verbose:
+                sys.stdout.write("\b" + "*+:."[i % 4])
+            if i < tries:
+                time.sleep(sleep)
+            continue
+
+        if leap in (ntp.magic.LEAP_NOWARNING, ntp.magic.LEAP_ADDSECOND,
+                    ntp.magic.LEAP_DELSECOND):
+            # We could check "sync" here to make sure we like the source...
+            if verbose:
+                sys.stdout.write("\bOK!\n")
+            sys.exit(0)
+
+        sys.stdout.write("\bUnexpected 'leap' status <%s>\n" % leap)
         sys.exit(1)
-    except:
-        sys.stderr.write("Aborted - possible internal error.\n")
-        sys.exit(2)
+
+    if verbose:
+        sys.stdout.write("\bNo!\nntpd did not synchronize.\n")
+    sys.exit(1)
 
 # end



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

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/fe13be892020828295779a1bdf1bd4421b5d9168
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/20170811/546ff43a/attachment.html>


More information about the vc mailing list