Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open with w and a, write/seek/truncate/tell #300

Closed
agroce opened this issue Sep 18, 2017 · 8 comments
Closed

open with w and a, write/seek/truncate/tell #300

agroce opened this issue Sep 18, 2017 · 8 comments
Labels

Comments

@agroce
Copy link

agroce commented Sep 18, 2017

import pyfakefs.fake_filesystem
import os

fs = pyfakefs.fake_filesystem.FakeFilesystem()
fs.CreateDirectory('/Volumes')
fs.CreateDirectory('/Volumes/ramdisk')
fs.CreateDirectory('/Volumes/ramdisk/test')
os0 = pyfakefs.fake_filesystem.FakeOsModule(fs)
opener0 = pyfakefs.fake_filesystem.FakeFileOpen(fs)

component0 = "alpha"
path0 = "/Volumes/ramdisk/test"
path0 += "/" + component0
bytes0 = 0
bytes0 += 1
str0 = ""
str0 += 'a'
str0 += 'a'
str0 += 'a'
str0 += 'a'
str0 += 'a'
str0 += 'a'
file0 = opener0(path0,'w')
file1 = opener0(path0,'a')
file1.write(str0)
file0.seek(bytes0)
file0.truncate()
result = file1.tell()

print result

os 7 vs. pyfakefs 6

@mrbean-bremen
Copy link
Member

Hm, can't reproduce this one. I get 6 in pyfakefs.

@agroce
Copy link
Author

agroce commented Sep 18, 2017

oops, reversed pyfakefs and os!

@agroce
Copy link
Author

agroce commented Sep 18, 2017

updated

@mrbean-bremen
Copy link
Member

Hm, I don't understand why it shall be 7 - why not 6? After writing 6 bytes the position shall not have moved - what am I missing?

@agroce
Copy link
Author

agroce commented Sep 18, 2017

Not sure. This extension produces the same file contents, but the tell result is different.

component0 = "alpha"
path0 = "/Volumes/ramdisk/test"
path0 += "/" + component0
bytes0 = 0
bytes0 += 1
str0 = ""
str0 += 'a'
str0 += 'a'
str0 += 'a'
str0 += 'a'
str0 += 'a'
str0 += 'a'
file0 = opener0(path0,'w')
file1 = opener0(path0,'a')
file1.write(str0)
file0.seek(bytes0)
file0.truncate()
file1.write('b')
file0.close()
file1.close()
file2 = opener0(path0,'r')
print file2.readline()

Hrm.

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Sep 18, 2017

Hmm. Have to understand this first, otherwise I have no idea how to fix it... I can reproduce the behavior under Windows (e.g. tell = 7), at least under Python 3, so there shall be a reason...

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Sep 19, 2017

I have still no real idea why this happens. It does not happen under Windows in Python 2, but happens in Python 3 (same as flush on reading, probably related). I happens only if file1 is open in append mode, file0 can be opened in both write or append mode. After the seek zeros are written to the file until the seek position, and the previously written content is appended to this. This happens the moment file1.tell() or file1.close() is called, e.g. on flush of file1.
Ok, that means that before flush() in append mode the position is reset to the current end of file - that makes some sense.
Note that I'm writing this more or less for my own reference, just thinking aloud... don't have a rubber duck here ;)

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Jun 10, 2018
@mrbean-bremen
Copy link
Member

Reopen as the fix broke other things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants