Skip to content

Commit

Permalink
Add Python 2 / 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pbauer committed Jan 26, 2018
1 parent 3dd57cf commit 242bdfa
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ New features:

Bug fixes:

- *add item here*
- Add Python 2 / 3 compatibility
[pbauer]


1.4.7 (2017-12-14)
Expand Down
7 changes: 5 additions & 2 deletions plone/app/contenttypes/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from zope.lifecycleevent import modified


import six


@implementer(ICollection)
class Collection(Item):
"""Convenience subclass for ``Collection`` portal type
Expand Down Expand Up @@ -91,7 +94,7 @@ def PUT(self, REQUEST=None, RESPONSE=None):
infile = request.get('BODYFILE', None)
filename = request['PATH_INFO'].split('/')[-1]
self.file = NamedBlobFile(
data=infile.read(), filename=unicode(filename))
data=infile.read(), filename=six.text_type(filename))

modified(self)
return response
Expand Down Expand Up @@ -125,7 +128,7 @@ def PUT(self, REQUEST=None, RESPONSE=None):
infile = request.get('BODYFILE', None)
filename = request['PATH_INFO'].split('/')[-1]
self.image = NamedBlobImage(
data=infile.read(), filename=unicode(filename))
data=infile.read(), filename=six.text_type(filename))

modified(self)
return response
Expand Down
9 changes: 6 additions & 3 deletions plone/app/contenttypes/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from ZODB.POSException import ConflictError


import six


logger = getLogger(__name__)

FALLBACK_CONTENTTYPE = 'application/octet-stream'
Expand All @@ -28,11 +31,11 @@
def _unicode_save_string_concat(*args):
"""
concats args with spaces between and returns utf-8 string, it does not
matter if input was unicode or str
matter if input was text or bytes
"""
result = ''
for value in args:
if isinstance(value, unicode):
if isinstance(value, six.text_type):
value = value.encode('utf-8', 'replace')
if value:
result = ' '.join((result, value))
Expand Down Expand Up @@ -116,7 +119,7 @@ def SearchableText_file(obj):
return SearchableText(obj)
except (ConflictError, KeyboardInterrupt):
raise
except Exception, msg:
except Exception as msg:
logger.exception(
'exception while trying to convert blob contents to "text/plain" '
'for {0}. Error: {1}'.format(obj, str(msg)),
Expand Down
2 changes: 1 addition & 1 deletion plone/app/contenttypes/migration/custom_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def __call__(self):
u'this configuration')
try:
results = self.migrate(dry_run=True)
except Exception, e:
except Exception as e:
trace = traceback.format_exc()
msg = 'Test-Migration failed: {0}\n{1}\n'.format(e, trace)
logger.error(msg)
Expand Down
5 changes: 4 additions & 1 deletion plone/app/contenttypes/migration/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import pkg_resources


import six


try:
pkg_resources.get_distribution('plone.app.collection')
except pkg_resources.DistributionNotFound:
Expand Down Expand Up @@ -123,7 +126,7 @@ def get_terms(context, counter, ext_dict, show_extended):
title = translated_meta_type (number_of_instances) - extended fields: list
"""
results = []
for k, v in counter.iteritems():
for k, v in six.iteritems(counter):
if not show_extended:
if k not in ext_dict:
display = u'{0} ({1})'.format(context.translate(_(k)), v)
Expand Down
2 changes: 1 addition & 1 deletion plone/app/contenttypes/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_respect_navigation_root(self):

# Create two subsites i.e create two folders and mark them with
# INavigationRoot
for i in xrange(1, 3):
for i in range(1, 3):
folder_id = 'folder{0}'.format(i)
portal.invokeFactory('Folder',
folder_id,
Expand Down
5 changes: 4 additions & 1 deletion plone/app/contenttypes/tests/test_collection_rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import unittest


import six


query = [{
'i': 'Title',
'o': 'plone.app.querystring.operation.string.is',
Expand Down Expand Up @@ -52,7 +55,7 @@ def assertIsValidRSS(self, rss):
# XXX: We might want to validate against a DTD or RelaxNG schema here.
# schema = etree.XMLSchema(schema_root)
# parser = etree.XMLParser(dtd_validation=True,schema=schema)
if isinstance(rss, unicode):
if isinstance(rss, six.text_type):
rss = rss.encode('utf-8')
parser = etree.XMLParser()
return etree.fromstring(rss, parser)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def read(*rnames):
'plone.app.lockingbehavior',
'pytz',
'plone.app.z3cform>=1.1.0.dev0',
'six',
'zope.deprecation',
],
extras_require={
Expand Down

0 comments on commit 242bdfa

Please sign in to comment.