[Checkins] SVN: zope.contenttype/trunk/ LP #242321: fix IndexError raised when testing strings consisting solely of leading whitespace.

Tres Seaver tseaver at palladion.com
Tue Mar 23 21:51:18 EDT 2010


Log message for revision 110128:
  LP #242321:  fix IndexError raised when testing strings consisting solely of leading whitespace.
  

Changed:
  U   zope.contenttype/trunk/CHANGES.txt
  U   zope.contenttype/trunk/src/zope/contenttype/__init__.py
  U   zope.contenttype/trunk/src/zope/contenttype/tests/testContentTypes.py

-=-
Modified: zope.contenttype/trunk/CHANGES.txt
===================================================================
--- zope.contenttype/trunk/CHANGES.txt	2010-03-23 22:54:37 UTC (rev 110127)
+++ zope.contenttype/trunk/CHANGES.txt	2010-03-24 01:51:17 UTC (rev 110128)
@@ -4,6 +4,9 @@
 3.5.1 (unreleased)
 ------------------
 
+* LP #242321:  fix IndexError raised when testing strings consisting
+  solely of leading whitespace.
+
 * Updated mime-type for .js to be application/javascript.
 
 3.5.0 (2009-10-22)

Modified: zope.contenttype/trunk/src/zope/contenttype/__init__.py
===================================================================
--- zope.contenttype/trunk/src/zope/contenttype/__init__.py	2010-03-23 22:54:37 UTC (rev 110127)
+++ zope.contenttype/trunk/src/zope/contenttype/__init__.py	2010-03-24 01:51:17 UTC (rev 110128)
@@ -32,25 +32,21 @@
     such as 'text/html'.
     """
     # at least the maximum length of any tags we look for
-    iMAXLEN=14 
-    if len(s) < iMAXLEN: return 'text/plain'
+    max_tags = 14
+    s = s.strip()[:max_tags]
+    s2 = s.lower()
 
-    i = 0
-    while s[i] in string.whitespace: 
-       i += 1
+    if len(s) == max_tags:
 
-    s2 = s[i : i+iMAXLEN]
-    s = s2.lower()
+        if s2.startswith('<html>'):
+            return 'text/html'
     
-    if s.startswith('<html>'):
-        return 'text/html'
-  
-    if s.startswith('<!doctype html'):
-        return 'text/html'
+        if s2.startswith('<!doctype html'):
+            return 'text/html'
 
-    # what about encodings??
-    if s2.startswith('<?xml'):
-        return 'text/xml'
+        # what about encodings??
+        if s.startswith('<?xml'):
+            return 'text/xml'
     
     return 'text/plain'
  

Modified: zope.contenttype/trunk/src/zope/contenttype/tests/testContentTypes.py
===================================================================
--- zope.contenttype/trunk/src/zope/contenttype/tests/testContentTypes.py	2010-03-23 22:54:37 UTC (rev 110127)
+++ zope.contenttype/trunk/src/zope/contenttype/tests/testContentTypes.py	2010-03-24 01:51:17 UTC (rev 110128)
@@ -67,8 +67,9 @@
         self._check_types_count(2)
 
     def test_text_type(self):
+        HTML = '<HtmL><body>hello world</body></html>'
         from zope.contenttype import text_type
-        self.assertEqual(text_type('<HtmL><body>hello world</body></html>'),
+        self.assertEqual(text_type(HTML),
                          'text/html')
         self.assertEqual(text_type('<?xml version="1.0"><foo/>'),
                          'text/xml')
@@ -80,6 +81,13 @@
                                    '"-//W3C//DTD HTML 4.01 Transitional//EN" '
                                    '"http://www.w3.org/TR/html4/loose.dtd">'),
                          'text/html')
+        # See https://bugs.launchpad.net/bugs/487998
+        self.assertEqual(text_type(' ' * 14 + HTML),
+                         'text/html')
+        self.assertEqual(text_type(' ' * 14 + 'abc'),
+                         'text/plain')
+        self.assertEqual(text_type(' ' * 14),
+                         'text/plain')
 
 
 def test_suite():



More information about the checkins mailing list