Skip to content

Commit

Permalink
Let patched sys.stderr.write() and sys.stdout.write() return number o…
Browse files Browse the repository at this point in the history
…f written bytes

On Python 3, a file’s write() method is expected to return the number of
written bytes.

It’s a bit unclear what do when no data is actually written (log file
couldn’t be opened in case of stderr or data is deliberately ignored in
case of stdout). I think it’s reasonable in this case to pretend that
all passed data was written. If we returned 0, a caller which calls
write() repeatedly until all data has been written will end up in an
infinite loop.
  • Loading branch information
Manuel Jacob committed May 26, 2022
1 parent b183301 commit 9da5416
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions py2exe/boot_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ def write(self, text, alert=ctypes.windll.user32.MessageBoxW,
"Errors in %r" % os.path.basename(sys.executable),
0)
if self._file is not None:
self._file.write(text)
n_written = self._file.write(text)
self._file.flush()
return n_written
else:
return len(text)
def flush(self):
if self._file is not None:
self._file.flush()
Expand All @@ -82,7 +85,7 @@ def flush(self):
class Blackhole(object):
softspace = 0
def write(self, text):
pass
return len(text)
def flush(self):
pass
sys.stdout = Blackhole()
Expand Down

0 comments on commit 9da5416

Please sign in to comment.