bpo-29623: Make pathlib objects work with ConfigParser.read. by DavidCEllis · Pull Request #242 · python/cpython (original) (raw)
I was mainly using the pathlib support as an example of support for bytestrings as paths. You can find similar checks in the compression libraries (lzma.py, gzip.py and bz2.py).
Having it work when in a list and not work when outside of a list is the same behaviour path objects currently have except OSError is already being caught while TypeError is not so it fails silently instead of loudly. It also attempts to open all the file descriptors with the same value as the individual characters which could in theory lead to some unusual behaviour.
A somewhat contrived example
>>> import os
>>> import configparser
>>> k = os.open('/home/david/develop/cpython/Lib/test/cfgparser.1', os.O_RDONLY)
>>> k
3
>>> c = configparser.ConfigParser()
>>> c.read(b'\x03')
[3]
>>> list(c)
['DEFAULT', 'Foo Bar']
>>> os.close(k) # by hitting the file descriptor for k it has actually closed the file already
OSError: [Errno 9] Bad file descriptor
Attempting to open(b'\x03') instead of putting it through the read function gives the expected FileNotFoundError.
I'm not sure how common it is to use bytes for paths but I think it makes most sense to add both and document both (looking at it there should probably be another test if so). Doing so makes the behaviour more consistent.
Either way I think the behaviour for bytes needs to be documented.