[Python-checkins] python/dist/src/Lib/compiler ast.py, 1.23, 1.24 pycodegen.py, 1.68, 1.69 symbols.py, 1.15, 1.16 transformer.py, 1.40, 1.41

anthonybaxter at users.sourceforge.net anthonybaxter at users.sourceforge.net
Mon Aug 2 08:10:26 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib/compiler
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6086/Lib/compiler

Modified Files:
	ast.py pycodegen.py symbols.py transformer.py 
Log Message:
PEP-0318, @decorator-style. In Guido's words:
"@ seems the syntax that everybody can hate equally"
Implementation by Mark Russell, from SF #979728.


Index: ast.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/compiler/ast.py,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** ast.py	19 May 2004 08:20:06 -0000	1.23
--- ast.py	2 Aug 2004 06:09:53 -0000	1.24
***************
*** 49,85 ****
      pass
  
! class Slice(Node):
!     nodes["slice"] = "Slice"
!     def __init__(self, expr, flags, lower, upper):
          self.expr = expr
          self.flags = flags
!         self.lower = lower
!         self.upper = upper
  
[...2389 lines suppressed...]
          return tuple(nodelist)
  
      def __repr__(self):
!         return "While(%s, %s, %s)" % (repr(self.test), repr(self.body), repr(self.else_))
  
! class Yield(Node):
!     nodes["yield"] = "Yield"
!     def __init__(self, value):
!         self.value = value
  
      def getChildren(self):
!         return self.value,
  
      def getChildNodes(self):
!         return self.value,
  
      def __repr__(self):
!         return "Yield(%s)" % (repr(self.value),)
  
  klasses = globals()

Index: pycodegen.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/compiler/pycodegen.py,v
retrieving revision 1.68
retrieving revision 1.69
diff -C2 -d -r1.68 -r1.69
*** pycodegen.py	7 Jul 2004 20:54:46 -0000	1.68
--- pycodegen.py	2 Aug 2004 06:09:53 -0000	1.69
***************
*** 367,370 ****
--- 367,377 ----
  
      def _visitFuncOrLambda(self, node, isLambda=0):
+         if not isLambda and node.decorators:
+             for decorator in reversed(node.decorators.nodes):
+                 self.visit(decorator)
+             ndecorators = len(node.decorators.nodes)
+         else:
+             ndecorators = 0
+             
          gen = self.FunctionGen(node, self.scopes, isLambda,
                                 self.class_name, self.get_module())
***************
*** 383,386 ****
--- 390,396 ----
              self.emit('LOAD_CONST', gen)
              self.emit('MAKE_FUNCTION', len(node.defaults))
+             
+         for i in range(ndecorators):
+             self.emit('CALL_FUNCTION', 1)
  
      def visitClass(self, node):

Index: symbols.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/compiler/symbols.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** symbols.py	7 Jul 2004 20:54:47 -0000	1.15
--- symbols.py	2 Aug 2004 06:09:53 -0000	1.16
***************
*** 225,228 ****
--- 225,230 ----
  
      def visitFunction(self, node, parent):
+         if node.decorators:
+             self.visit(node.decorators, parent)
          parent.add_def(node.name)
          for n in node.defaults:

Index: transformer.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/compiler/transformer.py,v
retrieving revision 1.40
retrieving revision 1.41
diff -C2 -d -r1.40 -r1.41
*** transformer.py	12 Jul 2004 13:15:56 -0000	1.40
--- transformer.py	2 Aug 2004 06:09:53 -0000	1.41
***************
*** 186,196 ****
          return Expression(self.com_node(nodelist[0]))
  
      def funcdef(self, nodelist):
!         # funcdef: 'def' NAME parameters ':' suite
          # parameters: '(' [varargslist] ')'
  
!         lineno = nodelist[1][2]
!         name = nodelist[1][1]
!         args = nodelist[2][2]
  
          if args[0] == symbol.varargslist:
--- 186,248 ----
          return Expression(self.com_node(nodelist[0]))
  
+     def decorator_name(self, nodelist):
+         listlen = len(nodelist)
+         assert listlen >= 1 and listlen % 2 == 1
+ 
+         item = self.atom_name(nodelist)
+         i = 1
+         while i < listlen:
+             assert nodelist[i][0] == token.DOT
+             assert nodelist[i + 1][0] == token.NAME
+             item = Getattr(item, nodelist[i + 1][1])
+             i += 2
+ 
+         return item
+         
+     def decorator(self, nodelist):
+         # '@' dotted_name [ '(' [arglist] ')' ]
+         assert len(nodelist) in (2, 4, 5)
+         assert nodelist[0][0] == token.AT
+ 
+         assert nodelist[1][0] == symbol.dotted_name
+         funcname = self.decorator_name(nodelist[1][1:])
+ 
+         if len(nodelist) > 2:
+             assert nodelist[2][0] == token.LPAR
+             expr = self.com_call_function(funcname, nodelist[3])
+         else:
+             expr = funcname
+             
+         return expr
+     
+     def decorators(self, nodelist):
+         # decorators: decorator ([NEWLINE] decorator)* NEWLINE
+         listlen = len(nodelist)
+         i = 0
+         items = []
+         while i < listlen:
+             assert nodelist[i][0] == symbol.decorator
+             items.append(self.decorator(nodelist[i][1:]))
+             i += 1
+ 
+             if i < listlen and nodelist[i][0] == token.NEWLINE:
+                 i += 1
+         return Decorators(items)
+     
      def funcdef(self, nodelist):
!         #                    -6   -5    -4         -3  -2    -1
!         # funcdef: [decorators] 'def' NAME parameters ':' suite
          # parameters: '(' [varargslist] ')'
  
!         if len(nodelist) == 6:
!             assert nodelist[0][0] == symbol.decorators
!             decorators = self.decorators(nodelist[0][1:])
!         else:
!             assert len(nodelist) == 5
!             decorators = None
!             
!         lineno = nodelist[-4][2]
!         name = nodelist[-4][1]
!         args = nodelist[-3][2]
  
          if args[0] == symbol.varargslist:
***************
*** 199,206 ****
              names = defaults = ()
              flags = 0
!         doc = self.get_docstring(nodelist[4])
  
          # code for function
!         code = self.com_node(nodelist[4])
  
          if doc is not None:
--- 251,258 ----
              names = defaults = ()
              flags = 0
!         doc = self.get_docstring(nodelist[-1])
  
          # code for function
!         code = self.com_node(nodelist[-1])
  
          if doc is not None:
***************
*** 208,212 ****
              assert isinstance(code.nodes[0], Discard)
              del code.nodes[0]
!         n = Function(name, names, defaults, flags, doc, code)
          n.lineno = lineno
          return n
--- 260,264 ----
              assert isinstance(code.nodes[0], Discard)
              del code.nodes[0]
!         n = Function(decorators, name, names, defaults, flags, doc, code)
          n.lineno = lineno
          return n



More information about the Python-checkins mailing list