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

Fix unicode decode error caused by latin-1 encoding when sending the banner #1120

Merged
merged 3 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changes/1120.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix unicode decode error caused by `latin-1` encoding when sending the banner.
8 changes: 3 additions & 5 deletions hikari/internal/ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import re
import string
import sys
import time
import typing
import warnings

Expand Down Expand Up @@ -223,10 +222,9 @@ def print_banner(
for code in colorlog.escape_codes.escape_codes:
args[code] = ""

sys.stdout.write(string.Template(raw_banner).safe_substitute(args))
# Give the stream some time to flush
sys.stdout.flush()
time.sleep(0.125)
# 1 maps to stdout
with open(1, "w", encoding="utf-8") as stdout:
stdout.write(string.Template(raw_banner).safe_substitute(args))


def supports_color(allow_color: bool, force_color: bool) -> bool:
Expand Down
16 changes: 10 additions & 6 deletions tests/hikari/internal/test_ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import builtins
import contextlib
import importlib
import logging
Expand Down Expand Up @@ -180,7 +181,7 @@ def test_when_supports_color(self, mock_args):
supports_color = stack.enter_context(mock.patch.object(ux, "supports_color", return_value=True))
read_text = stack.enter_context(mock.patch.object(importlib.resources, "read_text"))
template = stack.enter_context(mock.patch.object(string, "Template"))
write = stack.enter_context(mock.patch.object(sys.stdout, "write"))
builtins_open = stack.enter_context(mock.patch.object(builtins, "open"))
abspath = stack.enter_context(mock.patch.object(os.path, "abspath", return_value="some path"))
dirname = stack.enter_context(mock.patch.object(os.path, "dirname"))

Expand All @@ -207,7 +208,8 @@ def test_when_supports_color(self, mock_args):

template.assert_called_once_with(read_text())
template().safe_substitute.assert_called_once_with(args)
write.assert_called_once_with(template().safe_substitute())
builtins_open.assert_called_once_with(1, "w", encoding="utf-8")
builtins_open.return_value.__enter__.return_value.write.assert_called_once_with(template().safe_substitute())
dirname.assert_called_once_with("~/hikari")
abspath.assert_called_once_with(dirname())
supports_color.assert_called_once_with(True, False)
Expand All @@ -221,9 +223,9 @@ def test_when_doesnt_supports_color(self, mock_args):
supports_color = stack.enter_context(mock.patch.object(ux, "supports_color", return_value=False))
read_text = stack.enter_context(mock.patch.object(importlib.resources, "read_text"))
template = stack.enter_context(mock.patch.object(string, "Template"))
write = stack.enter_context(mock.patch.object(sys.stdout, "write"))
abspath = stack.enter_context(mock.patch.object(os.path, "abspath", return_value="some path"))
dirname = stack.enter_context(mock.patch.object(os.path, "dirname"))
builtins_open = stack.enter_context(mock.patch.object(builtins, "open"))

with stack:
ux.print_banner("hikari", True, False)
Expand All @@ -248,18 +250,19 @@ def test_when_doesnt_supports_color(self, mock_args):

template.assert_called_once_with(read_text())
template().safe_substitute.assert_called_once_with(args)
write.assert_called_once_with(template().safe_substitute())
dirname.assert_called_once_with("~/hikari")
abspath.assert_called_once_with(dirname())
supports_color.assert_called_once_with(True, False)
builtins_open.assert_called_once_with(1, "w", encoding="utf-8")
builtins_open.return_value.__enter__.return_value.write.assert_called_once_with(template().safe_substitute())

def test_use_extra_args(self, mock_args):
stack = contextlib.ExitStack()
stack.enter_context(mock.patch.object(colorlog.escape_codes, "escape_codes", new={}))
stack.enter_context(mock.patch.object(time, "sleep"))
read_text = stack.enter_context(mock.patch.object(importlib.resources, "read_text"))
template = stack.enter_context(mock.patch.object(string, "Template"))
write = stack.enter_context(mock.patch.object(sys.stdout, "write"))
builtins_open = stack.enter_context(mock.patch.object(builtins, "open"))
stack.enter_context(mock.patch.object(os.path, "abspath", return_value="some path"))

extra_args = {
Expand Down Expand Up @@ -289,7 +292,8 @@ def test_use_extra_args(self, mock_args):

template.assert_called_once_with(read_text())
template().safe_substitute.assert_called_once_with(args)
write.assert_called_once_with(template().safe_substitute())
builtins_open.assert_called_once_with(1, "w", encoding="utf-8")
builtins_open.return_value.__enter__.return_value.write.assert_called_once_with(template().safe_substitute())

def test_overwrite_args_raises_error(self, mock_args):
stack = contextlib.ExitStack()
Expand Down