[Zope3-checkins] CVS: Zope3/src/zope/pagetemplate - pagetemplatefile.py:1.6
Fred L. Drake, Jr.
fred@zope.com
Mon, 7 Apr 2003 15:24:37 -0400
Update of /cvs-repository/Zope3/src/zope/pagetemplate
In directory cvs.zope.org:/tmp/cvs-serv572
Modified Files:
pagetemplatefile.py
Log Message:
avoid re-reading more data than necessary
=== Zope3/src/zope/pagetemplate/pagetemplatefile.py 1.5 => 1.6 ===
--- Zope3/src/zope/pagetemplate/pagetemplatefile.py:1.5 Tue Apr 1 14:36:14 2003
+++ Zope3/src/zope/pagetemplate/pagetemplatefile.py Mon Apr 7 15:24:36 2003
@@ -60,15 +60,18 @@
return
f = open(self.filename, "rb")
try:
- text = f.read()
- finally:
+ text = f.read(XML_PREFIX_MAX_LENGTH)
+ except:
f.close()
+ raise
t = sniff_type(text)
- if t != "text/xml" and "\r" in text:
+ if t != "text/xml":
# For HTML, we really want the file read in text mode:
- f = open(self.filename)
- text = f.read()
f.close()
+ f = open(self.filename)
+ text = ''
+ text += f.read()
+ f.close()
self.pt_edit(text, t)
self._cook()
if self._v_errors:
@@ -103,6 +106,8 @@
"\xfe\xff\0<\0?\0x\0m\0l", # utf-16 big endian w/ byte order mark
"\xff\xfe<\0?\0x\0m\0l\0", # utf-16 little endian w/ byte order mark
]
+
+XML_PREFIX_MAX_LENGTH = max(map(len, XML_PREFIXES))
def sniff_type(text):
"""Return 'text/xml' if text appears to be XML, otherwise return None."""