5443: Maximum number of generations (fixed slightly different types, such as int to long)
svn: r20023
This commit is contained in:
parent
76e1edbd82
commit
d6cb6934fe
@ -40,8 +40,9 @@ import copy
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
def safe_eval(exp):
|
def safe_eval(exp):
|
||||||
# restrict eval to empty environment
|
# restrict eval to empty environment
|
||||||
return eval(exp, {})
|
return eval(exp, {})
|
||||||
|
|
||||||
##try:
|
##try:
|
||||||
## from ast import literal_eval as safe_eval
|
## from ast import literal_eval as safe_eval
|
||||||
## # this leaks memory !!
|
## # this leaks memory !!
|
||||||
@ -289,10 +290,7 @@ class ConfigManager(object):
|
|||||||
####################### Now, let's test and set:
|
####################### Now, let's test and set:
|
||||||
if (name in self.default and
|
if (name in self.default and
|
||||||
setting in self.default[name]):
|
setting in self.default[name]):
|
||||||
if (isinstance(value, basestring) and
|
if self.check_type(self.default[name][setting], value):
|
||||||
isinstance(self.default[name][setting], basestring)):
|
|
||||||
self.data[name][setting] = value
|
|
||||||
elif type(value) == type(self.default[name][setting]):
|
|
||||||
self.data[name][setting] = value
|
self.data[name][setting] = value
|
||||||
else:
|
else:
|
||||||
logging.warning("WARNING: ignoring key with wrong type "
|
logging.warning("WARNING: ignoring key with wrong type "
|
||||||
@ -493,13 +491,8 @@ class ConfigManager(object):
|
|||||||
raise AttributeError("No such config setting name: '%s.%s'" %
|
raise AttributeError("No such config setting name: '%s.%s'" %
|
||||||
(section, setting))
|
(section, setting))
|
||||||
# Check value to see if right type:
|
# Check value to see if right type:
|
||||||
if type(value) == long:
|
|
||||||
value = int(value)
|
|
||||||
if self.has_default(key):
|
if self.has_default(key):
|
||||||
if (isinstance(self.get_default(key), basestring) and
|
if not self.check_type(self.get_default(key), value):
|
||||||
isinstance(value, basestring)):
|
|
||||||
pass # ok
|
|
||||||
elif type(self.get_default(key)) != type(value):
|
|
||||||
raise AttributeError("attempting to set '%s' to wrong type "
|
raise AttributeError("attempting to set '%s' to wrong type "
|
||||||
"'%s'; should be '%s'" %
|
"'%s'; should be '%s'" %
|
||||||
(key, type(value),
|
(key, type(value),
|
||||||
@ -516,3 +509,19 @@ class ConfigManager(object):
|
|||||||
setting in self.callbacks[section]):
|
setting in self.callbacks[section]):
|
||||||
self.emit(key)
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user