[Python-checkins] r67647 - in python/branches/release30-maint: Lib/getopt.py Lib/test/test_getopt.py Lib/test/test_parser.py Misc/NEWS Modules/parsermodule.c configure.in

georg.brandl python-checkins at python.org
Sun Dec 7 17:04:05 CET 2008


Author: georg.brandl
Date: Sun Dec  7 17:04:03 2008
New Revision: 67647

Log:
Manual merge of r67639 from py3k branch:

Merged revisions 67463,67572,67576,67628 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

.........
  r67463 | skip.montanaro | 2008-12-01 02:55:22 +0100 (Mon, 01 Dec 2008) | 1 line
  
  typo in comment
.........
  r67572 | georg.brandl | 2008-12-05 10:23:14 +0100 (Fri, 05 Dec 2008) | 2 lines
  
  #4458: recognize "-" as an argument, not a malformed option in gnu_getopt().
.........
  r67576 | georg.brandl | 2008-12-05 13:09:41 +0100 (Fri, 05 Dec 2008) | 2 lines
  
  #4529: fix parser's validation for try-except-finally statements.
.........
  r67628 | skip.montanaro | 2008-12-07 03:16:00 +0100 (Sun, 07 Dec 2008) | 1 line
  
  muffed the default case
.........



Modified:
   python/branches/release30-maint/Lib/getopt.py
   python/branches/release30-maint/Lib/test/test_getopt.py
   python/branches/release30-maint/Lib/test/test_parser.py
   python/branches/release30-maint/Misc/NEWS
   python/branches/release30-maint/Modules/parsermodule.c
   python/branches/release30-maint/configure.in

Modified: python/branches/release30-maint/Lib/getopt.py
==============================================================================
--- python/branches/release30-maint/Lib/getopt.py	(original)
+++ python/branches/release30-maint/Lib/getopt.py	Sun Dec  7 17:04:03 2008
@@ -130,7 +130,7 @@
 
         if args[0][:2] == '--':
             opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
-        elif args[0][:1] == '-':
+        elif args[0][:1] == '-' and args[0] != '-':
             opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
         else:
             if all_options_first:

Modified: python/branches/release30-maint/Lib/test/test_getopt.py
==============================================================================
--- python/branches/release30-maint/Lib/test/test_getopt.py	(original)
+++ python/branches/release30-maint/Lib/test/test_getopt.py	Sun Dec  7 17:04:03 2008
@@ -124,6 +124,11 @@
         self.assertEqual(opts, [('-a', ''), ('-b', '1'),
                                 ('--alpha', ''), ('--beta', '2')])
 
+        # recognize "-" as an argument
+        opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
+        self.assertEqual(args, ['-'])
+        self.assertEqual(opts, [('-a', ''), ('-b', '-')])
+
         # Posix style via +
         opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
         self.assertEqual(opts, [('-a', '')])

Modified: python/branches/release30-maint/Lib/test/test_parser.py
==============================================================================
--- python/branches/release30-maint/Lib/test/test_parser.py	(original)
+++ python/branches/release30-maint/Lib/test/test_parser.py	Sun Dec  7 17:04:03 2008
@@ -193,6 +193,16 @@
         self.check_suite("with open('x'): pass\n")
         self.check_suite("with open('x') as f: pass\n")
 
+    def test_try_stmt(self):
+        self.check_suite("try: pass\nexcept: pass\n")
+        self.check_suite("try: pass\nfinally: pass\n")
+        self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
+        self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
+                         "finally: pass\n")
+        self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
+        self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
+                         "finally: pass\n")
+
     def test_position(self):
         # An absolutely minimal test of position information.  Better
         # tests would be a big project.

Modified: python/branches/release30-maint/Misc/NEWS
==============================================================================
--- python/branches/release30-maint/Misc/NEWS	(original)
+++ python/branches/release30-maint/Misc/NEWS	Sun Dec  7 17:04:03 2008
@@ -26,6 +26,15 @@
 - Issue #4483: _dbm module now builds on systems with gdbm & gdbm_compat
   libs.
 
+- Issue #4529: fix the parser module's validation of try-except-finally
+  statements.
+
+- Issue #4458: getopt.gnu_getopt() now recognizes a single "-" as an argument,
+  not a malformed option.
+
+- Added the subprocess.check_output() convenience function to get output
+  from a subprocess on success or raise an exception on error.
+
 - Issue #4542: On Windows, binascii.crc32 still accepted str as binary input;
   the corresponding tests now pass.
 

Modified: python/branches/release30-maint/Modules/parsermodule.c
==============================================================================
--- python/branches/release30-maint/Modules/parsermodule.c	(original)
+++ python/branches/release30-maint/Modules/parsermodule.c	Sun Dec  7 17:04:03 2008
@@ -1862,6 +1862,7 @@
 
 /*  try_stmt:
  *      'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
+                                                   ['finally' ':' suite]
  *    | 'try' ':' suite 'finally' ':' suite
  *
  */
@@ -1887,35 +1888,34 @@
         PyErr_Format(parser_error,
                      "Illegal number of children for try/%s node.", name);
     }
-    /*  Skip past except_clause sections:  */
+    /* Handle try/finally statement */
+    if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
+        (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
+        res = (validate_numnodes(tree, 6, "try/finally")
+               && validate_colon(CHILD(tree, 4))
+               && validate_suite(CHILD(tree, 5)));
+        return (res);
+    }
+    /* try/except statement: skip past except_clause sections */
     while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
         res = (validate_except_clause(CHILD(tree, pos))
                && validate_colon(CHILD(tree, pos + 1))
                && validate_suite(CHILD(tree, pos + 2)));
         pos += 3;
     }
-    if (res && (pos < nch)) {
-        res = validate_ntype(CHILD(tree, pos), NAME);
-        if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
-            res = (validate_numnodes(tree, 6, "try/finally")
-                   && validate_colon(CHILD(tree, 4))
-                   && validate_suite(CHILD(tree, 5)));
-        else if (res) {
-            if (nch == (pos + 3)) {
-                res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
-                       || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
-                if (!res)
-                    err_string("illegal trailing triple in try statement");
-            }
-            else if (nch == (pos + 6)) {
-                res = (validate_name(CHILD(tree, pos), "except")
-                       && validate_colon(CHILD(tree, pos + 1))
-                       && validate_suite(CHILD(tree, pos + 2))
-                       && validate_name(CHILD(tree, pos + 3), "else"));
-            }
-            else
-                res = validate_numnodes(tree, pos + 3, "try/except");
-        }
+    /* skip else clause */
+    if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
+        (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
+        res = (validate_colon(CHILD(tree, pos + 1))
+               && validate_suite(CHILD(tree, pos + 2)));
+        pos += 3;
+    }
+    if (res && pos < nch) {
+        /* last clause must be a finally */
+        res = (validate_name(CHILD(tree, pos), "finally")
+               && validate_numnodes(tree, pos + 3, "try/except/finally")
+               && validate_colon(CHILD(tree, pos + 1))
+               && validate_suite(CHILD(tree, pos + 2)));
     }
     return (res);
 }

Modified: python/branches/release30-maint/configure.in
==============================================================================
--- python/branches/release30-maint/configure.in	(original)
+++ python/branches/release30-maint/configure.in	Sun Dec  7 17:04:03 2008
@@ -1770,7 +1770,7 @@
 AC_CHECK_LIB(dl, dlopen)	# Dynamic linking for SunOS/Solaris and SYSV
 AC_CHECK_LIB(dld, shl_load)	# Dynamic linking for HP-UX
 
-# only check for sem_ini if thread support is requested
+# only check for sem_init if thread support is requested
 if test "$with_threads" = "yes" -o -z "$with_threads"; then
     AC_SEARCH_LIBS(sem_init, pthread rt posix4) # 'Real Time' functions on Solaris
 						# posix4 on Solaris 2.6


More information about the Python-checkins mailing list