SVN: r25524 - trunk/quixote

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

Modified:
   trunk/quixote/ptl_compile.py
   trunk/quixote/ptl_import.py
Log:
Compile .ptl files to .pyc files.  Also, remove 'htmltext' and
'TemplateIO' from the globals of .ptl modules.


Modified: trunk/quixote/ptl_compile.py
===================================================================
--- trunk/quixote/ptl_compile.py	2004-11-04 20:10:12 UTC (rev 25523)
+++ trunk/quixote/ptl_compile.py	2004-11-04 20:10:54 UTC (rev 25524)
@@ -23,6 +23,10 @@
 import token
 import parser
 import re
+import imp
+import stat
+import marshal
+import struct
 
 assert sys.hexversion >= 0x20000b1, 'PTL requires Python 2.0 or newer'
 
@@ -36,6 +40,7 @@
 # magic names inserted into the code
 IO_MODULE = "quixote.html"
 IO_CLASS = "TemplateIO"
+IO_MANGLED_CLASS = "_q_TemplateIO"
 IO_INSTANCE = "_q_output"
 HTML_TEMPLATE_PREFIX = "_q_html_template_"
 PLAIN_TEMPLATE_PREFIX = "_q_plain_template_"
@@ -55,19 +60,17 @@
         # beginning of the module.
         doc = None # self.get_docstring(nodelist, symbol.file_input)
 
-        io_imp = ast.From(IO_MODULE, [(IO_CLASS, None)])
-        markup_imp = ast.From(MARKUP_MODULE, [(MARKUP_CLASS, None)])
-        markup_assign = ast.Assign([ast.AssName(MARKUP_MANGLED_CLASS,
-                                                OP_ASSIGN)],
-                                   ast.Name(MARKUP_CLASS))
+        io_imp = ast.From(IO_MODULE, [(IO_CLASS, IO_MANGLED_CLASS)])
+        markup_imp = ast.From(MARKUP_MODULE,
+                              [(MARKUP_CLASS, MARKUP_MANGLED_CLASS)])
 
         # Add an IO_INSTANCE binding for module level expressions (like
         # doc strings).  This instance will not be returned.
-        io_instance = ast.CallFunc(ast.Name(IO_CLASS), [])
+        io_instance = ast.CallFunc(ast.Name(IO_MANGLED_CLASS), [])
         io_assign_name = ast.AssName(IO_INSTANCE, OP_ASSIGN)
         io_assign = ast.Assign([io_assign_name], io_instance)
 
-        stmts = [ io_imp, io_assign, markup_imp, markup_assign ]
+        stmts = [ io_imp, io_assign, markup_imp ]
 
         for node in nodelist:
             if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
@@ -120,7 +123,7 @@
             code = self.com_node(nodelist[-1])
 
             # create an instance, assign to IO_INSTANCE
-            klass = ast.Name(IO_CLASS)
+            klass = ast.Name(IO_MANGLED_CLASS)
             args = [ast.Const(template_type == "html")]
             instance = ast.CallFunc(klass, args)
             assign_name = ast.AssName(IO_INSTANCE, OP_ASSIGN)
@@ -249,17 +252,6 @@
 
 
 PTL_EXT = ".ptl"
-PTLC_EXT = ".ptlc"
-if sys.hexversion >= 0x20300a1:
-    PTLC_MAGIC = "PTLC\x04\x00"
-elif sys.hexversion >= 0x20200b1:
-    PTLC_MAGIC = "PTLC\x03\x00"
-elif sys.hexversion >= 0x20100b1:
-    PTLC_MAGIC = "PTLC\x02\x00"
-elif sys.hexversion >= 0x20000b1:
-    PTLC_MAGIC = "PTLC\x01\x00"
-else:
-    raise RuntimeError, 'python too old'
 
 class Template(pycodegen.Module):
 
@@ -276,18 +268,18 @@
             walk(ast, gen, 1)
             self.code = gen.getCode()
 
-    def dump(self, f):
-        import marshal
-        import stat
-
-        f.write(PTLC_MAGIC)
+    def dump(self, fp):
         mtime = os.stat(self.filename)[stat.ST_MTIME]
-        marshal.dump(mtime, f)
-        marshal.dump(self.code, f)
+        fp.write('\0\0\0\0')
+        fp.write(struct.pack('<I', mtime))
+        marshal.dump(self.code, fp)
+        fp.flush()
+        fp.seek(0)
+        fp.write(imp.get_magic())
 
 
 def compile_template(input, filename, output=None):
-    """compile_template(input, filename, output=None) -> code
+    """(input, filename, output=None) -> code
 
     Compile an open file.  If output is not None then the code is written
     with the magic template header.  The code object is returned.
@@ -295,7 +287,7 @@
     buf = input.read()
     template = Template(buf, filename)
     template.compile()
-    if output:
+    if output is not None:
         template.dump(output)
     return template.code
 
@@ -373,7 +365,7 @@
     else:
         for filename in args:
             path, ext = os.path.splitext(filename)
-            compile(filename, path + PTLC_EXT)
+            compile(filename, path + ".pyc")
 
 if __name__ == "__main__":
     main()

Modified: trunk/quixote/ptl_import.py
===================================================================
--- trunk/quixote/ptl_import.py	2004-11-04 20:10:12 UTC (rev 25523)
+++ trunk/quixote/ptl_import.py	2004-11-04 20:10:54 UTC (rev 25524)
@@ -16,11 +16,9 @@
 import sys
 import os.path
 import imp, ihooks, new
-import marshal
-import stat
 import __builtin__
 
-from ptl_compile import compile_template, PTL_EXT, PTLC_EXT, PTLC_MAGIC
+from ptl_compile import compile_template, PTL_EXT
 
 assert sys.hexversion >= 0x20000b1, "need Python 2.0b1 or later"
 
@@ -35,27 +33,6 @@
     exec code in mod.__dict__
     return mod
 
-def _load_ptlc(name, filename, file=None):
-    if not file:
-        try:
-            file = open(filename, "rb")
-        except IOError:
-            return None
-    path, ext = os.path.splitext(filename)
-    ptl_filename = path + PTL_EXT
-    magic = file.read(len(PTLC_MAGIC))
-    if magic != PTLC_MAGIC:
-        return _load_ptl(name, ptl_filename)
-    ptlc_mtime = marshal.load(file)
-    try:
-        mtime = os.stat(ptl_filename)[stat.ST_MTIME]
-    except OSError:
-        mtime = ptlc_mtime
-    if mtime > ptlc_mtime:
-        return _load_ptl(name, ptl_filename)
-    code = marshal.load(file)
-    return _exec_module_code(code, name, filename)
-
 def _load_ptl(name, filename, file=None):
     if not file:
         try:
@@ -63,18 +40,17 @@
         except IOError:
             return None
     path, ext = os.path.splitext(filename)
-    ptlc_filename = path + PTLC_EXT
+    compiled_filename = path + ".pyc"
     try:
-        output = open(ptlc_filename, "wb")
-    except IOError, msg:
+        output = open(compiled_filename, "wb")
+    except IOError:
         output = None
     try:
         code = compile_template(file, filename, output)
     except:
-        # don't leave a corrupt .ptlc file around
         if output:
             output.close()
-            os.unlink(ptlc_filename)
+            os.unlink(compiled_filename)
         raise
     else:
         if output:
@@ -83,15 +59,13 @@
 
 
 # Constant used to signal a PTL files
-PTLC_FILE = 128
-PTL_FILE = 129
+PTL_FILE = object()
 
 class PTLHooks(ihooks.Hooks):
 
     def get_suffixes(self):
         # add our suffixes
-        L = imp.get_suffixes()
-        return L + [(PTLC_EXT, 'rb', PTLC_FILE), (PTL_EXT, 'r', PTL_FILE)]
+        return [(PTL_EXT, 'r', PTL_FILE)] + imp.get_suffixes()
 
 class PTLLoader(ihooks.ModuleLoader):
 
@@ -100,10 +74,7 @@
         (suff, mode, type) = info
 
         # If it's a PTL file, load it specially.
-        if type == PTLC_FILE:
-            return _load_ptlc(name, filename, file)
-
-        elif type == PTL_FILE:
+        if type is PTL_FILE:
             return _load_ptl(name, filename, file)
 
         else:
@@ -152,5 +123,4 @@
 
 
 if __name__ == '__main__':
-    import ZODB
     install()