[Checkins] SVN: z3c.pt/trunk/ Added some code in a filecache module that can later be used to write out and reload the compiled Python code to and from the file system. We should be able to avoid reparsing on Python process restart.

Hanno Schlichting plone at hannosch.info
Sun Jun 15 16:39:28 EDT 2008


Log message for revision 87414:
  Added some code in a filecache module that can later be used to write out and reload the compiled Python code to and from the file system. We should be able to avoid reparsing on Python process restart.
  

Changed:
  U   z3c.pt/trunk/docs/HISTORY.txt
  A   z3c.pt/trunk/src/z3c/pt/filecache.py

-=-
Modified: z3c.pt/trunk/docs/HISTORY.txt
===================================================================
--- z3c.pt/trunk/docs/HISTORY.txt	2008-06-15 18:18:40 UTC (rev 87413)
+++ z3c.pt/trunk/docs/HISTORY.txt	2008-06-15 20:39:26 UTC (rev 87414)
@@ -4,6 +4,10 @@
 Version 0.8.x
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Added some code in a filecache module that can later be used to write out
+  and reload the compiled Python code to and from the file system. We should
+  be able to avoid reparsing on Python process restart.
+
 - Simplified the generated _escape code. cgi.escape's second argument is a
   simple boolean and not a list of characters to quote.
 

Added: z3c.pt/trunk/src/z3c/pt/filecache.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/filecache.py	                        (rev 0)
+++ z3c.pt/trunk/src/z3c/pt/filecache.py	2008-06-15 20:39:26 UTC (rev 87414)
@@ -0,0 +1,37 @@
+import os
+import marshal
+import py_compile
+import struct
+
+from imp import get_magic
+
+MAGIC = get_magic()
+
+def code_read(self, filename, timestamp):
+    code = None
+    if os.path.isfile(self.code_filename):
+        fd = open(filename, 'rb')
+        if fd.read(4) == MAGIC:
+            ctimestamp = struct.unpack('<I', fd.read(4))[0]
+            if ctimestamp == timestamp:
+                code = marshal.load(fd)
+        fd.close()
+    return code
+
+def code_write(self, source, filename, timestamp):
+    try:
+        fd = open(filename, 'wb')
+        try:
+            code = compile(source, filename, "exec")
+            # Create a byte code file. See the py_compile module
+            fd.write('\0\0\0\0')
+            fd.write(struct.pack("<I", timestamp))
+            marshal.dump(code, fd)
+            fd.flush()
+            fd.seek(0, 0)
+            fd.write(MAGIC)
+        finally:
+            fd.close()
+        py_compile.set_creator_type(filename)
+    except (IOError, OSError):
+        pass


Property changes on: z3c.pt/trunk/src/z3c/pt/filecache.py
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list