Skip to content

Commit

Permalink
Fix windows encoding detection: encoding depends on python build options
Browse files Browse the repository at this point in the history
* Normalize Windows OS detection to `sys.platform == "win32"`

Fix urwid#690
  • Loading branch information
penguinolog committed Dec 9, 2023
1 parent db62c81 commit 3738f8a
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions tests/test_doctests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import doctest
import os
import sys
import unittest

import urwid
Expand Down Expand Up @@ -56,7 +56,7 @@ def load_tests(loader: unittest.TestLoader, tests: unittest.BaseTestSuite, ignor
else:
tests.addTests(doctest.DocTestSuite(curses_display, optionflags=option_flags))

if os.name == "nt":
if sys.platform == "win32":
tests.addTests(doctest.DocTestSuite("urwid._win32_raw_display", optionflags=option_flags))
else:
tests.addTests(doctest.DocTestSuite("urwid._posix_raw_display", optionflags=option_flags))
Expand Down
3 changes: 1 addition & 2 deletions tests/test_event_loops.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import asyncio
import os
import socket
import sys
import typing
Expand All @@ -12,7 +11,7 @@
if typing.TYPE_CHECKING:
from types import TracebackType

IS_WINDOWS = os.name == "nt"
IS_WINDOWS = sys.platform == "win32"


class ClosingSocketPair(typing.ContextManager[typing.Tuple[socket.socket, socket.socket]]):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_vterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import urwid
from urwid.util import set_temporary_encoding

IS_WINDOWS = os.name == "nt"
IS_WINDOWS = sys.platform == "win32"


class DummyCommand:
Expand Down
4 changes: 2 additions & 2 deletions urwid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from __future__ import annotations

import os
import sys

from urwid import raw_display
from urwid.canvas import (
Expand Down Expand Up @@ -231,7 +231,7 @@
pass

# OS Specific
if os.name != "nt":
if sys.platform != "win32":
from urwid.vterm import TermCanvas, TermCharset, Terminal, TermModes, TermScroller

# ZMQEventLoop cause interpreter crash on windows
Expand Down
4 changes: 2 additions & 2 deletions urwid/curses_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from __future__ import annotations

import curses
import os
import sys
import typing
from contextlib import suppress

Expand All @@ -37,7 +37,7 @@
if typing.TYPE_CHECKING:
from typing_extensions import Literal

IS_WINDOWS = os.name == "nt"
IS_WINDOWS = sys.platform == "win32"

# curses.KEY_RESIZE (sometimes not defined)
if IS_WINDOWS:
Expand Down
2 changes: 1 addition & 1 deletion urwid/display_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from typing_extensions import Literal, Self

IS_WINDOWS = os.name == "nt"
IS_WINDOWS = sys.platform == "win32"

if not IS_WINDOWS:
import termios
Expand Down
4 changes: 2 additions & 2 deletions urwid/escape.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

from __future__ import annotations

import os
import re
import sys
from collections.abc import MutableMapping, Sequence

try:
Expand All @@ -37,7 +37,7 @@
# from urwid.util import is_mouse_event -- will not work here
import urwid.util

IS_WINDOWS = os.name == "nt"
IS_WINDOWS = sys.platform == "win32"

within_double_byte = str_util.within_double_byte

Expand Down
4 changes: 2 additions & 2 deletions urwid/event_loop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

import os
import sys

from .abstract_loop import EventLoop, ExitMainLoop
from .asyncio_loop import AsyncioEventLoop
Expand Down Expand Up @@ -39,7 +39,7 @@
except ImportError:
pass

if os.name != "nt":
if sys.platform != "win32":
# ZMQEventLoop cause interpreter crash on windows
try:
from .zmq_loop import ZMQEventLoop
Expand Down
4 changes: 2 additions & 2 deletions urwid/raw_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

from __future__ import annotations

import os
import sys

__all__ = ("Screen",)

IS_WINDOWS = os.name == "nt"
IS_WINDOWS = sys.platform == "win32"

if IS_WINDOWS:
from ._win32_raw_display import Screen
Expand Down
9 changes: 9 additions & 0 deletions urwid/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import codecs
import contextlib
import sys
import typing
import warnings
from contextlib import suppress
Expand All @@ -45,6 +46,14 @@


def detect_encoding() -> str:
# Windows is a special case:
# CMD is Unicode and non-unicode at the same time:
# Unicode display support depends on C API usage and partially limited by font settings.
# By default, python is distributed in "Unicode" version, and since Python version 3.8 only Unicode.
# In case of curses, "windows-curses" is distributed in unicode-only.
# Since Windows 10 default console font is already unicode.
if sys.platform == "win32" and sys.getdefaultencoding() == "utf-8":
return "urf-8"
# Try to determine if using a supported double-byte encoding
import locale

Expand Down

0 comments on commit 3738f8a

Please sign in to comment.