Commit 29d6c7b7 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki Committed by Jérome Perrin

py2/py3: add str2unicode() utility function, that does nothing in Python 3.

parent 33ee8497
......@@ -567,6 +567,8 @@ def checkPythonSourceCode(source_code_str, portal_type=None):
if str is bytes:
bytes2str = str2bytes = lambda s: s
def str2unicode(s):
return s.decode('utf-8')
def unicode2str(s):
return s.encode('utf-8')
else:
......@@ -574,6 +576,8 @@ else:
return s.decode()
def str2bytes(s):
return s.encode()
def str2unicode(s):
return s
def unicode2str(s):
return s
......
......@@ -189,7 +189,7 @@ ModuleSecurityInfo('Products.ERP5Type.Utils').declarePublic(
'int2letter', 'getMessageIdWithContext', 'getTranslationStringWithContext',
'Email_parseAddressHeader', 'guessEncodingFromText',
'isValidTALESExpression',
'ensure_list', 'bytes2str', 'str2bytes', 'unicode2str',
'ensure_list', 'bytes2str', 'str2bytes', 'str2unicode', 'unicode2str',
)
allow_module('Products.ERP5Type.Message')
......
  • @jerome which is better / which do you prefer ?

    def foo(s):
      return s

    or

    foo = lambda s: s
  • maybe the lambda form causes some pylint warning, but in any case, I think both forms are OK.

    I tried timeit and it seems equivalent from a performance point of view,

    python3 -m timeit -s 'foo = lambda s: s' 'foo("")' ;  python3  -m timeit -s 'def foo(s):' -s '  return s' 'foo("")'

    Looking again at this patch, it would be a bit nicer to be consistent here, do you have a preference ? one point where function is a bit better, is that they have a meaningful repr <function str2bytes at 0x7f996ad848c0> vs <function <lambda> at 0x7f996ad84848>

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