From 94c6d31fa8aa92c2d7b1654caa29c7dd51733266 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Wed, 18 Jul 2012 04:45:38 +0000 Subject: [PATCH] 5443: Maximum number of generations (fixed slightly different types, such as int to long) svn: r20021 --- src/gen/utils/configmanager.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/gen/utils/configmanager.py b/src/gen/utils/configmanager.py index 7ea82e905..efc060395 100644 --- a/src/gen/utils/configmanager.py +++ b/src/gen/utils/configmanager.py @@ -40,8 +40,9 @@ import copy import logging def safe_eval(exp): - # restrict eval to empty environment - return eval(exp, {}) + # restrict eval to empty environment + return eval(exp, {}) + ##try: ## from ast import literal_eval as safe_eval ## # this leaks memory !! @@ -289,10 +290,7 @@ class ConfigManager(object): ####################### Now, let's test and set: if (name in self.default and setting in self.default[name]): - if (isinstance(value, basestring) and - isinstance(self.default[name][setting], basestring)): - self.data[name][setting] = value - elif type(value) == type(self.default[name][setting]): + if self.check_type(self.default[name][setting], value): self.data[name][setting] = value else: logging.warning("WARNING: ignoring key with wrong type " @@ -493,13 +491,8 @@ class ConfigManager(object): raise AttributeError("No such config setting name: '%s.%s'" % (section, setting)) # Check value to see if right type: - if type(value) == long: - value = int(value) if self.has_default(key): - if (isinstance(self.get_default(key), basestring) and - isinstance(value, basestring)): - pass # ok - elif type(self.get_default(key)) != type(value): + if not self.check_type(self.get_default(key), value): raise AttributeError("attempting to set '%s' to wrong type " "'%s'; should be '%s'" % (key, type(value), @@ -516,3 +509,19 @@ class ConfigManager(object): setting in self.callbacks[section]): self.emit(key) + def check_type(self, value1, value2): + """ + Check if value1 and value2 are different types. + """ + type1 = type(value1) + type2 = type(value2) + if type1 == type2: + return True + elif (isinstance(value1, basestring) and + isinstance(value2, basestring)): + return True + elif (type1 in [int, float, long] and + type2 in [int, float, long]): + return True + else: + return False