Skip to content

Commit

Permalink
- PropertyManagers and PropertySheets now correctly accept all forms of
Browse files Browse the repository at this point in the history
  strings as property values.
  • Loading branch information
dataflake committed Sep 14, 2017
1 parent 05ec0d6 commit 432020a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ https://zope.readthedocs.io/en/2.13/CHANGES.html
Bugs Fixed
++++++++++

- PropertyManagers and PropertySheets now correctly accept all forms of
strings as property values.

- Allow handling of multipart requests in functional doctests using ``http``.

- Fix Content-Length header for non-ascii responses incl. a base tag.
Expand Down
4 changes: 3 additions & 1 deletion src/OFS/PropertyManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"""Property management
"""

import six

from AccessControl.class_init import InitializeClass
from AccessControl.Permissions import access_contents_information
from AccessControl.Permissions import manage_properties
Expand Down Expand Up @@ -206,7 +208,7 @@ def _updateProperty(self, id, value):
if not self.hasProperty(id):
raise BadRequest(
'The property %s does not exist' % escape(id, True))
if isinstance(value, str):
if isinstance(value, (six.string_types, six.binary_type)):
proptype = self.getPropertyType(id) or 'string'
if proptype in type_converters:
value = type_converters[proptype](value)
Expand Down
4 changes: 3 additions & 1 deletion src/OFS/PropertySheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"""Property sheets
"""

import six

from AccessControl.class_init import InitializeClass
from AccessControl.Permissions import access_contents_information
from AccessControl.Permissions import manage_properties
Expand Down Expand Up @@ -248,7 +250,7 @@ def _updateProperty(self, id, value, meta=None):
propinfo = self.propertyInfo(id)
if 'w' not in propinfo.get('mode', 'wd'):
raise BadRequest('%s cannot be changed.' % escape(id, True))
if isinstance(value, str):
if isinstance(value, (six.string_types, six.binary_type)):
proptype = propinfo.get('type', 'string')
if proptype in type_converters:
value = type_converters[proptype](value)
Expand Down
33 changes: 33 additions & 0 deletions src/OFS/tests/testProperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Properties unit tests.
"""

import six
import unittest


Expand Down Expand Up @@ -85,6 +86,21 @@ class WithDescription(self._getTargetClass()):
self.assertEqual(inst.propertyDescription('with_description'),
'With Description')

def test_updateProperty_transforms(self):
pm = self._makeOne()
pm._setProperty('test_lines', [], type='lines')

pm._updateProperty('test_lines', 'foo\nbar')
self.assertEqual(pm.getProperty('test_lines'), ('foo', 'bar'))

# Under Python 3, bytes are decoded back to str
pm._updateProperty('test_lines', b'bar\nbaz')
self.assertEqual(pm.getProperty('test_lines'), ('bar', 'baz'))

pm._updateProperty('test_lines', six.u('uni\ncode'))
self.assertEqual(pm.getProperty('test_lines'),
(six.u('uni'), six.u('code')))


class TestPropertySheet(unittest.TestCase):

Expand All @@ -106,3 +122,20 @@ def testPropertySheetLinesPropertyIsTuple(self):
inst.manage_addProperty('prop2', ['xxx', 'yyy'], 'lines')
self.assertIsInstance(inst.getProperty('prop2'), tuple)
self.assertIsInstance(inst.prop2, tuple)

def test_updateProperty_transforms(self):
ps = self._makeOne('foo')
ps._setProperty('test_lines', [], type='lines')

ps._updateProperty('test_lines', 'foo\nbar')
self.assertEqual(ps.getProperty('test_lines'), ('foo', 'bar'))

# Under Python 3, bytes are decoded back to str
ps._updateProperty('test_lines', b'bar\nbaz')
self.assertEqual(ps.getProperty('test_lines'), ('bar', 'baz'))

ps._updateProperty('test_lines', six.u('uni\ncode'))
self.assertEqual(ps.getProperty('test_lines'),
(six.u('uni'), six.u('code')))


0 comments on commit 432020a

Please sign in to comment.