[New-bugs-announce] [issue27351] Unexpected ConfigParser.read() behavior when passed fileobject

Rich Rauenzahn report at bugs.python.org
Sun Jun 19 00:50:27 EDT 2016


New submission from Rich Rauenzahn:

This came up on StackOverflow: http://stackoverflow.com/a/37903779/2077386

I wanted to bring it to your attention in case it hasn't been notice before.

It turns out that if you pass a fileobject (i.e., ConfigParser().read(open("foo"))) ConfigParser.read() will look at the argument, see it isn't a basestring, and then will iterate over it.  fileobjects are iterables.

This results in iterating over the contents of the file 'foo'.  It then attempts to open a file named after every line read from 'foo'.

For example, I made a file foo and filled it with a-g, each letter per line.  

strace shows:

open("foo", O_RDONLY)                     = 3
open("a\n", O_RDONLY)                   = -1 ENOENT (No such file or directory)
open("b\n", O_RDONLY)                   = -1 ENOENT (No such file or directory)
open("c\n", O_RDONLY)                   = -1 ENOENT (No such file or directory)
open("d\n", O_RDONLY)                   = -1 ENOENT (No such file or directory)
open("e\n", O_RDONLY)                   = -1 ENOENT (No such file or directory)
open("f\n", O_RDONLY)                   = -1 ENOENT (No such file or directory)
open("g\n", O_RDONLY)                   = -1 ENOENT (No such file or directory)

...and since the API is designed to ignore files it can't open, it just ignores the open errors.

I wonder if this fileobject case ought to be checked for when checking the arguments passed into ConfigParser.read().  

Thank you.

----------
components: Library (Lib)
messages: 268838
nosy: Rich.Rauenzahn
priority: normal
severity: normal
status: open
title: Unexpected ConfigParser.read() behavior when passed fileobject
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27351>
_______________________________________


More information about the New-bugs-announce mailing list