[issue42733] io's r+ mode truncate(0)

2020-12-28 Thread 施文峰

施文峰  added the comment:

hi Terry

you are right
i download python version 3.0.1 to check this case

'''
Python 3.0.1 (r301:69556, Dec 28 2020, 14:14:02) 
[GCC 7.5.0] on linux5
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_case import test
[43552 refs]
>>> test()
beginning tell 0
tell after read 32
tell after delete content 0
tell after write 32
[52665 refs]
>>> 

'''

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42733] io's r+ mode truncate(0)

2020-12-25 Thread Steven D'Aprano

Steven D'Aprano  added the comment:

On Sat, Dec 26, 2020 at 02:19:55AM +, Terry J. Reedy wrote:

> "Enhancements" (non-bugfix feature changes) can only be applied to 
> future versions.  However, you are asking for the reversion of an 
> intentional feature change made in a 'bugfix' release# for (I believe) 
> 3.1.  Before the change, as I remember, truncating to 0 *did* move the 
> file pointer back to 0.  As I remember, Guide von Rossum requested the 
> change and Antoine Pitrou made it.

Thanks for that insight Terry. I think the current behaviour is correct. 
Sparse files can have holes in them, and non-sparse files have to be 
filled with NUL bytes, so this has to work:

f.truncate(0)
f.seek(10)
f.write('x')
# File is now ten NUL bytes and a single 'x'

If you swap the order of the truncate and the seek, the behaviour 
shouldn't change: truncate is documented as not moving the file 
position, so changing this will be backwards incompatible and will 
probably break a lot of code that expects the current behaviour.

https://docs.python.org/3/library/io.html#io.IOBase.truncate

I agree with Terry rejecting this feature request. If 施文峰 (Shīwén 
Fēng according to Google translate) wishes to disagree, this will have 
to be discussed on the Python-Ideas mailing list first.

--
title: [issue] io's r+ mode truncate(0) -> io's r+ mode truncate(0)

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42733] io's r+ mode truncate(0)

2020-12-25 Thread Terry J. Reedy

Terry J. Reedy  added the comment:

"Enhancements" (non-bugfix feature changes) can only be applied to future 
versions.  However, you are asking for the reversion of an intentional feature 
change made in a 'bugfix' release# for (I believe) 3.1.  Before the change, as 
I remember, truncating to 0 *did* move the file pointer back to 0.  As I 
remember, Guide von Rossum requested the change and Antoine Pitrou made it.  

https://docs.python.org/3/library/io.html#io.IOBase.seek
new says "The current stream position isn’t changed."

If you also want to change the stream position, do it with seek(), perhaps 
before the truncate.

# This change in a bugfix release, a violation the rule stated above, broke the 
code of multiple people.  (We thereafter strengthened the  policy.)  To fix my 
code, I had to add a seek(0).  I put it before truncate(0), so I know that this 
works.

--
nosy: +terry.reedy
resolution:  -> rejected
stage:  -> resolved
status: open -> closed
title: [issue] io's r+ mode truncate(0) -> io's r+ mode truncate(0)
versions: +Python 3.10 -Python 3.6, Python 3.7, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com