From c8bc3093c7f5ed74c86eb8fad522a9f20283bacb Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 9 Jul 2017 09:49:03 +0200 Subject: [PATCH] python 3.5 support --- test/test_cache.py | 2 +- test/test_choice_option.py | 2 +- test/test_config.py | 2 +- test/test_config_api.py | 2 +- test/test_config_domain.py | 2 +- test/test_config_ip.py | 2 +- test/test_dereference.py | 2 +- test/test_duplicate_config.py | 2 +- test/test_dyn_optiondescription.py | 2 +- test/test_freeze.py | 2 +- test/test_mandatory.py | 2 +- test/test_metaconfig.py | 2 +- test/test_multi.py | 2 +- test/test_option.py | 2 +- test/test_option_calculation.py | 6 +++--- test/test_option_consistency.py | 2 +- test/test_option_default.py | 2 +- test/test_option_owner.py | 2 +- test/test_option_setting.py | 28 ++++++++++++++++----------- test/test_option_type.py | 2 +- test/test_option_username.py | 2 +- test/test_option_validator.py | 4 ++-- test/test_option_with_special_name.py | 2 +- test/test_parsing_group.py | 2 +- test/test_permissive.py | 2 +- test/test_requires.py | 2 +- test/test_slots.py | 4 ++-- test/test_state.py | 2 +- test/test_storage.py | 2 +- test/test_submulti.py | 2 +- test/test_symlink.py | 2 +- tiramisu/autolib.py | 4 ++-- tiramisu/config.py | 15 +++++++++++--- tiramisu/error.py | 8 +++++--- tiramisu/option/baseoption.py | 26 ++++++++++++++++--------- tiramisu/option/option.py | 2 +- tiramisu/storage/dictionary/option.py | 2 +- 37 files changed, 89 insertions(+), 64 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index c4611ca..e425832 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu import setting, value diff --git a/test/test_choice_option.py b/test/test_choice_option.py index 7469d9b..6094f2e 100644 --- a/test/test_choice_option.py +++ b/test/test_choice_option.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.setting import owners diff --git a/test/test_config.py b/test/test_config.py index 0114af9..5919df7 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -2,7 +2,7 @@ """theses tests are much more to test that config, option description, vs... **it's there** and answers via attribute access""" -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_config_api.py b/test/test_config_api.py index eeaf275..06ba86a 100644 --- a/test/test_config_api.py +++ b/test/test_config_api.py @@ -1,5 +1,5 @@ "configuration objects global API" -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_config_domain.py b/test/test_config_domain.py index 528efda..2fe4495 100644 --- a/test/test_config_domain.py +++ b/test/test_config_domain.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() import warnings, sys diff --git a/test/test_config_ip.py b/test/test_config_ip.py index 3f15b02..f2f27be 100644 --- a/test/test_config_ip.py +++ b/test/test_config_ip.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() import warnings diff --git a/test/test_dereference.py b/test/test_dereference.py index 00e824d..a1e7652 100644 --- a/test/test_dereference.py +++ b/test/test_dereference.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.config import Config, GroupConfig, MetaConfig diff --git a/test/test_duplicate_config.py b/test/test_duplicate_config.py index ccc45aa..0ae6837 100644 --- a/test/test_duplicate_config.py +++ b/test/test_duplicate_config.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.setting import groups diff --git a/test/test_dyn_optiondescription.py b/test/test_dyn_optiondescription.py index d6e798a..086f10c 100644 --- a/test/test_dyn_optiondescription.py +++ b/test/test_dyn_optiondescription.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.setting import groups, owners diff --git a/test/test_freeze.py b/test/test_freeze.py index 504cda6..a2c9da5 100644 --- a/test/test_freeze.py +++ b/test/test_freeze.py @@ -1,6 +1,6 @@ # coding: utf-8 "frozen and hidden values" -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_mandatory.py b/test/test_mandatory.py index f90b049..424de72 100644 --- a/test/test_mandatory.py +++ b/test/test_mandatory.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_metaconfig.py b/test/test_metaconfig.py index 78479c9..ad5c264 100644 --- a/test/test_metaconfig.py +++ b/test/test_metaconfig.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_multi.py b/test/test_multi.py index bdda282..276a025 100644 --- a/test/test_multi.py +++ b/test/test_multi.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.value import Multi diff --git a/test/test_option.py b/test/test_option.py index 5c2fc25..0a86788 100644 --- a/test/test_option.py +++ b/test/test_option.py @@ -1,7 +1,7 @@ """these tests are here to create some :class:`tiramisu.option.Option`'s and to compare them """ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_option_calculation.py b/test/test_option_calculation.py index b1fc7dd..2e8a34f 100644 --- a/test/test_option_calculation.py +++ b/test/test_option_calculation.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises @@ -1083,9 +1083,9 @@ def test_callback_raise(): cfg.read_write() try: cfg.od1.opt1 - except ConfigError, err: + except ConfigError as err: assert '"Option 1"' in str(err) try: cfg.od2.opt2 - except ConfigError, err: + except ConfigError as err: assert '"Option 2"' in str(err) diff --git a/test/test_option_consistency.py b/test/test_option_consistency.py index ff5c3e8..3761cf2 100644 --- a/test/test_option_consistency.py +++ b/test/test_option_consistency.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_option_default.py b/test/test_option_default.py index dfe2b97..1ed8ee8 100644 --- a/test/test_option_default.py +++ b/test/test_option_default.py @@ -1,5 +1,5 @@ "test all types of option default values for options, add new option in a descr" -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_option_owner.py b/test/test_option_owner.py index d26c60c..527ee95 100644 --- a/test/test_option_owner.py +++ b/test/test_option_owner.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_option_setting.py b/test/test_option_setting.py index 9034d4e..4e67308 100644 --- a/test/test_option_setting.py +++ b/test/test_option_setting.py @@ -1,5 +1,5 @@ "config.set() or config.setoption() or option.setoption()" -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises @@ -564,10 +564,11 @@ def test_pprint(): setting = config.cfgimpl_get_settings() config.read_write() config.int = 1 + err = None try: config.str - except Exception, err: - pass + except Exception as error: + err = error list_disabled_1 = 'disabled (' + display_list([msg_is.format('string2', 'string'), msg_is.format('Test int option', '1')]) + ')' list_disabled_2 = 'disabled (' + display_list([msg_is.format('Test int option', '1'), msg_is.format('string2', 'string')]) + ')' @@ -578,17 +579,20 @@ def test_pprint(): comp4 = str(err) == _(msg_error.format('option', 'Test string option', properties, display_list([list_hidden, list_disabled_2]))) assert comp1 or comp2 or comp3 or comp4 + err = None try: config.options.val2 - except Exception, err: - pass + except Exception as error: + err = error assert str(err) == msg_error.format('optiondescription', 'options', prop, 'hidden (' + msg_is.format('Test int option', 1) + ')') + err = None try: config.val3 - except Exception, err: - pass + except Exception as error: + err = error + msg_1 = msg_is.format('string2', 'string') msg_2 = msg_is.format('Test int option', 1) msg_3 = msg_is_not.format('Test int option', display_list([2, 3, 4], 'or')) @@ -607,16 +611,18 @@ def test_pprint(): comp6 = str(err) == msg_error.format('option', 'val3', prop, list_hidden) assert comp1 or comp2 or comp3 or comp4 or comp5 or comp6 + err = None try: config.string - except Exception, err: - pass + except Exception as error: + err = error assert str(err) == msg_error.format('option', 'string', properties, display_list(['disabled', 'hidden'])) + err = None try: config.string3 - except Exception, err: - pass + except Exception as error: + err = error assert str(err) == msg_error.format('option', 'string3', prop, 'hidden') diff --git a/test/test_option_type.py b/test/test_option_type.py index 2da3e92..7ebefe0 100644 --- a/test/test_option_type.py +++ b/test/test_option_type.py @@ -1,6 +1,6 @@ # coding: utf-8 "frozen and hidden values" -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_option_username.py b/test/test_option_username.py index 88c800b..e23a3a8 100644 --- a/test/test_option_username.py +++ b/test/test_option_username.py @@ -1,5 +1,5 @@ "configuration objects global API" -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_option_validator.py b/test/test_option_validator.py index da28f25..78b774f 100644 --- a/test/test_option_validator.py +++ b/test/test_option_validator.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() import warnings @@ -97,7 +97,7 @@ def test_validator(): raises(ValueError, "cfg.opt2 = 'val'") try: cfg.opt2 = 'val' - except ValueError, err: + except ValueError as err: msg = _('"{0}" is an invalid {1} for "{2}", {3}').format('val', _('string'), 'opt2', 'test error') assert str(err) == msg diff --git a/test/test_option_with_special_name.py b/test/test_option_with_special_name.py index 63fe82f..a331fa0 100644 --- a/test/test_option_with_special_name.py +++ b/test/test_option_with_special_name.py @@ -1,5 +1,5 @@ #this test is much more to test that **it's there** and answers attribute access -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_parsing_group.py b/test/test_parsing_group.py index 0ac2457..d6a9d9c 100644 --- a/test/test_parsing_group.py +++ b/test/test_parsing_group.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.setting import groups, owners diff --git a/test/test_permissive.py b/test/test_permissive.py index 423b9b5..7caf61d 100644 --- a/test/test_permissive.py +++ b/test/test_permissive.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises diff --git a/test/test_requires.py b/test/test_requires.py index bd499b2..7dc3ef2 100644 --- a/test/test_requires.py +++ b/test/test_requires.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from copy import copy diff --git a/test/test_slots.py b/test/test_slots.py index 20beef5..5932165 100644 --- a/test/test_slots.py +++ b/test/test_slots.py @@ -1,5 +1,5 @@ ## coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from py.test import raises @@ -11,7 +11,7 @@ from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption,\ URLOption, FilenameOption -print "FIXME slot pour un masterslaves !!" +#FIXME slot pour un masterslaves !! def test_slots_option(): diff --git a/test/test_state.py b/test/test_state.py index 7a78ebc..5a54c4a 100644 --- a/test/test_state.py +++ b/test/test_state.py @@ -1,4 +1,4 @@ -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.option import BoolOption, UnicodeOption, SymLinkOption, \ diff --git a/test/test_storage.py b/test/test_storage.py index ad12a51..f1aee89 100644 --- a/test/test_storage.py +++ b/test/test_storage.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() #from py.test import raises diff --git a/test/test_submulti.py b/test/test_submulti.py index 9477699..6896ed2 100644 --- a/test/test_submulti.py +++ b/test/test_submulti.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.setting import groups, owners diff --git a/test/test_symlink.py b/test/test_symlink.py index 765f50a..592d244 100644 --- a/test/test_symlink.py +++ b/test/test_symlink.py @@ -1,5 +1,5 @@ # coding: utf-8 -from autopath import do_autopath +from .autopath import do_autopath do_autopath() from tiramisu.option import BoolOption, StrOption, SymLinkOption, \ diff --git a/tiramisu/autolib.py b/tiramisu/autolib.py index 3c4530b..375dfa2 100644 --- a/tiramisu/autolib.py +++ b/tiramisu/autolib.py @@ -263,13 +263,13 @@ def calculate(option, callback, is_validator, args, kwargs): if len(args) != 0 or len(kwargs) != 0: msg = _('unexpected error "{0}" in function "{1}" with arguments "{3}" and "{4}" ' 'for option "{2}"').format(str(error), - callback.func_name, + callback.__name__, option.impl_get_display_name(), args, kwargs) else: msg = _('unexpected error "{0}" in function "{1}" for option "{2}"' '').format(str(error), - callback.func_name, + callback.__name__, option.impl_get_display_name()) raise ConfigError(msg) diff --git a/tiramisu/config.py b/tiramisu/config.py index cc61b29..5a63346 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -168,7 +168,10 @@ class SubConfig(object): yield name, self.getattr(name, force_permissive=force_permissive) except GeneratorExit: # pragma: optional cover - raise StopIteration + if sys.version_info[0] < 3: + raise StopIteration + else: + raise GeneratorExit() except PropertiesOptionError: # pragma: optional cover pass # option with properties @@ -180,7 +183,10 @@ class SubConfig(object): yield child.impl_getname(), self.getattr(child.impl_getname(), force_permissive=force_permissive) except GeneratorExit: # pragma: optional cover - raise StopIteration + if sys.version_info[0] < 3: + raise StopIteration + else: + raise GeneratorExit() except PropertiesOptionError: # pragma: optional cover pass # option with properties @@ -206,7 +212,10 @@ class SubConfig(object): name = child.impl_getname() yield name, self.getattr(name, force_permissive=force_permissive) except GeneratorExit: # pragma: optional cover - raise StopIteration + if sys.version_info[0] < 3: + raise StopIteration + else: + raise GeneratorExit() except PropertiesOptionError: # pragma: optional cover pass # ______________________________________________________________________ diff --git a/tiramisu/error.py b/tiramisu/error.py index a904bd3..011fd35 100644 --- a/tiramisu/error.py +++ b/tiramisu/error.py @@ -16,6 +16,7 @@ # ____________________________________________________________ "user defined exceptions" from .i18n import _ +import sys def display_list(lst, separator='and'): @@ -27,21 +28,22 @@ def display_list(lst, separator='and'): return '' elif len(lst) == 1: ret = lst[0] - if isinstance(ret, unicode): + if sys.version_info[0] < 3 and isinstance(ret, unicode): ret = ret.encode('utf8') if not isinstance(ret, str): ret = str(ret) return ret else: + lst.sort() lst_ = [] for l in lst[:-1]: - if isinstance(l, unicode): + if sys.version_info[0] < 3 and isinstance(l, unicode): l = l.encode('utf8') elif not isinstance(l, str): l = str(l) lst_.append(l) last = lst[-1] - if isinstance(last, unicode): + if sys.version_info[0] < 3 and isinstance(last, unicode): last = last.encode('utf8') if not isinstance(last, str): last = str(last) diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index 13c99d5..3b0102e 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -22,7 +22,11 @@ import re from types import FunctionType import warnings import sys -from inspect import getargspec + +if sys.version_info[0] >= 3: + from inspect import signature +else: + from inspect import getargspec from ..i18n import _ from ..setting import log, undefined, debug, groups @@ -180,13 +184,17 @@ class Base(StorageBase): self.commit(session) def _build_validator_params(self, validator, validator_params): - func_args = getargspec(validator) - defaults = func_args.defaults - if defaults is None: - defaults = [] - args = func_args.args[0:len(func_args.args)-len(defaults)] + if sys.version_info[0] < 3: + func_args = getargspec(validator) + defaults = func_args.defaults + if defaults is None: + defaults = [] + args = func_args.args[0:len(func_args.args)-len(defaults)] + else: + func_params = signature(validator).parameters + args = [f.name for f in func_params.values() if f.default is f.empty] if validator_params is not None: - kwargs = validator_params.keys() + kwargs = list(validator_params.keys()) if '' in kwargs: kwargs.remove('') for kwarg in kwargs: @@ -454,7 +462,7 @@ class BaseOption(Base): name = dyn_name else: name = self.impl_getname() - if isinstance(name, unicode): + if sys.version_info[0] < 3 and isinstance(name, unicode): name = name.encode('utf8') return name @@ -1024,7 +1032,7 @@ def validate_requires_arg(new_option, multi, requires, name): operator = get_operator(require) if isinstance(expected, list): for exp in expected: - if exp.keys() != ['option', 'value']: + if set(exp.keys()) != {'option', 'value'}: raise ValueError(_('malformed requirements expected must have ' 'option and value for option {0}').format(name)) option = exp['option'] diff --git a/tiramisu/option/option.py b/tiramisu/option/option.py index 9997be5..4eabb6c 100644 --- a/tiramisu/option/option.py +++ b/tiramisu/option/option.py @@ -633,5 +633,5 @@ class DateOption(Option): return err try: datetime.datetime.strptime(value,"%Y-%m-%d") - except ValueError, err: + except ValueError: return ValueError() diff --git a/tiramisu/storage/dictionary/option.py b/tiramisu/storage/dictionary/option.py index b866330..df6692a 100644 --- a/tiramisu/storage/dictionary/option.py +++ b/tiramisu/storage/dictionary/option.py @@ -75,7 +75,7 @@ class StorageBase(object): _setattr = object.__setattr__ _setattr(self, '_name', name) if doc is not undefined: - if isinstance(doc, str): + if sys.version_info[0] < 3 and isinstance(doc, str): doc = doc.decode('utf8') _setattr(self, '_informations', {'doc': doc}) if multi != 1: