Commit 8257ac4b authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

py2/py3: handle bytes correctly in Python3.

parent fb3903a6
......@@ -29,7 +29,7 @@ from __future__ import absolute_import
from six import string_types as basestring
from six.moves import xrange
from Products.ERP5Type.Utils import ensure_list, str2bytes
from Products.ERP5Type.Utils import bytes2str, ensure_list, str2bytes
from collections import defaultdict
from contextlib import contextmanager
from itertools import product, chain
......@@ -397,10 +397,10 @@ CREATE TABLE %s (
for line in result]
def countMessageSQL(self, quote, **kw):
return "SELECT count(*) FROM %s WHERE processing_node > %d AND %s" % (
self.sql_table, DEPENDENCY_IGNORED_ERROR_STATE, " AND ".join(
return b"SELECT count(*) FROM %s WHERE processing_node > %d AND %s" % (
str2bytes(self.sql_table), DEPENDENCY_IGNORED_ERROR_STATE, b" AND ".join(
sqltest_dict[k](v, quote) for (k, v) in six.iteritems(kw) if v
) or "1")
) or b"1")
def hasActivitySQL(self, quote, only_valid=False, only_invalid=False, **kw):
where = [sqltest_dict[k](v, quote) for (k, v) in six.iteritems(kw) if v]
......@@ -425,7 +425,7 @@ CREATE TABLE %s (
0,
)[1]
else:
subquery = (b"("
subquery = lambda *a, **k: str2bytes(bytes2str(b"("
b"SELECT 3*priority{} AS effective_priority, date"
b" FROM %s"
b" WHERE"
......@@ -434,7 +434,7 @@ CREATE TABLE %s (
b" date <= UTC_TIMESTAMP(6)"
b" ORDER BY priority, date"
b" LIMIT 1"
b")" % self.sql_table).format
b")" % str2bytes(self.sql_table)).format(*a, **k))
result = query(
b"SELECT *"
b" FROM (%s) AS t"
......@@ -443,11 +443,11 @@ CREATE TABLE %s (
b" UNION ALL ".join(
chain(
(
subquery(b'-1', b'node = %i' % processing_node),
subquery(b'', b'node=0'),
subquery('-1', 'node = %i' % processing_node),
subquery('', 'node=0'),
),
(
subquery(b'-1', b'node = %i' % x)
subquery('-1', 'node = %i' % x)
for x in node_set
),
),
......@@ -464,7 +464,7 @@ CREATE TABLE %s (
# sorted set to filter negative node values.
# This is why this query is only executed when the previous one
# did not find anything.
result = query(subquery(b'+1', b'node>0'), 0)[1]
result = query(subquery('+1', 'node>0'), 0)[1]
if result:
return result[0]
return Queue.getPriority(self, activity_tool, processing_node, node_set)
......@@ -778,7 +778,7 @@ CREATE TABLE %s (
0,
))
else:
subquery = (b"("
subquery = lambda *a, **k: str2bytes(bytes2str(b"("
b"SELECT *, 3*priority{} AS effective_priority"
b" FROM %s"
b" WHERE"
......@@ -787,7 +787,7 @@ CREATE TABLE %s (
b" %s%s"
b" ORDER BY priority, date"
b" LIMIT %i"
b")" % args).format
b")" % args).format(*a, *k))
result = Results(query(
b"SELECT *"
b" FROM (%s) AS t"
......@@ -796,11 +796,11 @@ CREATE TABLE %s (
b" UNION ALL ".join(
chain(
(
subquery(b'-1', b'node = %i' % processing_node),
subquery(b'', b'node=0'),
subquery('-1', 'node = %i' % processing_node),
subquery('', 'node=0'),
),
(
subquery(b'-1', b'node = %i' % x)
subquery('-1', 'node = %i' % x)
for x in node_set
),
),
......@@ -818,7 +818,7 @@ CREATE TABLE %s (
# sorted set to filter negative node values.
# This is why this query is only executed when the previous one
# did not find anything.
result = Results(query(subquery(b'+1', b'node>0'), 0))
result = Results(query(subquery('+1', 'node>0'), 0))
if result:
# Reserve messages.
uid_list = [x.uid for x in result]
......
......@@ -1827,7 +1827,7 @@ class ActivityTool (BaseTool):
"""
db = self.getSQLConnection()
quote = db.string_literal
return sum(x for x, in db.query("(%s)" % ") UNION ALL (".join(
return sum(x for x, in db.query(b"(%s)" % b") UNION ALL (".join(
activity.countMessageSQL(quote, **kw)
for activity in six.itervalues(activity_dict)))[1])
......
......@@ -385,7 +385,7 @@ def getTranslationStringWithContext(self, msg_id, context, context_id):
result = localizer.erp5_ui.gettext(msg_id_context, default='')
if result == '':
result = localizer.erp5_ui.gettext(msg_id)
return result.encode('utf8')
return unicode2str(result)
def Email_parseAddressHeader(text):
"""
......
......@@ -70,7 +70,7 @@ from Products.PythonScripts.PythonScript import PythonScript
from Products.ERP5Type.Accessor.Constant import PropertyGetter as ConstantGetter
from Products.ERP5Form.PreferenceTool import Priority
from zLOG import LOG, DEBUG
from Products.ERP5Type.Utils import convertToUpperCase, str2bytes
from Products.ERP5Type.Utils import convertToUpperCase, bytes2str, str2bytes
from Products.ERP5Type.tests.backportUnittest import SetupSiteError
from Products.ERP5Type.tests.utils import addUserToDeveloperRole
from Products.ERP5Type.tests.utils import parseListeningAddress
......@@ -1567,7 +1567,7 @@ def optimize():
PythonScript._compile = _compile
PythonScript_exec = PythonScript._exec
def _exec(self, *args):
self.func_code # trigger compilation if needed
self.__code__ # trigger compilation if needed
return PythonScript_exec(self, *args)
PythonScript._exec = _exec
from Acquisition import aq_parent
......
......@@ -31,6 +31,8 @@ def sqlquote(value):
# (ex: ZMySQLDA.DA.Connection.sql_quote__).
# Duplicating such code is error-prone, and makes us rely on a specific SQL
# dialect...
if str != bytes and isinstance(value, bytes): # six.PY3
value = value.decode()
return "'" + (value
.replace('\x5c', r'\\')
.replace('\x00', r'\0')
......
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