python 3.4 support

This commit is contained in:
Emmanuel Garette 2016-03-19 21:27:37 +01:00
parent 924692d3ab
commit 7305cfa134
12 changed files with 62 additions and 23 deletions

View File

@ -372,7 +372,7 @@ def test_config_od_function():
descr = OptionDescription('tiramisu', '', [o])
cfg = Config(descr)
try:
print cfg.impl_get_opt_by_path()
except AttributeError, err:
print(cfg.impl_get_opt_by_path())
except AttributeError as err:
assert str(err) == _('unknown Option {0} in OptionDescription {1}'
'').format('impl_get_opt_by_path', descr.impl_getname())

View File

@ -1,7 +1,7 @@
from autopath import do_autopath
do_autopath()
import warnings
import warnings, sys
from py.test import raises
from tiramisu.config import Config
@ -49,14 +49,20 @@ def test_domainname_upper():
has_error = False
try:
c.d = 'TOTO.COM'
except ValueError, err:
except ValueError as err:
if sys.version_info[0] >= 3: # pragma: optional cover
assert msg in str(err)
else:
assert msg in unicode(err)
has_error = True
assert has_error is True
has_error = False
try:
c.d = 'toTo.com'
except ValueError, err:
except ValueError as err:
if sys.version_info[0] >= 3: # pragma: optional cover
assert msg in str(err)
else:
assert msg in unicode(err)
has_error = True
assert has_error is True

View File

@ -207,7 +207,7 @@ def test_prop_dyndescription():
assert str(cfg.cfgimpl_get_settings()[stval2]) in [str(['test']), str([u'test'])]
cfg.cfgimpl_get_settings()[stval2].append('test2')
assert str(cfg.cfgimpl_get_settings()[stval1]) in [str(['test']), str([u'test'])]
assert str(cfg.cfgimpl_get_settings()[stval2]) in [str(['test', 'test2']), str([u'test', 'test2'])]
assert str(cfg.cfgimpl_get_settings()[stval2]) in [str(['test', 'test2']), str([u'test', 'test2']), str(['test2', 'test'])]
cfg.cfgimpl_get_settings()[stval1].remove('test')
assert str(cfg.cfgimpl_get_settings()[stval1]) == str([])
#
@ -417,7 +417,7 @@ def test_prop_dyndescription_context():
assert str(cfg.cfgimpl_get_settings()[stval2]) in [str(['test']), str([u'test'])]
cfg.cfgimpl_get_settings()[stval2].append('test2')
assert str(cfg.cfgimpl_get_settings()[stval1]) in [str(['test']), str([u'test'])]
assert str(cfg.cfgimpl_get_settings()[stval2]) in [str(['test', 'test2']), str([u'test', 'test2'])]
assert str(cfg.cfgimpl_get_settings()[stval2]) in [str(['test', 'test2']), str([u'test', 'test2']), str(['test2', 'test'])]
cfg.cfgimpl_get_settings()[stval1].remove('test')
assert str(cfg.cfgimpl_get_settings()[stval1]) == str([])
@ -1346,7 +1346,7 @@ def test_invalid_samevalue_dyndescription():
od = OptionDescription('od', '', [dod])
cfg = Config(od)
cfg
raises(ConfigError, "print cfg")
raises(ConfigError, "print(cfg)")
def test_invalid_name_dyndescription():
@ -1355,4 +1355,4 @@ def test_invalid_name_dyndescription():
od = OptionDescription('od', '', [dod])
cfg = Config(od)
cfg
raises(ValueError, "print cfg")
raises(ValueError, "print(cfg)")

View File

@ -10,6 +10,7 @@ from tiramisu.storage import delete_session
from tiramisu.error import ConfigError
from pickle import dumps, loads
from py.test import raises
import sys
def return_value(value=None):
@ -460,7 +461,7 @@ def test_state_groupconfig():
def test_state_unkown_setting_owner():
"""load an unknow _owner, should create it"""
assert not 'supernewuser' in owners.__dict__
loads("""ccopy_reg
val = """ccopy_reg
_reconstructor
p0
(ctiramisu.setting
@ -496,5 +497,8 @@ sS'_properties'
p17
(dp18
sbsb.
.""")
."""
if sys.version_info[0] >= 3: # pragma: optional cover
val = bytes(val, "UTF-8")
loads(val)
assert 'supernewuser' in owners.__dict__

View File

@ -243,7 +243,7 @@ def calculate(callback, args, kwargs, returns_raise):
if returns_raise:
try:
return callback(*args, **kwargs)
except ValueError, err:
except ValueError as err:
return err
else:
return callback(*args, **kwargs)

View File

@ -20,6 +20,7 @@
# ____________________________________________________________
"options handler global entry point"
import weakref
import sys
from .error import PropertiesOptionError, ConfigError, ConflictError
@ -33,6 +34,10 @@ from .value import Values, Multi
from .i18n import _
if sys.version_info[0] >= 3: # pragma: optional cover
xrange = range
class SubConfig(object):
"""Sub configuration management entry.
Tree if OptionDescription's responsability. SubConfig are generated

View File

@ -21,6 +21,7 @@
import re
from types import FunctionType
import warnings
import sys
from ..i18n import _
from ..setting import log, undefined
@ -344,6 +345,10 @@ class BaseOption(Base):
return self._properties
def _impl_valid_unicode(self, value):
if sys.version_info[0] >= 3:
if not isinstance(value, str):
return ValueError(_('invalid string'))
else:
if not isinstance(value, unicode) and not isinstance(value, str):
return ValueError(_('invalid unicode or string'))
@ -676,7 +681,7 @@ class Option(OnlyOption):
warnings_only, transitive)
if err:
if warnings_only:
return ValueWarning(err.message, option)
return ValueWarning(str(err), option)
else:
return err

View File

@ -278,6 +278,9 @@ class PortOption(Option):
def _validate(self, value, context=undefined, current_opt=undefined):
if isinstance(value, int):
if sys.version_info[0] >= 3: # pragma: optional cover
value = str(value)
else:
value = unicode(value)
err = self._impl_valid_unicode(value)
if err:

View File

@ -35,6 +35,11 @@ StorageOptionDescription = get_storages_option('optiondescription')
name_regexp = re.compile(r'^[a-zA-Z\d\-_]*$')
import sys
if sys.version_info[0] >= 3: # pragma: optional cover
xrange = range
del(sys)
class OptionDescription(BaseOption, StorageOptionDescription):
"""Config's schema (organisation, group) and container of Options

View File

@ -130,7 +130,7 @@ def get_storages(context, session_id, persistent):
values = imp.Values(storage)
try:
return settings, values
except Exception, err:
except Exception as err:
raise Exception(_('unable to get storages:') + str(err))

View File

@ -17,11 +17,14 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ____________________________________________________________
import sys
from ...i18n import _
from ...setting import undefined
from ...error import ConfigError
static_tuple = tuple()
static_set = frozenset()
if sys.version_info[0] >= 3: # pragma: optional cover
xrange = range
#____________________________________________________________
@ -128,7 +131,7 @@ class StorageBase(object):
if isinstance(dico, tuple):
if key in dico[0]:
return dico[1][dico[0].index(key)]
elif isinstance(dico, str) or isinstance(dico, unicode):
elif self._is_string(dico):
if key == 'doc':
return dico
elif isinstance(dico, dict):
@ -262,6 +265,12 @@ class StorageBase(object):
def _impl_setopt(self, opt):
self._opt = opt
def _is_string(self, infos):
if sys.version_info[0] >= 3: # pragma: optional cover
return isinstance(infos, str)
else:
return isinstance(infos, str) or isinstance(infos, unicode)
def _impl_convert_zinformations(self, descr, load=False):
if not load:
infos = self._informations
@ -269,7 +278,7 @@ class StorageBase(object):
self._state_informations = {}
for idx, key in enumerate(infos[0]):
self._state_informations[key] = infos[1][idx]
elif isinstance(infos, str) or isinstance(infos, unicode):
elif self._is_string(infos):
self._state_informations = {'doc': infos}
else:
self._state_informations = infos

View File

@ -114,8 +114,10 @@ class Cache(object):
return path in self._cache and index in self._cache[path]
def reset_expired_cache(self, exp):
for key in self._cache.keys():
for index in self._cache[key].keys():
cache_keys = list(self._cache.keys())
for key in cache_keys:
key_cache_keys = list(self._cache[key].keys())
for index in key_cache_keys:
val, created = self._cache[key][index]
if created is not None and exp > created:
del(self._cache[key][index])