[issue18534] File "name" attribute should always be a text string

Nick Coghlan report at bugs.python.org
Tue Jul 23 07:06:58 CEST 2013


New submission from Nick Coghlan:

Currently, if a byte sequence is passed to open() as the file name, the resulting file will have that object as its name:

>>> open(os.path.expanduser(b"~/.hgrc"))
<_io.TextIOWrapper name=b'/home/ncoghlan/.hgrc' mode='r' encoding='UTF-8'>
>>> open(os.path.expanduser("~/.hgrc"))
<_io.TextIOWrapper name='/home/ncoghlan/.hgrc' mode='r' encoding='UTF-8'>

As documented in a recent post from Armin Ronacher [1], this causes the "bytes" to leak out to the public API of the file, causing problems since user code can't assume that "f.name" is a string.

When bytes are passed to open() as the filename, os.fsdecode should be used to convert them to a text string before storing them in the name attribute.


[1] http://lucumr.pocoo.org/2013/7/2/the-updated-guide-to-unicode/

----------
messages: 193585
nosy: haypo, ncoghlan
priority: normal
severity: normal
status: open
title: File "name" attribute should always be a text string

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


More information about the Python-bugs-list mailing list