From 0eac29f25559b83a1fc1dc725b5863c853499c7a Mon Sep 17 00:00:00 2001
From: Rocky Burt <rocky@serverzen.com>
Date: Wed, 28 Jun 2006 11:38:18 +0000
Subject: [PATCH] OFS Image: Image and File now both support simple unicode
 objects for data (they function the same as strings for data).

---
 doc/CHANGES.txt                          |  3 +++
 lib/python/OFS/Image.py                  | 13 ++++++-------
 lib/python/OFS/tests/testFileAndImage.py | 11 +++++++++--
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/doc/CHANGES.txt b/doc/CHANGES.txt
index ccff7e499..d4fd25577 100644
--- a/doc/CHANGES.txt
+++ b/doc/CHANGES.txt
@@ -27,6 +27,9 @@ Zope Changes
 
     Bugs Fixed
 
+      - OFS Image: Image and File now both support simple unicode objects
+        for data (they function the same as strings for data).
+
       - Collector #2122: fixed missing is_proxying_match definition
         in ZServer/HTTPServer
 
diff --git a/lib/python/OFS/Image.py b/lib/python/OFS/Image.py
index cec47d781..0c379f938 100644
--- a/lib/python/OFS/Image.py
+++ b/lib/python/OFS/Image.py
@@ -43,7 +43,6 @@ from zExceptions import Redirect
 from cgi import escape
 import transaction
 
-StringType=type('')
 manage_addFileForm=DTMLFile('dtml/imageAdd', globals(),Kind='File',kind='file')
 def manage_addFile(self,id,file='',title='',precondition='', content_type='',
                    REQUEST=None):
@@ -231,7 +230,7 @@ class File(Persistent, Implicit, PropertyManager,
                     RESPONSE.setStatus(206) # Partial content
 
                     data = self.data
-                    if type(data) is StringType:
+                    if isinstance(data, basestring):
                         RESPONSE.write(data[start:end])
                         return True
 
@@ -302,7 +301,7 @@ class File(Persistent, Implicit, PropertyManager,
                             'Content-Range: bytes %d-%d/%d\r\n\r\n' % (
                                 start, end - 1, self.size))
 
-                        if type(data) is StringType:
+                        if isinstance(data, basestring):
                             RESPONSE.write(data[start:end])
 
                         else:
@@ -401,7 +400,7 @@ class File(Persistent, Implicit, PropertyManager,
         self.ZCacheable_set(None)
 
         data=self.data
-        if type(data) is type(''):
+        if isinstance(data, basestring):
             RESPONSE.setBase(None)
             return data
 
@@ -481,7 +480,7 @@ class File(Persistent, Implicit, PropertyManager,
         if headers and headers.has_key('content-type'):
             content_type=headers['content-type']
         else:
-            if type(body) is not type(''): body=body.data
+            if not isinstance(body, basestring): body=body.data
             content_type, enc=guess_content_type(
                 getattr(file, 'filename',id), body, content_type)
         return content_type
@@ -490,7 +489,7 @@ class File(Persistent, Implicit, PropertyManager,
 
         n=1 << 16
 
-        if type(file) is StringType:
+        if isinstance(file, basestring):
             size=len(file)
             if size < n: return file, size
             # Big string: cut it into smaller chunks
@@ -617,7 +616,7 @@ class File(Persistent, Implicit, PropertyManager,
                 return result
 
         data = self.data
-        if type(data) is type(''):
+        if isinstance(data, basestring):
             RESPONSE.setBase(None)
             return data
 
diff --git a/lib/python/OFS/tests/testFileAndImage.py b/lib/python/OFS/tests/testFileAndImage.py
index c8d5a4a1b..7b1c19380 100644
--- a/lib/python/OFS/tests/testFileAndImage.py
+++ b/lib/python/OFS/tests/testFileAndImage.py
@@ -252,7 +252,15 @@ class FileTests(unittest.TestCase):
         verifyClass(HTTPRangeInterface, File)
         verifyClass(WriteLockInterface, File)
 
-
+    def testUnicodeWithIndexHtml(self):
+        # Introduced to help test the fact that Image.py has been
+        # changed to be lenient towards any basestring type, not just str
+        
+        val = u'some unicode string here'
+        self.file.manage_edit('foobar', 'text/plain', filedata=val)
+        s = self.file.index_html(self.app.REQUEST, self.app.REQUEST.RESPONSE)
+        self.assertEquals(s, val)
+        
 class ImageTests(FileTests):
     data = open(filedata, 'rb').read()
     content_type = 'image/gif'
@@ -285,7 +293,6 @@ class ImageTests(FileTests):
 
         verifyClass(WriteLockInterface, Image)
 
-
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(FileTests),
-- 
2.30.9