Commit 65693e36 authored by Jérome Perrin's avatar Jérome Perrin

IdTool: handle bytes for group_id on python3

group_id is used as key of OOBtree and as documented, it's not
possible to mix keys that can not be compared, so we can not have a mix
of string and bytes. Since years
parent 07d7a5f9
...@@ -73,6 +73,8 @@ class SQLNonContinuousIncreasingIdGenerator(IdGenerator): ...@@ -73,6 +73,8 @@ class SQLNonContinuousIncreasingIdGenerator(IdGenerator):
# Check the arguments # Check the arguments
if id_group in (None, 'None'): if id_group in (None, 'None'):
raise ValueError('%r is not a valid group Id.' % id_group) raise ValueError('%r is not a valid group Id.' % id_group)
if not isinstance(id_group, str):
raise TypeError('id_group must be str')
if default is None: if default is None:
default = 0 default = 0
...@@ -134,6 +136,7 @@ class SQLNonContinuousIncreasingIdGenerator(IdGenerator): ...@@ -134,6 +136,7 @@ class SQLNonContinuousIncreasingIdGenerator(IdGenerator):
# the last id stored in the sql table # the last id stored in the sql table
for line in self._getValueListFromTable(): for line in self._getValueListFromTable():
id_group = line['id_group'] id_group = line['id_group']
assert isinstance(id_group, str)
last_id = line['last_id'] last_id = line['last_id']
if id_group in self.last_max_id_dict and \ if id_group in self.last_max_id_dict and \
self.last_max_id_dict[id_group].value > last_id: self.last_max_id_dict[id_group].value > last_id:
...@@ -197,6 +200,7 @@ class SQLNonContinuousIncreasingIdGenerator(IdGenerator): ...@@ -197,6 +200,7 @@ class SQLNonContinuousIncreasingIdGenerator(IdGenerator):
getattr(portal_ids, 'dict_length_ids', None) is None): getattr(portal_ids, 'dict_length_ids', None) is None):
dump_dict = portal_ids.dict_length_ids dump_dict = portal_ids.dict_length_ids
for id_group, last_id in dump_dict.items(): for id_group, last_id in dump_dict.items():
assert isinstance(id_group, str)
last_insert_id = get_last_id_method(id_group=id_group) last_insert_id = get_last_id_method(id_group=id_group)
last_id = int(last_id.value) last_id = int(last_id.value)
if len(last_insert_id) != 0: if len(last_insert_id) != 0:
......
...@@ -59,6 +59,8 @@ class ZODBContinuousIncreasingIdGenerator(IdGenerator): ...@@ -59,6 +59,8 @@ class ZODBContinuousIncreasingIdGenerator(IdGenerator):
""" """
if id_group in (None, 'None'): if id_group in (None, 'None'):
raise ValueError('%r is not a valid group Id.' % id_group) raise ValueError('%r is not a valid group Id.' % id_group)
if not isinstance(id_group, str):
raise TypeError('id_group must be str')
if default is None: if default is None:
default = 0 default = 0
last_id_dict = getattr(self, 'last_id_dict', None) last_id_dict = getattr(self, 'last_id_dict', None)
...@@ -108,6 +110,7 @@ class ZODBContinuousIncreasingIdGenerator(IdGenerator): ...@@ -108,6 +110,7 @@ class ZODBContinuousIncreasingIdGenerator(IdGenerator):
if getattr(portal_ids, 'dict_ids', None) is not None: if getattr(portal_ids, 'dict_ids', None) is not None:
for id_group, last_id in portal_ids.dict_ids.items(): for id_group, last_id in portal_ids.dict_ids.items():
if not isinstance(id_group, str): if not isinstance(id_group, str):
assert not isinstance(id_group, bytes), id_group
id_group = repr(id_group) id_group = repr(id_group)
if id_group in self.last_id_dict and \ if id_group in self.last_id_dict and \
self.last_id_dict[id_group] > last_id: self.last_id_dict[id_group] > last_id:
...@@ -148,7 +151,9 @@ class ZODBContinuousIncreasingIdGenerator(IdGenerator): ...@@ -148,7 +151,9 @@ class ZODBContinuousIncreasingIdGenerator(IdGenerator):
self.clearGenerator() self.clearGenerator()
if not isinstance(id_dict, dict): if not isinstance(id_dict, dict):
raise TypeError('the argument given is not a dictionary') raise TypeError('the argument given is not a dictionary')
for value in id_dict.values(): for key, value in id_dict.items():
if not isinstance(key, str):
raise TypeError('key %r given in dictionary is not str' % (key, ))
if not isinstance(value, six.integer_types): if not isinstance(value, six.integer_types):
raise TypeError('the value given in dictionary is not a integer') raise TypeError('the value given in dictionary is not a integer')
self.last_id_dict.update(id_dict) self.last_id_dict.update(id_dict)
......
...@@ -118,6 +118,9 @@ class IdTool(BaseTool): ...@@ -118,6 +118,9 @@ class IdTool(BaseTool):
if id_group in (None, 'None'): if id_group in (None, 'None'):
raise ValueError('%r is not a valid id_group' % id_group) raise ValueError('%r is not a valid id_group' % id_group)
# for compatibilty with sql data, must not use id_group as a list # for compatibilty with sql data, must not use id_group as a list
if six.PY3 and isinstance(id_group, bytes):
warnings.warn('id_group must be a string, not bytes.', BytesWarning)
id_group = id_group.decode('utf-8')
if not isinstance(id_group, str): if not isinstance(id_group, str):
id_group = repr(id_group) id_group = repr(id_group)
warnings.warn('id_group must be a string, other types ' warnings.warn('id_group must be a string, other types '
...@@ -177,6 +180,9 @@ class IdTool(BaseTool): ...@@ -177,6 +180,9 @@ class IdTool(BaseTool):
""" """
if id_group in (None, 'None'): if id_group in (None, 'None'):
raise ValueError('%r is not a valid id_group' % id_group) raise ValueError('%r is not a valid id_group' % id_group)
if six.PY3 and isinstance(id_group, bytes):
warnings.warn('id_group must be a string, not bytes.', BytesWarning)
id_group = id_group.decode('utf-8')
# for compatibilty with sql data, must not use id_group as a list # for compatibilty with sql data, must not use id_group as a list
if not isinstance(id_group, str): if not isinstance(id_group, str):
id_group = repr(id_group) id_group = repr(id_group)
......
CREATE TABLE `portal_ids` ( CREATE TABLE `portal_ids` (
`id_group` VARBINARY(255), `id_group` VARCHAR(255),
`last_id` BIGINT UNSIGNED, `last_id` BIGINT UNSIGNED,
PRIMARY KEY (`id_group`) PRIMARY KEY (`id_group`)
) ENGINE=InnoDB; ) ENGINE=InnoDB;
\ No newline at end of file
CREATE TABLE `portal_ids` ( CREATE TABLE `portal_ids` (
`id_group` VARBINARY(255), `id_group` VARCHAR(255),
`last_id` BIGINT UNSIGNED, `last_id` BIGINT UNSIGNED,
PRIMARY KEY (`id_group`) PRIMARY KEY (`id_group`)
) ENGINE=InnoDB ) ENGINE=InnoDB
......
select id_group, last_id from portal_ids select id_group, cast(id_group as CHAR) id_group from portal_ids
\ No newline at end of file \ No newline at end of file
select id_group, last_id from portal_ids select cast(id_group as CHAR) id_group, last_id from portal_ids
<dtml-if id_group>where id_group > "<dtml-var id_group>"</dtml-if> <dtml-if id_group>where id_group > <dtml-sqlvar id_group type="string"></dtml-if>
order by id_group order by id_group
\ No newline at end of file
...@@ -3655,7 +3655,7 @@ class Base( ...@@ -3655,7 +3655,7 @@ class Base(
next_id = default next_id = default
new_next_id = None if poison else next_id + count new_next_id = None if poison else next_id + count
id_generator_state[group].value = new_next_id id_generator_state[group].value = new_next_id
return range(next_id, new_next_id) return ensure_list(range(next_id, new_next_id))
InitializeClass(Base) InitializeClass(Base)
......
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