Skip to content

Commit

Permalink
field2* converters should not handle files (#963)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Howitz <mh@gocept.com>
  • Loading branch information
jugmac00 and Michael Howitz authored May 6, 2021
1 parent 9dc8033 commit f3bfc64
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
11 changes: 9 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ The change log for the previous version, Zope 4, is at
https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst


5.1.3 (unreleased)
------------------
5.2 (unreleased)
----------------

- Updated/fixed the poll application tutorial in the Zope Developers Guide
(`#958 <https://github.com/zopefoundation/Zope/issues/958>`_)
Expand All @@ -20,6 +20,13 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
``rfc850_date``, and ``rfc1123_date`` which used to be in ``App.Common``
keeping backwards-compatibility imports in place.

Backwards incompatible changes
++++++++++++++++++++++++++++++

- With the exception of ``field2bytes``, field converters do no longer try to
read file like objects
(`#558 <https://github.com/zopefoundation/Zope/issues/558>`_)


5.1.2 (2021-03-02)
------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _read_file(filename):
README = _read_file('README.rst')
CHANGES = _read_file('CHANGES.rst')

version = '5.1.3.dev0'
version = '5.2.dev0'


setup(
Expand Down
15 changes: 3 additions & 12 deletions src/ZPublisher/Converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@


def field2string(v):
"""Converts value to native strings.
So always to `str` no matter which Python version you are on.
"""
if hasattr(v, 'read'):
return v.read()
elif isinstance(v, bytes):
"""Converts value to string."""
if isinstance(v, bytes):
return v.decode(default_encoding)
else:
return str(v)


def field2bytes(v):
# Converts value to bytes.
"""Converts value to bytes."""
if hasattr(v, 'read'):
return v.read()
elif isinstance(v, str):
Expand Down Expand Up @@ -172,8 +167,6 @@ def __call__(self, v):
# <input name="description:utf8:ustring" .....
# rather than
# <input name="description:ustring" .....
if hasattr(v, 'read'):
v = v.read()
v = str(v)
return self.convert_unicode(v)

Expand Down Expand Up @@ -209,8 +202,6 @@ def convert_unicode(self, v):

class field2ulines:
def __call__(self, v):
if hasattr(v, 'read'):
v = v.read()
if isinstance(v, (list, tuple)):
return [field2ustring(x) for x in v]
v = str(v)
Expand Down
18 changes: 9 additions & 9 deletions src/ZPublisher/tests/test_Converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,6 @@ def test_field2string_with_string(self):
to_convert = 'to_convert'
self.assertEqual(field2string(to_convert), to_convert)

def test_field2string_with_filelike_object(self):
from ZPublisher.Converters import field2string
to_convert = 'to_convert'

class Filelike:
def read(self):
return to_convert
self.assertEqual(field2string(Filelike()), to_convert)

def test_field2bytes_with_bytes(self):
from ZPublisher.Converters import field2bytes
to_convert = b'to_convert'
Expand All @@ -163,6 +154,15 @@ def test_field2bytes_with_text(self):
expected = b'to_convert'
self.assertEqual(field2bytes(to_convert), expected)

def test_field2bytes_with_filelike_object(self):
from ZPublisher.Converters import field2bytes
to_convert = b'to_convert'

class Filelike:
def read(self):
return to_convert
self.assertEqual(field2bytes(Filelike()), to_convert)

def test_field2date_international_with_proper_date_string(self):
from ZPublisher.Converters import field2date_international
to_convert = "2.1.2019"
Expand Down

0 comments on commit f3bfc64

Please sign in to comment.