classification
Title: configparser: TypeError occurs when handling errors in files with binary names
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: lukasz.langa Nosy List: Arfrever, lukasz.langa, python-dev
Priority: normal Keywords: 3.2regression

Created on 2013-06-19 04:18 by Arfrever, last changed 2013-06-23 17:15 by lukasz.langa. This issue is now closed.

Messages (3)
msg191443 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2013-06-19 04:18
TypeError occurs when handling errors in files with binary names. 
configparser.* exceptions are expected.
This regression was introduced in Python 3.2.

$ cat /tmp/test1
[section]
[section]
$ cat /tmp/test2
[section]
option = value
option = value
$ python3.1 -c 'import configparser; configparser.ConfigParser().readfp(open("/tmp/test1"))'
$ python3.1 -c 'import configparser; configparser.ConfigParser().readfp(open("/tmp/test2"))'
$ python3.1 -c 'import configparser; configparser.ConfigParser().readfp(open(b"/tmp/test1"))'
$ python3.1 -c 'import configparser; configparser.ConfigParser().readfp(open(b"/tmp/test2"))'
$ python3.4 -c 'import configparser; configparser.ConfigParser().read_file(open("/tmp/test1"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.4/configparser.py", line 708, in read_file
    self._read(f, source)
  File "/usr/lib64/python3.4/configparser.py", line 1061, in _read
    lineno)
configparser.DuplicateSectionError: While reading from /tmp/test1 [line  2]: section 'section' already exists
$ python3.4 -c 'import configparser; configparser.ConfigParser().read_file(open("/tmp/test2"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.4/configparser.py", line 708, in read_file
    self._read(f, source)
  File "/usr/lib64/python3.4/configparser.py", line 1087, in _read
    fpname, lineno)
configparser.DuplicateOptionError: While reading from /tmp/test2 [line  3]: option 'option' in section 'section' already exists
$ python3.4 -c 'import configparser; configparser.ConfigParser().read_file(open(b"/tmp/test1"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.4/configparser.py", line 708, in read_file
    self._read(f, source)
  File "/usr/lib64/python3.4/configparser.py", line 1061, in _read
    lineno)
  File "/usr/lib64/python3.4/configparser.py", line 202, in __init__
    Error.__init__(self, "".join(msg))
TypeError: sequence item 1: expected str instance, bytes found
$ python3.4 -c 'import configparser; configparser.ConfigParser().read_file(open(b"/tmp/test2"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.4/configparser.py", line 708, in read_file
    self._read(f, source)
  File "/usr/lib64/python3.4/configparser.py", line 1087, in _read
    fpname, lineno)
  File "/usr/lib64/python3.4/configparser.py", line 228, in __init__
    Error.__init__(self, "".join(msg))
TypeError: sequence item 1: expected str instance, bytes found
msg191711 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-06-23 17:12
New changeset 56c1227f21f5 by Łukasz Langa in branch '3.3':
Fixed issue #18260: configparser TypeError on source name specified as bytes
http://hg.python.org/cpython/rev/56c1227f21f5

New changeset 06e70937364b by Łukasz Langa in branch 'default':
Merged fix for issue #18260 from 3.3
http://hg.python.org/cpython/rev/06e70937364b
msg191712 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2013-06-23 17:15
1. Duplicate sections and options are consciously reported as errors now. This is a documented backwards-incompatible change [1]_. If you wish to revert to previous behaviour, you can set strict=False on parser creation.

2. It's really just a coincidence that specifying the filename as bytes worked in this case before on Python 3. That being said, it is indeed an unfortunate regression and was fixed in 56c1227f21f5 for 3.3 and 06e70937364b for 3.4. As a workaround for Python versions < 3.3.3 you can specify the source name as a Unicode string (e.g. `read_file(f, source=path_str)`.


.. [1] http://docs.python.org/3/library/configparser.html#customizing-parser-behaviour
History
Date User Action Args
2013-06-23 17:15:09lukasz.langasetstatus: open -> closed
type: behavior
messages: + msg191712

resolution: fixed
stage: resolved
2013-06-23 17:12:54python-devsetnosy: + python-dev
messages: + msg191711
2013-06-19 04:18:08Arfrevercreate