Drekin / win-unicode-console

A Python package to enable Unicode support when running Python from Windows console.
MIT License
103 stars 12 forks source link

File warning instead of fail if stdout does not have encoding #37

Open throwable-one opened 6 years ago

throwable-one commented 6 years ago

According to Python manual sys.stdout should have encoding. Unfortunatelly, nose monekypatches it with StringIO with out of encoding.

See: https://github.com/nose-devs/nose/issues/1065

Its nose fault, not win-unicode-console fault. But can we please do

def check_encodings():
    try:
        if sys.stdin.encoding != sys.stdout.encoding:
            # raise RuntimeError("sys.stdin.encoding != sys.stdout.encoding, readline hook doesn't know, which one to use to decode prompt")

            warnings.warn("sys.stdin.encoding == {!r}, whereas sys.stdout.encoding == {!r}, readline hook consumer may assume they are the same".format(sys.stdin.encoding, sys.stdout.encoding),
                RuntimeWarning, stacklevel=3)
    except AttributeError:
        warnings.warn("stdin or stdout does not have encoding")

?

throwable-one commented 6 years ago

Could be fixed by removing check_encoding as suggested here https://github.com/Drekin/win-unicode-console/issues/36

Drekin commented 6 years ago

Does the solution of #36 work for you? Also thank you for the pull request, but I'll probably add something like

no_encoding = object()
stdin_encoding = getattr(sys.stdin, "encoding", no_encoding)
if stdin_encoding is no_encoding: warn()

So, stdin and stdout cases are distinguished. Also, there is probably a reason to use stacklevel=3 in the warn, so I would use it here also.

GadgetSteve commented 6 years ago

Note that I have also had an issue with this problem when using py2exe on code that uses this module. (It has a Blackhole class that substitutes for sys.stdout and doesn't have an encoding property) In my case I found that getattr(sys.stdout, "encoding", None) worked fine.

Drekin commented 6 years ago

@GadgetSteve Is a warning ok for you?

GadgetSteve commented 6 years ago

@Drekin A clear warning is always better than a crash.