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

Skip to content

Commit 39e70a4

Browse files
Issue #9290: In IDLE the sys.std* streams now implement io.TextIOBase
interface and support all mandatory methods and properties.
1 parent 3e6e2ac commit 39e70a4

3 files changed

Lines changed: 81 additions & 71 deletions

File tree

Lib/idlelib/PyShell.py

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,8 @@ def start_subprocess(self):
417417
except socket.timeout as err:
418418
self.display_no_subprocess_error()
419419
return None
420-
# Can't regiter self.tkconsole.stdin, since run.py wants to
421-
# call non-TextIO methods on it (such as getvar)
422-
# XXX should be renamed to "console"
423-
self.rpcclt.register("stdin", self.tkconsole)
420+
self.rpcclt.register("console", self.tkconsole)
421+
self.rpcclt.register("stdin", self.tkconsole.stdin)
424422
self.rpcclt.register("stdout", self.tkconsole.stdout)
425423
self.rpcclt.register("stderr", self.tkconsole.stderr)
426424
self.rpcclt.register("flist", self.tkconsole.flist)
@@ -860,10 +858,10 @@ def __init__(self, flist=None):
860858
self.save_stderr = sys.stderr
861859
self.save_stdin = sys.stdin
862860
from idlelib import IOBinding
863-
self.stdin = PseudoInputFile(self)
864-
self.stdout = PseudoFile(self, "stdout", IOBinding.encoding)
865-
self.stderr = PseudoFile(self, "stderr", IOBinding.encoding)
866-
self.console = PseudoFile(self, "console", IOBinding.encoding)
861+
self.stdin = PseudoInputFile(self, "stdin", IOBinding.encoding)
862+
self.stdout = PseudoOutputFile(self, "stdout", IOBinding.encoding)
863+
self.stderr = PseudoOutputFile(self, "stderr", IOBinding.encoding)
864+
self.console = PseudoOutputFile(self, "console", IOBinding.encoding)
867865
if not use_subprocess:
868866
sys.stdout = self.stdout
869867
sys.stderr = self.stderr
@@ -1259,36 +1257,82 @@ def rmenu_check_paste(self):
12591257
return 'disabled'
12601258
return super().rmenu_check_paste()
12611259

1262-
class PseudoFile(object):
1260+
class PseudoFile(io.TextIOBase):
12631261

12641262
def __init__(self, shell, tags, encoding=None):
12651263
self.shell = shell
12661264
self.tags = tags
1267-
self.encoding = encoding
1265+
self._encoding = encoding
1266+
1267+
@property
1268+
def encoding(self):
1269+
return self._encoding
1270+
1271+
@property
1272+
def name(self):
1273+
return '<%s>' % self.tags
1274+
1275+
def isatty(self):
1276+
return True
1277+
1278+
1279+
class PseudoOutputFile(PseudoFile):
1280+
1281+
def writable(self):
1282+
return True
12681283

12691284
def write(self, s):
1285+
if self.closed:
1286+
raise ValueError("write to closed file")
12701287
if not isinstance(s, str):
12711288
raise TypeError('must be str, not ' + type(s).__name__)
12721289
return self.shell.write(s, self.tags)
12731290

1274-
def writelines(self, lines):
1275-
for line in lines:
1276-
self.write(line)
12771291

1278-
def flush(self):
1279-
pass
1292+
class PseudoInputFile(PseudoFile):
12801293

1281-
def isatty(self):
1282-
return True
1294+
def __init__(self, shell, tags, encoding=None):
1295+
PseudoFile.__init__(self, shell, tags, encoding)
1296+
self._line_buffer = ''
12831297

1284-
class PseudoInputFile(object):
1285-
def __init__(self, shell):
1286-
self.readline = shell.readline
1287-
self.isatty = shell.isatty
1298+
def readable(self):
1299+
return True
12881300

1289-
def write(self, s):
1290-
raise io.UnsupportedOperation("not writable")
1291-
writelines = write
1301+
def read(self, size=-1):
1302+
if self.closed:
1303+
raise ValueError("read from closed file")
1304+
if size is None:
1305+
size = -1
1306+
elif not isinstance(size, int):
1307+
raise TypeError('must be int, not ' + type(size).__name__)
1308+
result = self._line_buffer
1309+
self._line_buffer = ''
1310+
if size < 0:
1311+
while True:
1312+
line = self.shell.readline()
1313+
if not line: break
1314+
result += line
1315+
else:
1316+
while len(result) < size:
1317+
line = self.shell.readline()
1318+
if not line: break
1319+
result += line
1320+
self._line_buffer = result[size:]
1321+
result = result[:size]
1322+
return result
1323+
1324+
def readline(self, size=-1):
1325+
if self.closed:
1326+
raise ValueError("read from closed file")
1327+
if size is None:
1328+
size = -1
1329+
elif not isinstance(size, int):
1330+
raise TypeError('must be int, not ' + type(size).__name__)
1331+
line = self._line_buffer or self.shell.readline()
1332+
if size < 0:
1333+
size = len(line)
1334+
self._line_buffer = line[size:]
1335+
return line[:size]
12921336

12931337

12941338
usage_msg = """\

Lib/idlelib/run.py

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from idlelib import RemoteObjectBrowser
1616
from idlelib import StackViewer
1717
from idlelib import rpc
18+
from idlelib import PyShell
19+
from idlelib import IOBinding
1820

1921
import __main__
2022

@@ -262,62 +264,23 @@ def handle_error(self, request, client_address):
262264
quitting = True
263265
thread.interrupt_main()
264266

265-
class _RPCFile(io.TextIOBase):
266-
"""Wrapper class for the RPC proxy to typecheck arguments
267-
that may not support pickling. The base class is there only
268-
to support type tests; all implementations come from the remote
269-
object."""
270-
271-
def __init__(self, rpc):
272-
super.__setattr__(self, 'rpc', rpc)
273-
274-
def __getattribute__(self, name):
275-
# When accessing the 'rpc' attribute, or 'write', use ours
276-
if name in ('rpc', 'write', 'writelines'):
277-
return io.TextIOBase.__getattribute__(self, name)
278-
# Else only look into the remote object only
279-
return getattr(self.rpc, name)
280-
281-
def __setattr__(self, name, value):
282-
return setattr(self.rpc, name, value)
283-
284-
@staticmethod
285-
def _ensure_string(func):
286-
def f(self, s):
287-
if not isinstance(s, str):
288-
raise TypeError('must be str, not ' + type(s).__name__)
289-
return func(self, s)
290-
return f
291-
292-
class _RPCOutputFile(_RPCFile):
293-
@_RPCFile._ensure_string
294-
def write(self, s):
295-
if not isinstance(s, str):
296-
raise TypeError('must be str, not ' + type(s).__name__)
297-
return self.rpc.write(s)
298-
299-
class _RPCInputFile(_RPCFile):
300-
@_RPCFile._ensure_string
301-
def write(self, s):
302-
raise io.UnsupportedOperation("not writable")
303-
writelines = write
304-
305267
class MyHandler(rpc.RPCHandler):
306268

307269
def handle(self):
308270
"""Override base method"""
309271
executive = Executive(self)
310272
self.register("exec", executive)
311-
self.console = self.get_remote_proxy("stdin")
312-
sys.stdin = _RPCInputFile(self.console)
313-
sys.stdout = _RPCOutputFile(self.get_remote_proxy("stdout"))
314-
sys.stderr = _RPCOutputFile(self.get_remote_proxy("stderr"))
273+
self.console = self.get_remote_proxy("console")
274+
sys.stdin = PyShell.PseudoInputFile(self.console, "stdin",
275+
IOBinding.encoding)
276+
sys.stdout = PyShell.PseudoOutputFile(self.console, "stdout",
277+
IOBinding.encoding)
278+
sys.stderr = PyShell.PseudoOutputFile(self.console, "stderr",
279+
IOBinding.encoding)
280+
315281
# page help() text to shell.
316282
import pydoc # import must be done here to capture i/o binding
317283
pydoc.pager = pydoc.plainpager
318-
from idlelib import IOBinding
319-
sys.stdin.encoding = sys.stdout.encoding = \
320-
sys.stderr.encoding = IOBinding.encoding
321284
self.interp = self.get_remote_proxy("interp")
322285
rpc.RPCHandler.getresponse(self, myseq=None, wait=0.05)
323286

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ Core and Builtins
202202
Library
203203
-------
204204

205+
- Issue #9290: In IDLE the sys.std* streams now implement io.TextIOBase
206+
interface and support all mandatory methods and properties.
207+
205208
- Issue #13454: Fix a crash when deleting an iterator created by itertools.tee()
206209
if all other iterators were very advanced before.
207210

0 commit comments

Comments
 (0)