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

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

Open
throwable-one opened this issue Nov 16, 2017 · 5 comments
Open

Comments

@throwable-one
Copy link

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

See: nose-devs/nose#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
Copy link
Author

Could be fixed by removing check_encoding as suggested here #36

throwable-one added a commit to throwable-one/win-unicode-console that referenced this issue Nov 17, 2017
Several frameworks are known to monkeypatch stdout with class that does not have "encoding" field.
We must survive this case.
@Drekin
Copy link
Owner

Drekin commented Nov 27, 2017

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
Copy link

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
Copy link
Owner

Drekin commented Jan 15, 2018

@GadgetSteve Is a warning ok for you?

@GadgetSteve
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants