Skip to content

Commit

Permalink
make FSImage.__str__ available in Python 3 (#282)
Browse files Browse the repository at this point in the history
* make FSImage.__str__ available in Python 3

In both Python 2 and 3, the tag() method returns the respective str type, so
the result can always be used for __str__. Without having this method
available in Python 3, the image tag cannot be included, for example, in
Chamaeleon page templates by simply accessing the image object, as that uses
i18n which in turn casts the object to the respective text (unicode) type. Not
implementing __str__ in Python 3 results in the object representation instead
of the tag in that case.
  • Loading branch information
tlotze authored and Michael Howitz committed May 18, 2018
1 parent 52a0a3d commit c5433f2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Bugfixes
- Accept bytes and text as cookie value.
(`#263 <https://github.com/zopefoundation/Zope/pull/263>`_)

- ``__str__`` of an Image object now returns the image HTML tag in
Python 3 as it already did on Python 2.
(`#282 <https://github.com/zopefoundation/Zope/pull/282>`_)


4.0b4 (2018-04-23)
------------------
Expand Down
5 changes: 2 additions & 3 deletions src/OFS/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,9 +876,8 @@ def update_data(self, data, content_type=None, size=None):
def __bytes__(self):
return self.tag().encode('utf-8')

if PY2:
def __str__(self):
return self.tag()
def __str__(self):
return self.tag()

security.declareProtected(View, 'tag')
def tag(self, height=None, width=None, alt=None,
Expand Down
7 changes: 7 additions & 0 deletions src/OFS/tests/testFileAndImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from zope.lifecycleevent.interfaces import IObjectModifiedEvent
from zope.lifecycleevent.interfaces import IObjectCreatedEvent

import six

here = os.path.dirname(os.path.abspath(__file__))
filedata = os.path.join(here, 'test.gif')

Expand Down Expand Up @@ -328,3 +330,8 @@ def test_interfaces(self):
from OFS.interfaces import IWriteLock

verifyClass(IWriteLock, Image)

def test_text_representation_is_tag(self):
self.assertEqual(six.text_type(self.file),
'<img src="http://nohost/file"'
' alt="" title="" height="16" width="16" />')

0 comments on commit c5433f2

Please sign in to comment.