classification
Title: A bug of the write funtion of ConfigParser.py
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, jiangjinhu666, lukasz.langa, r.david.murray
Priority: normal Keywords:

Created on 2018-01-17 14:11 by jiangjinhu666, last changed 2021-06-18 15:14 by iritkatriel. This issue is now closed.

Messages (7)
msg310168 - (view) Author: jiangjinhu (jiangjinhu666) Date: 2018-01-17 14:11
By the write funtion of ConfigParser.py,the partial content of the configue will change.
msg310172 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-01-17 14:16
Please provide a way to reproduce the problem you are observing.  There is insufficient information here so far to understand the problem.
msg310175 - (view) Author: jiangjinhu (jiangjinhu666) Date: 2018-01-17 14:26
def write(self, fp):
        """Write an .ini-format representation of the configuration state."""
        if self._defaults:
            fp.write("[%s]\n" % DEFAULTSECT)
            for (key, value) in self._defaults.items():
                fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
            fp.write("\n")
        for section in self._sections:
            fp.write("[%s]\n" % section)
            for (key, value) in self._sections[section].items():
                if key == "__name__":
                    continue
                if (value is not None) or (self._optcre == self.OPTCRE):
                    if value == "":    #
                        value = "\"\""  #
                        key = " = ".join((key, str(value).replace('\n', '\n\t')))#
                    else:                                         #
                        key = " = ".join((key, str(value).replace('\n', '\n\t')))

      #              key = " = ".join((key, str(value).replace('\n', '\n\t')))

                fp.write("%s\n" % (key))
            fp.write("\n")
msg310176 - (view) Author: jiangjinhu (jiangjinhu666) Date: 2018-01-17 14:27
The configure file "test.ini":

[section_a]
addr = "127.0.0.1"
password = ""

Change the value of addr by the "test.py":

import ConfigParser

tmpfile = "test.ini"
conf = ConfigParser.ConfigParser()
conf.read(tmpfile)
conf.set("section_a", "addr", "\"127.0.0.2\"")
cfgfile = open(tmpfile, 'w')
conf.write(cfgfile) 

After change the value of addr to 127.0.0.2,the test.ini will be:

[section_a]
addr = "127.0.0.2"
password = 

The "" of the password lost!

And one method which will fix this bug of the write function of the ConfigParder.py:

    def write(self, fp):
        """Write an .ini-format representation of the configuration state."""
        if self._defaults:
            fp.write("[%s]\n" % DEFAULTSECT)
            for (key, value) in self._defaults.items():
                fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
            fp.write("\n")
        for section in self._sections:
            fp.write("[%s]\n" % section)
            for (key, value) in self._sections[section].items():
                if key == "__name__":
                    continue
                if (value is not None) or (self._optcre == self.OPTCRE):
                    if value == "":    #
                        value = "\"\""  #
                        key = " = ".join((key, str(value).replace('\n', '\n\t')))#
                    else:                                         #
                        key = " = ".join((key, str(value).replace('\n', '\n\t')))

    
                fp.write("%s\n" % (key))
            fp.write("\n") 


Please check it.
Thanks!
msg310179 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-01-17 14:34
I'll defer to Lukasz, but I don't believe this is a bug.  An empty value and a value of "" are equivalent in config.ini syntax, as far as I know.
msg310181 - (view) Author: jiangjinhu (jiangjinhu666) Date: 2018-01-17 14:53
But accidentally,with the "password = "" " in the configure files ,which are called by my services, the service will start successfully,and with "password =   ",the services can not.On the other hand ,I just set the option "addr = "127.0.0.1"",why the option "password = "" " will change.
Just add this.
msg396069 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-18 15:14
I trued this on 3.11/Windows and 3.10/Linux and could not reproduce the issue. The Password remained "" in both cases.
History
Date User Action Args
2021-06-18 15:14:33iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg396069

resolution: out of date
stage: resolved
2018-01-17 14:53:40jiangjinhu666setmessages: + msg310181
2018-01-17 14:34:04r.david.murraysetnosy: + lukasz.langa
messages: + msg310179
2018-01-17 14:27:42jiangjinhu666setmessages: + msg310176
2018-01-17 14:26:47jiangjinhu666setmessages: + msg310175
2018-01-17 14:16:31r.david.murraysetnosy: + r.david.murray
messages: + msg310172
2018-01-17 14:11:48jiangjinhu666create