Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field2* converters should not handle files #963

Merged
merged 9 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
icemac marked this conversation as resolved.
Show resolved Hide resolved
"""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