What are the differences between ConfigParser
and SafeConfigParser
? And why, exactly, is the latter is safer? What's 'unsafe' about ConfigParser
? I know that SafeConfigParser
inherited the ConfigParser
, what did it do different?
This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily.
configparser comes from Python 3 and as such it works well with Unicode.
Just use a StringIO object and the configparser's write method. It looks like the only method for "printing" the contents of a config object is ConfigParser. write which takes a file-like object. io.
Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.
The SafeConfigParser implements a different set(section, option, value)
method which will raise a NoSectionError if the section does not exists, and a TypeError
if value
is not a string.
This allows more control over the behaviour of the parser, an example from documentation:
try:
config.set(section2, option, config.get(section1, option, 1))
except ConfigParser.NoSectionError:
# Create non-existent section
config.add_section(section2)
opt_move(config, section1, section2, option)
else:
config.remove_option(section1, option)
From documentation: It also support interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization.
Update
I just checked the source code of the SafeConfigParser
, and even if it is true that ConfigParser
also allows interpolation, SafeConfigParser
provides an updated version of it that documentation describes as a more-sane and more predictable variant of the magical interpolation feature.
For example, it will raise an InterpolationSyntaxError
in the event of a bad reference or a syntax error after a '%' character.
Update 2
That could be useful to precise that the SafeConfigParser
class has been renamed to ConfigParser in Python 3.2. If you wonder which of the SafeConfigParser
or the ConfigParser
you should use in python 2.7, use the first (unless you have a very specific reason to use the second)
You could also make easier your future transition to python 3+, (which should happen soon) by doing:
from ConfigParser import SafeConfigParser as ConfigParser
SafeConfigParser
is...
Derived class of ConfigParser that implements a more-sane variant of the magical interpolation feature. This implementation is more predictable as well. New applications should prefer this version if they don’t need to be compatible with older versions of Python.
I think SafeConfigParser
does not seem to consider Python version compatibility. ConfigParser
also exists in Python 3 version, but SafeConfigParser
does not exist. Exactly, SafeConfigParser
is renamed ConfigParser
and ConfigParser
is removed in 3.2
. See this question.
So I think differences between ConfigParser
and SafeConfigParser
are usability and version compatibility.
UPDATE:
SafeConfigParser
is just safer than ConfigParser
. That's not to say that ConfigParser
isn't safe. I tried to figure out what was safer. It supports more-sane variant of the magical interpolation and it is more stricter than ConfigParser
.
So why SafeConfigParser is safe?
The answer is SafeConfigParser
is more strict. Example of strict is in @olinox14 answer.
The fact that SafeConfigParser
has become the default ConfigParser
in Python 3 doesn't necessarily mean you need to separate them.
Ultimately, SafeConfigParser
is more strict. And it is recommended to use SafeConfigParser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With