# HG changeset patch # User Thierry Florac # Date 1437576944 -7200 # Node ID 138d2a1faf820d745477bb172b6943a81ebea8af # Parent a29ce361f097a0c6b29e55a6fc419778cc1c68e4 Use BytesIO when needed diff -r a29ce361f097 -r 138d2a1faf82 src/pyams_file/file.py --- a/src/pyams_file/file.py Wed Jul 22 16:55:18 2015 +0200 +++ b/src/pyams_file/file.py Wed Jul 22 16:55:44 2015 +0200 @@ -9,8 +9,6 @@ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # -from pyramid.events import subscriber -from zope.lifecycleevent.interfaces import IObjectAddedEvent __docformat__ = 'restructuredtext' @@ -27,7 +25,7 @@ from PIL import Image # import interfaces -from pyams_file.interfaces import IFile, IImage, IVideo, IAudio, IFileInfo, FileModifiedEvent, IFileFieldContainer +from pyams_file.interfaces import IFile, IImage, IVideo, IAudio, IFileInfo, FileModifiedEvent from zope.copy.interfaces import ICopyHook, ResumeCopy from zope.location.interfaces import IContained @@ -145,6 +143,8 @@ def _set_data(self, data): if isinstance(data, str): data = BytesIO(data.encode('utf-8')) + elif isinstance(data, bytes): + data = BytesIO(data) File._set_data(self, data) if hasattr(data, 'seek'): data.seek(0) @@ -197,11 +197,11 @@ def get_magic_content_type(input): """Get content-type based on magic library""" - if hasattr(input, 'seek'): - input.seek(0) - if hasattr(input, 'read'): - input = input.read() if magic is not None: + if hasattr(input, 'seek'): + input.seek(0) + if hasattr(input, 'read'): + input = input.read() return magic.from_buffer(input, mime=True) else: return None