[New-bugs-announce] [issue46695] _io_TextIOWrapper_reconfigure_impl errors out too early

mirabilos report at bugs.python.org
Wed Feb 9 13:00:50 EST 2022


New submission from mirabilos <tg at debian.org>:

The following is not possible:

with open('/tmp/x.ssv', 'r', newline='\n') as f:
    f.readline()
    # imagine a library call boundary here
    if hasattr(f, 'reconfigure'):
        f.reconfigure(newline='\n')

The .reconfigure() call would not do anything, but it errors out nevertheless, simply because it is called (from reading the _io_TextIOWrapper_reconfigure_impl code in Modules/_io/textio.c).

Unfortunately, I *have* to call this in my library because I have to rely on “newline='\n'” behaviour (the hasattr avoids erroring out on binary streams), and the normal behaviour of erroring out if it’s too late to change is also good for me.

But the behaviour of erroring out if called at all when anything has already been read is a problem. This can easily be solved without breaking backwards compatibility, as the operation is a nop.

To clarify: I wish for…

with open('/tmp/x.ssv', 'r', newline='\n') as f:
    f.readline()
    # imagine a library call boundary here
    if hasattr(f, 'reconfigure'):
        f.reconfigure(newline='\n')

… to work, but for…

with open('/tmp/x.ssv', 'r') as f:
    f.readline()
    # imagine a library call boundary here
    if hasattr(f, 'reconfigure'):
        f.reconfigure(newline='\n')

… (line 1 is the only changed one) to continue to error out.

----------
components: IO
messages: 412935
nosy: mirabilos
priority: normal
severity: normal
status: open
title: _io_TextIOWrapper_reconfigure_impl errors out too early
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46695>
_______________________________________


More information about the New-bugs-announce mailing list