Commit a11790a3 authored by Rocky Burt's avatar Rocky Burt

OFS Image: Image and File updated to use isinstance(data, str) and raises...

OFS Image: Image and File updated to use isinstance(data, str) and raises TypeError upon encountering unicode objects.
parent 513bc037
...@@ -18,8 +18,8 @@ Zope Changes ...@@ -18,8 +18,8 @@ Zope Changes
Bugs Fixed Bugs Fixed
- OFS Image: Image and File now both support simple unicode objects - OFS Image: Image and File updated to use isinstance(data, str)
for data (they function the same as strings for data). and raises TypeError upon encountering unicode objects.
- OFS Application: Updated deprecation warnings. - OFS Application: Updated deprecation warnings.
Support for '__ac_permissions__' and 'meta_types' will be removed in Support for '__ac_permissions__' and 'meta_types' will be removed in
......
...@@ -230,7 +230,7 @@ class File(Persistent, Implicit, PropertyManager, ...@@ -230,7 +230,7 @@ class File(Persistent, Implicit, PropertyManager,
RESPONSE.setStatus(206) # Partial content RESPONSE.setStatus(206) # Partial content
data = self.data data = self.data
if isinstance(data, basestring): if isinstance(data, str):
RESPONSE.write(data[start:end]) RESPONSE.write(data[start:end])
return True return True
...@@ -301,7 +301,7 @@ class File(Persistent, Implicit, PropertyManager, ...@@ -301,7 +301,7 @@ class File(Persistent, Implicit, PropertyManager,
'Content-Range: bytes %d-%d/%d\r\n\r\n' % ( 'Content-Range: bytes %d-%d/%d\r\n\r\n' % (
start, end - 1, self.size)) start, end - 1, self.size))
if isinstance(data, basestring): if isinstance(data, str):
RESPONSE.write(data[start:end]) RESPONSE.write(data[start:end])
else: else:
...@@ -400,7 +400,7 @@ class File(Persistent, Implicit, PropertyManager, ...@@ -400,7 +400,7 @@ class File(Persistent, Implicit, PropertyManager,
self.ZCacheable_set(None) self.ZCacheable_set(None)
data=self.data data=self.data
if isinstance(data, basestring): if isinstance(data, str):
RESPONSE.setBase(None) RESPONSE.setBase(None)
return data return data
...@@ -427,6 +427,10 @@ class File(Persistent, Implicit, PropertyManager, ...@@ -427,6 +427,10 @@ class File(Persistent, Implicit, PropertyManager,
security.declarePrivate('update_data') security.declarePrivate('update_data')
def update_data(self, data, content_type=None, size=None): def update_data(self, data, content_type=None, size=None):
if isinstance(data, unicode):
raise TypeError('Data can only be str or file-like. '
'Unicode objects are expressly forbidden.')
if content_type is not None: self.content_type=content_type if content_type is not None: self.content_type=content_type
if size is None: size=len(data) if size is None: size=len(data)
self.size=size self.size=size
...@@ -480,7 +484,7 @@ class File(Persistent, Implicit, PropertyManager, ...@@ -480,7 +484,7 @@ class File(Persistent, Implicit, PropertyManager,
if headers and headers.has_key('content-type'): if headers and headers.has_key('content-type'):
content_type=headers['content-type'] content_type=headers['content-type']
else: else:
if not isinstance(body, basestring): body=body.data if not isinstance(body, str): body=body.data
content_type, enc=guess_content_type( content_type, enc=guess_content_type(
getattr(file, 'filename',id), body, content_type) getattr(file, 'filename',id), body, content_type)
return content_type return content_type
...@@ -489,7 +493,7 @@ class File(Persistent, Implicit, PropertyManager, ...@@ -489,7 +493,7 @@ class File(Persistent, Implicit, PropertyManager,
n=1 << 16 n=1 << 16
if isinstance(file, basestring): if isinstance(file, str):
size=len(file) size=len(file)
if size < n: return file, size if size < n: return file, size
# Big string: cut it into smaller chunks # Big string: cut it into smaller chunks
...@@ -616,7 +620,7 @@ class File(Persistent, Implicit, PropertyManager, ...@@ -616,7 +620,7 @@ class File(Persistent, Implicit, PropertyManager,
return result return result
data = self.data data = self.data
if isinstance(data, basestring): if isinstance(data, str):
RESPONSE.setBase(None) RESPONSE.setBase(None)
return data return data
...@@ -776,6 +780,10 @@ class Image(File): ...@@ -776,6 +780,10 @@ class Image(File):
security.declarePrivate('update_data') security.declarePrivate('update_data')
def update_data(self, data, content_type=None, size=None): def update_data(self, data, content_type=None, size=None):
if isinstance(data, unicode):
raise TypeError('Data can only be str or file-like. '
'Unicode objects are expressly forbidden.')
if size is None: size=len(data) if size is None: size=len(data)
self.size=size self.size=size
......
...@@ -252,14 +252,11 @@ class FileTests(unittest.TestCase): ...@@ -252,14 +252,11 @@ class FileTests(unittest.TestCase):
verifyClass(HTTPRangeInterface, File) verifyClass(HTTPRangeInterface, File)
verifyClass(WriteLockInterface, File) verifyClass(WriteLockInterface, File)
def testUnicodeWithIndexHtml(self): def testUnicode(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' 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.assertRaises(TypeError, self.file.manage_edit,
self.assertEquals(s, val) 'foobar', 'text/plain', filedata=val)
class ImageTests(FileTests): class ImageTests(FileTests):
data = open(filedata, 'rb').read() data = open(filedata, 'rb').read()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment