Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40ec4c3

Browse files
gbrandonpBrandon Parsons
authored andcommitted
pythonw in py3k sets std{in,out,err} to None
The print statement works without error, even though the stdio file objects are instances of None. Since they are instances of None, however, the encoding attribute will not exist, so protect blind attribute access of std{in,out,err} by using a new function, get_stream_enc.
1 parent 15f8e05 commit 40ec4c3

5 files changed

Lines changed: 18 additions & 9 deletions

File tree

IPython/core/splitinput.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def split_user_input(line, pattern=None):
5353
and the rest.
5454
"""
5555
# We need to ensure that the rest of this routine deals only with unicode
56-
line = py3compat.cast_unicode(line, sys.stdin.encoding or 'utf-8')
56+
encoding = py3compat.get_stream_enc(sys.stdin, 'utf-8')
57+
line = py3compat.cast_unicode(line, encoding)
5758

5859
if pattern is None:
5960
pattern = line_split

IPython/frontend/terminal/interactiveshell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
319319

320320
for i in range(hlen - hlen_before_cell):
321321
self.readline.remove_history_item(hlen - i - 1)
322-
stdin_encoding = sys.stdin.encoding or "utf-8"
322+
stdin_encoding = py3compat.get_stream_enc(sys.stdin, 'utf-8')
323323
self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
324324
stdin_encoding))
325325
return self.readline.get_current_history_length()

IPython/utils/io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ def __init__(self, stdin=None, stdout=None, stderr=None):
8787
self.stderr = IOStream(stderr, sys.stderr)
8888

8989
# 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)
9393

9494

9595
class Tee(object):

IPython/utils/py3compat.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
def no_code(x, encoding=None):
1212
return x
1313

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+
1421
def decode(s, encoding=None):
15-
encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
22+
encoding = get_stream_enc(sys.stdin, encoding) or sys.getdefaultencoding()
1623
return s.decode(encoding, "replace")
1724

1825
def encode(u, encoding=None):
19-
encoding = encoding or sys.stdin.encoding or sys.getdefaultencoding()
26+
encoding = get_stream_enc(sys.stdin, encoding) or sys.getdefaultencoding()
2027
return u.encode(encoding, "replace")
21-
28+
29+
2230
def cast_unicode(s, encoding=None):
2331
if isinstance(s, bytes):
2432
return decode(s, encoding)

IPython/utils/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def getdefaultencoding():
4747
and finally to sys.getdefaultencoding() which is the most conservative option,
4848
and usually ASCII.
4949
"""
50-
enc = sys.stdin.encoding
50+
enc = py3compat.get_stream_enc(sys.stdin)
5151
if not enc or enc=='ascii':
5252
try:
5353
# There are reports of getpreferredencoding raising errors

0 commit comments

Comments
 (0)