question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File warning instead of fail if stdout does not have encoding

See original GitHub issue

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")

?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Drekincommented, 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.

0reactions
GadgetStevecommented, Jan 15, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting the correct encoding when piping stdout in Python
Your code works when run in an script because Python encodes the output to whatever encoding your terminal application is using.
Read more >
Command output not displayed in Windows #201 - GitHub
Under Python 3, if you don't specify an encoding when opening a file, you will get os.device_encoding(fd.fileno()) if the file is a terminal ......
Read more >
subprocess — Subprocess management — Python 3.11.1 ...
If encoding or errors are specified, or text is true, the file objects stdin, stdout and stderr are opened in text mode with...
Read more >
Encoding of Python stdout - Exterior Memory - MacFreek
Python determines the encoding of stdout and stderr based on the value of the LC_CTYPE variable, but only if the stdout is a...
Read more >
Python 3 Support — Click Documentation (4.x)
This causes problems if the terminal is incorrectly set and Python does not figure out the encoding. In that case, the Unicode string...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found