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

2020-12-25 Thread Steven D'Aprano

Steven D'Aprano  added the comment:

On Fri, Dec 25, 2020 at 01:31:51PM +, 施文峰 wrote:

> first test have a problem,you didn’t use r+ mode

I did, I copied your `test()` function exactly, however I did make a 
mistake. I tried again with this:

>>> with open(FILE_PATH, 'r') as f:
... print(repr(f.read()))
... 
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00{"how_dare_you":
 
"how_dare_you"}'

so you are correct, the file is padded with NUL characters.

Here is a simpler demonstration of the behaviour.

```
FILE_PATH = 'example.data'

# create a file
with open(FILE_PATH, 'w') as f:
f.write('abc\n')

# Truncate using r+ mode.
with open(FILE_PATH, 'r+') as f:
assert f.tell() == 0
assert f.read() == 'abc\n'
assert f.tell() == 4  # File position is now at end of file.
f.truncate(0)
assert f.tell() == 4  # File position has not changed.
assert f.read() == ''  # Nothing remaining to read.
f.write('xyz\n')
f.flush()
assert f.tell() == 8
assert f.read() == ''  # Nothing remaining to read.
# Return the file position to start of file.
f.seek(0)
assert f.read() == '\0\0\0\0xyz\n'

```

All the assertions pass.

I think this is standard and correct behaviour. Do you have examples of 
other programming languages that behave differently? PHP seems to do the 
same thing:

https://www.php.net/manual/en/function.ftruncate.php

--
title: io's r+ mode truncate(0) -> [issue] 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] [issue] io's r+ mode truncate(0)

2020-12-25 Thread 施文峰

施文峰  added the comment:

hi Steven

thanks for check my post

first test have a problem,you didn’t use r+ mode
step is 
1. f.read
2. f.truncate(0)
3. f.write(something)

my test msg tell you

“
tell after delete content 33
content 0 
tell after write 65
“

so you know after truncate f.tell =33
but no content in file
and after you write something into file
msg tell you f.tell =65
but “ {"how_dare_you": "how_dare_you"}” is only 33 length 
so you know must have someing in file

please use editor open the file
you will find it

--

___
Python tracker 

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



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

2020-12-25 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

You say:

>  after process python3 test_case.py
>  json file's content like this
>  
>  @@{"how_dare_you": "how_dare_you"}


I cannot replicate that result.


I created a "data.json" with the following content:


```
>>> with open(FILE_PATH, 'w') as f:
... f.write('{"how_dare_you": "how_dare_you"}\n')
... 
33
>>> with open(FILE_PATH, 'r') as f:
... print(f.read())
... 
{"how_dare_you": "how_dare_you"}

```


Then I ran your `test` function and the only result was to delete the newline 
at the end of the file:


```
>>> test()
beginning tell 0
tell after read 33
tell after delete content 33
content 0 
tell after write 65
content 0 
>>> 
>>> with open(FILE_PATH, 'r') as f:
... print(f.read())
... 
{"how_dare_you": "how_dare_you"}
>>> 
```

So I cannot replicate your reported bug. There is no sign of any garbage 
characters inserted at the start of the file as you state.

--
nosy: +steven.daprano

___
Python tracker 

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



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

2020-12-24 Thread 施文峰

New submission from 施文峰 :

happen at io's reading & updating(r+) mode
after read,
file object's postion stay in last if you remove whole content ( truncate(0) ),
postion wil not back to 0 still stay in the last
then you start writing from last position(not 0)
that's why problem happen

test case can check here https://github.com/841020/open_source

--
components: IO
files: Screenshot from 2020-12-25 10-46-42.png
hgrepos: 396
messages: 383710
nosy: ke265379ke
priority: normal
severity: normal
status: open
title: [issue] io's r+ mode truncate(0)
type: enhancement
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49697/Screenshot from 2020-12-25 
10-46-42.png

___
Python tracker 

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