I’m creating a subclass of ConfigParser that is easier for me to use throughout my project:
class MyConfiguration(ConfigParser.ConfigParser):
def __init__(self, filename):
ConfigParser.ConfigParser.__init__(self)
self.readfp(open(filename))
def get(self, section, option):
return eval(ConfigParser.ConfigParser.get(self, section, option))
Question: are there any downsides (security, unintended consequences) to overriding the get() method with one that includes eval?
I’d rather bake the eval into the MyConfiguration class because I want to use Python data types (tuples, etc.) in my config files but I don’t want to deal with evals all over my project code.
If your only interest in
evalis literal values as you seem to indicate, then you can useast.literal_evalThis will read tuple literals, list literals and others and is safe to use because it is selective about what it will accept.
Use cases like this are exactly what this function is intended for.