question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

open(..., 'w') ... flush() fails to write data to the file

See original GitHub issue

Repro:

with open('test.txt', 'w') as test:
  test.write('hi')
  test.flush()
  r = open('test.txt', 'r')
  print(r.read(1))
  r.close()

Expected: ‘h’ printed Actual: ‘’ printed

This seems to be a regression from IronPython 2.

Side note: what surprises me is that in both 3 and 2 BufferedWriter.flush() does not call .flush() on the underlying _raw/_rawIO, but somehow it works in IronPython 2. I’d appreciate an explanation. Does IP2 create the underlying stream with buffering off?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sloziercommented, Nov 2, 2020

My example is a counter-example to the statement that BufferedWriter should call the underlying IO object’s flush. If it did call flush, then the sample above would fail. You can manually flush the raw and see the difference:

import io
b = io.BytesIO()
f = io.BufferedWriter(io.BufferedWriter(b))
f.write(b"a")
f.flush()
assert b.getvalue() == b""
f.raw.flush()
assert b.getvalue() == b"a"
0reactions
lostmsucommented, Nov 2, 2020

@slozier I am not sure what do you mean here. BufferedWriter should be calling the underlying IO object’s flush, so if flush is fixed to behave correctly, this would pass.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How often does python flush to a file? - Stack Overflow
From the docs: Note: flush() does not necessarily write the file's data to disk. Use flush() followed by os.fsync() to ensure this behavior....
Read more >
Python File flush() Method - Tutorialspoint
Description. Python file method flush() flushes the internal buffer, like stdio's fflush. This may be a no-op on some file-like objects.
Read more >
File flush() method in Python - GeeksforGeeks
The flush() method in Python file handling clears the internal buffer of the file. In Python, files are automatically flushed while closing ...
Read more >
File Reading and Writing Methods - Python 2.7 Tutorial
Create a file object using the open() function. Along with the file name, specify: 'r' for reading in an existing file (default; can...
Read more >
flush() [File]
Writes to disk a file previously opened with create( ) or open( ) without closing the file. Returns true if successful and false...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found