5443: Maximum number of generations (fixed slightly different types, such as int to long)

svn: r20021
This commit is contained in:
Doug Blank 2012-07-18 04:45:38 +00:00
parent cd5da12282
commit 94c6d31fa8

View File

@ -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