Skip to content

Commit

Permalink
Merge pull request #13 from zopefoundation/decode-compatibitlity
Browse files Browse the repository at this point in the history
Improve the unicode compatibility between Python 2 and 3
  • Loading branch information
sallner committed Aug 15, 2017
2 parents d88381d + e95f61d commit 22096ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Changes

- Drop support for Python 2.6 and 3.3.

- Use ``UTF-8`` as default encoding when casting bytes to unicode for Python 2
*and* 3.


4.3.0 (2014-12-24)
==================
Expand Down
8 changes: 7 additions & 1 deletion src/zope/formlib/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ def safeBase64Encode(obj):

from StringIO import StringIO
from itertools import imap
unicode = toUnicode = unicode
unicode = unicode
basestring = basestring

def toUnicode(obj):
if isinstance(obj, bytes):
return unicode(obj, 'utf-8')
else:
return unicode(obj)

def safeBase64Encode(obj):
return base64.b64encode(toUnicode(obj)).strip().replace('=', '_')
9 changes: 9 additions & 0 deletions src/zope/formlib/tests/test_textwidget.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2001, 2002, 2004, 2005 Zope Foundation and Contributors.
Expand Down Expand Up @@ -132,6 +133,14 @@ def testRender(self):
self._widget.extra = 'style="color: red"'
self.verifyResult(self._widget.hidden(), check_list)

def testRenderUTF8Input(self):
value = u"☃".encode('utf-8') # results in \u2603
self._widget.setRenderedValue(value)
check_list = ('type="text"', 'id="field.foo"', 'name="field.foo"',
u'value="\u2603"', 'size="20"')
self.verifyResult(self._widget(), check_list)


class URIDisplayWidgetTest(BrowserWidgetTest):

_WidgetFactory = URIDisplayWidget
Expand Down

0 comments on commit 22096ae

Please sign in to comment.