Commit fa66bc52 authored by Arnaud Fontaine's avatar Arnaud Fontaine

py3: Add helper functions to write code for both python2 and python3.

* Add functions to properly convert/handle bytes/str/unicode (from
  slapos.util) (stricter than six.ensure_{text,str}()).
* Python3 {keys,values,items}() now returns dict_{keys,values,items} rather than
  real list() so add ensure_list() to create a list() when it is latter modified
  but do nothing on Python2 for performance sake.
parent ae3fe621
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
# Required modules - some modules are imported later to prevent circular deadlocks # Required modules - some modules are imported later to prevent circular deadlocks
from __future__ import absolute_import from __future__ import absolute_import
import six
import os import os
import re import re
import string import string
...@@ -554,6 +555,24 @@ def checkPythonSourceCode(source_code_str, portal_type=None): ...@@ -554,6 +555,24 @@ def checkPythonSourceCode(source_code_str, portal_type=None):
# Python 2-3 compat # Python 2-3 compat
##################################################### #####################################################
if str is bytes:
bytes2str = str2bytes = lambda s: s
def unicode2str(s):
return s.encode('utf-8')
else:
def bytes2str(s):
return s.decode()
def str2bytes(s):
return s.encode()
def unicode2str(s):
return s
if six.PY3:
def ensure_list(o):
return list(o)
else:
ensure_list = lambda x: x
def with_metaclass(meta, *bases): def with_metaclass(meta, *bases):
""" """
Function from jinja2/_compat.py. License: BSD (copy/paste here for Function from jinja2/_compat.py. License: BSD (copy/paste here for
......
...@@ -180,7 +180,9 @@ ModuleSecurityInfo('Products.ERP5Type.Utils').declarePublic( ...@@ -180,7 +180,9 @@ ModuleSecurityInfo('Products.ERP5Type.Utils').declarePublic(
'convertToMixedCase', 'cartesianProduct', 'sleep', 'getCommonTimeZoneList', 'convertToMixedCase', 'cartesianProduct', 'sleep', 'getCommonTimeZoneList',
'int2letter', 'getMessageIdWithContext', 'getTranslationStringWithContext', 'int2letter', 'getMessageIdWithContext', 'getTranslationStringWithContext',
'Email_parseAddressHeader', 'guessEncodingFromText', 'Email_parseAddressHeader', 'guessEncodingFromText',
'isValidTALESExpression') 'isValidTALESExpression',
'ensure_list', 'bytes2str', 'str2bytes', 'unicode2str',
)
allow_module('Products.ERP5Type.Message') allow_module('Products.ERP5Type.Message')
ModuleSecurityInfo('Products.ERP5Type.Message').declarePublic('translateString') ModuleSecurityInfo('Products.ERP5Type.Message').declarePublic('translateString')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment