Skip to content

sphinx.util.logging: Make write() methods return int #13595

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions sphinx/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,13 @@ def __init__(self, stream: IO[str]) -> None:
self.stream = stream
self.encoding = getattr(stream, 'encoding', 'ascii') or 'ascii'

def write(self, data: str) -> None:
def write(self, data: str) -> int:
try:
self.stream.write(data)
return self.stream.write(data)
except UnicodeEncodeError:
# stream accept only str, not bytes. So, we encode and replace
# non-encodable characters, then decode them.
self.stream.write(
return self.stream.write(
data.encode(self.encoding, 'replace').decode(self.encoding)
)
Comment on lines +586 to 594
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srittau what is the return value here meant to mean? In one branch it is the number of characters written, in the other it is the number of bytes. This doesn't make sense. Please can we keep None as a valid return value for the (small-p) protocol?

A

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typing.Writer protocol (as well as the legacy typing.IO pseudo-protocol) require an int return type. If you keep the None type here, you will need to # type: ignore this.


Expand All @@ -604,8 +604,9 @@ class LastMessagesWriter:
def __init__(self, app: Sphinx, stream: IO[str]) -> None:
self.app = app

def write(self, data: str) -> None:
def write(self, data: str) -> int:
self.app.messagelog.append(data)
return len(data)


def setup(app: Sphinx, status: IO[str], warning: IO[str]) -> None:
Expand Down
Loading