SVN: r23389 - trunk/quixote

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Tue, 03 Feb 2004 11:04:33 -0500
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-02-03 11:04:33 -0500 (Tue, 03 Feb 2004)
New Revision: 23389

Modified:
   trunk/quixote/ptl_compile.py
Log:
Use a simpler version of parse() if using Python >= 2.3.  Unfortunately,
the hacks are still needed for older versions.


Modified: trunk/quixote/ptl_compile.py
===================================================================
--- trunk/quixote/ptl_compile.py	2004-02-03 00:14:30 UTC (rev 23388)
+++ trunk/quixote/ptl_compile.py	2004-02-03 16:04:33 UTC (rev 23389)
@@ -211,16 +211,30 @@
     return buf
 
 
-def parse(buf, filename='<string>'):
-    buf = translate_tokens(buf, filename)
-    try:
+if sys.hexversion >= 0x20300b1:
+    def parse(buf, filename='<string>'):
+        buf = translate_tokens(buf, filename)
         return TemplateTransformer().parsesuite(buf)
-    except parser.ParserError, value:
-        raise parser.ParserError(str(value), (filename, value.lineno, value.offset, value.text))
-    except SyntaxError, value:
-        raise SyntaxError(str(value), (filename, value.lineno, value.offset, value.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, filename)
+        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 (otherwise
+                # it is often "<string>", even though we specify it).
+                raise SyntaxError(str(exc), (filename, exc.lineno, exc.offset,
+                                             exc.text))
 
+
 PTL_EXT = ".ptl"
 PTLC_EXT = ".ptlc"
 if sys.hexversion >= 0x20300a1: