Show More
@@ -53,7 +53,8 b' def split_user_input(line, pattern=None):' | |||
|
53 | 53 | and the rest. |
|
54 | 54 | """ |
|
55 | 55 | # We need to ensure that the rest of this routine deals only with unicode |
|
56 |
|
|
|
56 | encoding = py3compat.get_stream_enc(sys.stdin, 'utf-8') | |
|
57 | line = py3compat.cast_unicode(line, encoding) | |
|
57 | 58 | |
|
58 | 59 | if pattern is None: |
|
59 | 60 | pattern = line_split |
@@ -319,7 +319,7 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
319 | 319 | |
|
320 | 320 | for i in range(hlen - hlen_before_cell): |
|
321 | 321 | self.readline.remove_history_item(hlen - i - 1) |
|
322 |
stdin_encoding = sys.stdin |
|
|
322 | stdin_encoding = py3compat.get_stream_enc(sys.stdin, 'utf-8') | |
|
323 | 323 | self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(), |
|
324 | 324 | stdin_encoding)) |
|
325 | 325 | return self.readline.get_current_history_length() |
@@ -87,9 +87,9 b' class IOTerm:' | |||
|
87 | 87 | self.stderr = IOStream(stderr, sys.stderr) |
|
88 | 88 | |
|
89 | 89 | # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr |
|
90 | stdin = IOStream(sys.stdin) | |
|
91 | stdout = IOStream(sys.stdout) | |
|
92 | stderr = IOStream(sys.stderr) | |
|
90 | stdin = sys.stdin if not sys.stdin else IOStream(sys.stdin) | |
|
91 | stdout = sys.stdout if not sys.stdout else IOStream(sys.stdout) | |
|
92 | stderr = sys.stderr if not sys.stderr else IOStream(sys.stderr) | |
|
93 | 93 | |
|
94 | 94 | |
|
95 | 95 | class Tee(object): |
@@ -11,14 +11,22 b' orig_open = open' | |||
|
11 | 11 | def no_code(x, encoding=None): |
|
12 | 12 | return x |
|
13 | 13 | |
|
14 | # to deal with the possibility of sys.std* not being a stream at all | |
|
15 | def get_stream_enc(stream, default=None): | |
|
16 | if not hasattr(stream, 'encoding') or not stream.encoding: | |
|
17 | return default | |
|
18 | else: | |
|
19 | return stream.encoding | |
|
20 | ||
|
14 | 21 | def decode(s, encoding=None): |
|
15 |
encoding = |
|
|
22 | encoding = get_stream_enc(sys.stdin, encoding) or sys.getdefaultencoding() | |
|
16 | 23 | return s.decode(encoding, "replace") |
|
17 | 24 | |
|
18 | 25 | def encode(u, encoding=None): |
|
19 |
encoding = |
|
|
26 | encoding = get_stream_enc(sys.stdin, encoding) or sys.getdefaultencoding() | |
|
20 | 27 | return u.encode(encoding, "replace") |
|
21 | 28 | |
|
29 | ||
|
22 | 30 | def cast_unicode(s, encoding=None): |
|
23 | 31 | if isinstance(s, bytes): |
|
24 | 32 | return decode(s, encoding) |
@@ -47,7 +47,7 b' def getdefaultencoding():' | |||
|
47 | 47 | and finally to sys.getdefaultencoding() which is the most conservative option, |
|
48 | 48 | and usually ASCII. |
|
49 | 49 | """ |
|
50 | enc = sys.stdin.encoding | |
|
50 | enc = py3compat.get_stream_enc(sys.stdin) | |
|
51 | 51 | if not enc or enc=='ascii': |
|
52 | 52 | try: |
|
53 | 53 | # There are reports of getpreferredencoding raising errors |
General Comments 0
You need to be logged in to leave comments.
Login now