SVN: r25526 - trunk/quixote

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Thu, 4 Nov 2004 15:29:57 -0500
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-11-04 15:29:37 -0500 (Thu, 04 Nov 2004)
New Revision: 25526

Modified:
   trunk/quixote/ptl_compile.py
Log:
Remove support for old style templates and support for versions of
Python older than 2.3.


Modified: trunk/quixote/ptl_compile.py
===================================================================
--- trunk/quixote/ptl_compile.py	2004-11-04 20:12:28 UTC (rev 25525)
+++ trunk/quixote/ptl_compile.py	2004-11-04 20:29:37 UTC (rev 25526)
@@ -5,17 +5,13 @@
 """
 Compile a PTL template.
 
-First the tokens "template" are replaced with "def".  Next, the file is
-parsed into a parse tree.  This tree is converted into a modified AST.
-It is during this state that the semantics are modified by adding extra
-nodes to the tree.  Finally bytecode is generated using the compiler
-package.
-
-Note that script/module requires the compiler package.
+First template function names are mangled, noting the template type.
+Next, the file is parsed into a parse tree.  This tree is converted into
+a modified AST.  It is during this state that the semantics are modified
+by adding extra nodes to the tree.  Finally bytecode is generated using
+the compiler package.
 """
 
-__revision__ = "$Id$"
-
 import sys
 import os
 import stat
@@ -28,13 +24,12 @@
 import marshal
 import struct
 
-assert sys.hexversion >= 0x20000b1, 'PTL requires Python 2.0 or newer'
+assert sys.hexversion >= 0x20300b1, 'PTL requires Python 2.3 or newer'
 
 from compiler import pycodegen, transformer, walk
 from compiler import ast
 from compiler.consts import OP_ASSIGN
-if sys.hexversion >= 0x20200b1:
-    from compiler import misc, syntax
+from compiler import misc, syntax
 
 
 # magic names inserted into the code
@@ -182,11 +177,6 @@
         return n
 
 
-_old_template_re = re.compile(r"^([ \t]*) template ([ \t]+)"
-                              r" ([a-zA-Z_][a-zA-Z_0-9]*)"   # name of template
-                              r" ([ \t]*[\(\\])",
-                              re.MULTILINE|re.VERBOSE)
-
 _template_re = re.compile(r"^([ \t]*) def (?:[ \t]+)"                # def
                           r" ([a-zA-Z_][a-zA-Z_0-9]*)"               # <name>
                           r" (?:[ \t]*) \[(plain|html)\] (?:[ \t]*)" # <type>
@@ -199,8 +189,6 @@
     must do token translation here.  Luckily it does not affect line
     numbers.
 
-    template foo(...): -> def _q_template__foo(...):
-
     def foo [plain] (...): -> def _q_plain_template__foo(...):
 
     def foo [html] (...): -> def _q_html_template__foo(...):
@@ -208,65 +196,27 @@
     XXX This parser is too stupid.  For example, it doesn't understand
     triple quoted strings.
     """
-    global _template_re
+    return _template_re.sub(r"\1def _q_\3_template_\2(", buf)
 
-    # handle new style template declarations
-    buf = _template_re.sub(r"\1def _q_\3_template_\2(", buf)
 
-    # change old style template to def
-    buf = _old_template_re.sub(r"\1def\2%s\3\4" % TEMPLATE_PREFIX, buf)
+def parse(buf, filename='<string>'):
+    buf = translate_tokens(buf)
+    try:
+        return TemplateTransformer().parsesuite(buf)
+    except SyntaxError, e:
+        # set the filename attribute
+        raise SyntaxError(str(e), (filename, e.lineno, e.offset, e.text))
 
-    return buf
 
-
-if sys.hexversion >= 0x20300b1:
-    def parse(buf, filename='<string>'):
-        buf = translate_tokens(buf)
-        try:
-            return TemplateTransformer().parsesuite(buf)
-        except SyntaxError, e:
-            # set the filename attribute
-            raise SyntaxError(str(e), (filename, e.lineno, e.offset, e.text))
-
-else:
-    # The parser module in Python <= 2.2 can raise ParserError.  Since
-    # the ParserError exception is basically useless, we use compile()
-    # to generate a better exception.
-    def parse(buf, filename='<string>'):
-        buf = translate_tokens(buf)
-        # compile() and parsermodule don't accept code that is missing a
-        # trailing newline.  The Python interpreter seems to add a newline when
-        # importing modules so we match that behavior.
-        if buf[-1:] != '\n':
-            buf += "\n"
-        try:
-            return TemplateTransformer().parsesuite(buf)
-        except (parser.ParserError, SyntaxError):
-            import __builtin__
-            try:
-                __builtin__.compile(buf, filename, 'exec')
-            except SyntaxError, exc:
-                # Another hack to fix the filename attribute.
-                raise SyntaxError(str(exc), (filename, exc.lineno, exc.offset,
-                                             exc.text))
-
-
 PTL_EXT = ".ptl"
 
 class Template(pycodegen.Module):
 
-    if sys.hexversion >= 0x20200b1:
-        def _get_tree(self):
-            tree = parse(self.source, self.filename)
-            misc.set_filename(self.filename, tree)
-            syntax.check(tree)
-            return tree
-    else:
-        def compile(self):
-            ast = parse(self.source, self.filename)
-            gen = pycodegen.ModuleCodeGenerator(self.filename)
-            walk(ast, gen, 1)
-            self.code = gen.getCode()
+    def _get_tree(self):
+        tree = parse(self.source, self.filename)
+        misc.set_filename(self.filename, tree)
+        syntax.check(tree)
+        return tree
 
     def dump(self, fp):
         mtime = os.stat(self.filename)[stat.ST_MTIME]