This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: _io_TextIOWrapper_reconfigure_impl errors out too early
Type: Stage:
Components: IO Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mirabilos
Priority: normal Keywords:

Created on 2022-02-09 18:00 by mirabilos, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg412935 - (view) Author: mirabilos (mirabilos) Date: 2022-02-09 18:00
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.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90853
2022-02-09 18:00:50mirabiloscreate