]> granicus.if.org Git - python/commitdiff
Flush out support for ``class B(): pass`` syntax by adding support to the
authorBrett Cannon <bcannon@gmail.com>
Sat, 9 Apr 2005 02:30:16 +0000 (02:30 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sat, 9 Apr 2005 02:30:16 +0000 (02:30 +0000)
'parser' module and 'compiler' package.

Closes patch #1176012.  Thanks logistix.

Lib/compiler/transformer.py
Lib/test/test_compiler.py
Lib/test/test_parser.py
Misc/NEWS
Modules/parsermodule.c

index 0c6d1483f4b2a19e98cdf1fcd5316e6369997d70..dfa25b8425c73c25aa7950fcc04e09170e457fc0 100644 (file)
@@ -280,12 +280,14 @@ class Transformer:
         return Lambda(names, defaults, flags, code, lineno=nodelist[1][2])
 
     def classdef(self, nodelist):
-        # classdef: 'class' NAME ['(' testlist ')'] ':' suite
+        # classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
 
         name = nodelist[1][1]
         doc = self.get_docstring(nodelist[-1])
         if nodelist[2][0] == token.COLON:
             bases = []
+        elif nodelist[3][0] == token.RPAR:
+            bases = []
         else:
             bases = self.com_bases(nodelist[3])
 
index 9976a47cf381f8b885a66ccc60f8ffa5098fefb9..d9a3cb8ee911701ec54408437df275da110da470 100644 (file)
@@ -33,6 +33,9 @@ class CompilerTest(unittest.TestCase):
                 else:
                     compiler.compile(buf, basename, "exec")
 
+    def testNewClassSyntax(self):
+        compiler.compile("class foo():pass\n\n","<string>","exec")
+        
     def testLineNo(self):
         # Test that all nodes except Module have a correct lineno attribute.
         filename = __file__
index 0f8c1d0d0ab6b131dda06d575f20cf5af5d9725b..bd81aca2170693e071d0f2eca475e4f5f10fc665 100644 (file)
@@ -127,6 +127,9 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
         self.check_suite("@funcattrs()\n"
                          "def f(): pass")
 
+    def test_class_defs(self):
+        self.check_suite("class foo():pass")
+        
     def test_import_from_statement(self):
         self.check_suite("from sys.path import *")
         self.check_suite("from sys.path import dirname")
index e891cefbb0fbbec0932c470c94277111bd1ac467..ae0216117fe5e8f77872083713ea8674a9713541 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,7 +24,9 @@ Core and builtins
 - Added two new builtins, any() and all().
 
 - Defining a class with empty parentheses is now allowed
-  (e.g., ``class C(): pass`` is no longer a syntax error)
+  (e.g., ``class C(): pass`` is no longer a syntax error).
+  Patch #1176012 added support to the 'parser' module and 'compiler' package
+  (thanks to logistix for that added support).
 
 - Patch #1115086: Support PY_LONGLONG in structmember.
 
index eb23b585724f611e35a625f192bb6dfe4bd5a672..63b2cd784f42617f021542031d32e98e7fd61467 100644 (file)
@@ -947,7 +947,8 @@ static int
 validate_class(node *tree)
 {
     int nch = NCH(tree);
-    int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
+    int res = (validate_ntype(tree, classdef) &&
+               ((nch == 4) || (nch == 6) || (nch == 7)));
 
     if (res) {
         res = (validate_name(CHILD(tree, 0), "class")
@@ -955,12 +956,20 @@ validate_class(node *tree)
                && validate_colon(CHILD(tree, nch - 2))
                && validate_suite(CHILD(tree, nch - 1)));
     }
-    else
+    else {
         (void) validate_numnodes(tree, 4, "class");
-    if (res && (nch == 7)) {
-        res = (validate_lparen(CHILD(tree, 2))
-               && validate_testlist(CHILD(tree, 3))
-               && validate_rparen(CHILD(tree, 4)));
+    }
+       
+    if (res) {
+       if (nch == 7) {
+               res = ((validate_lparen(CHILD(tree, 2)) &&
+                       validate_testlist(CHILD(tree, 3)) &&
+                       validate_rparen(CHILD(tree, 4))));
+       }
+       else if (nch == 6) {
+               res = (validate_lparen(CHILD(tree,2)) &&
+                       validate_rparen(CHILD(tree,3)));
+       }
     }
     return (res);
 }