Kaydet (Commit) 6cf0e13b authored tarafından Terry Jan Reedy's avatar Terry Jan Reedy

Issue #25507: Move 4 objects from pyshell to run and switch inports.

This removes one problem inport and reduces len(sys.modules) by 37.
üst ce7b27d1
...@@ -25,7 +25,6 @@ import sys ...@@ -25,7 +25,6 @@ import sys
import threading import threading
import time import time
import tokenize import tokenize
import io
import linecache import linecache
from code import InteractiveInterpreter from code import InteractiveInterpreter
...@@ -37,6 +36,7 @@ from idlelib.colorizer import ColorDelegator ...@@ -37,6 +36,7 @@ from idlelib.colorizer import ColorDelegator
from idlelib.undo import UndoDelegator from idlelib.undo import UndoDelegator
from idlelib.outwin import OutputWindow from idlelib.outwin import OutputWindow
from idlelib.config import idleConf from idlelib.config import idleConf
from idlelib.run import idle_formatwarning, PseudoInputFile, PseudoOutputFile
from idlelib import rpc from idlelib import rpc
from idlelib import debugger from idlelib import debugger
from idlelib import debugger_r from idlelib import debugger_r
...@@ -52,19 +52,6 @@ PORT = 0 # someday pass in host, port for remote debug capability ...@@ -52,19 +52,6 @@ PORT = 0 # someday pass in host, port for remote debug capability
warning_stream = sys.__stderr__ # None, at least on Windows, if no console. warning_stream = sys.__stderr__ # None, at least on Windows, if no console.
import warnings import warnings
def idle_formatwarning(message, category, filename, lineno, line=None):
"""Format warnings the IDLE way."""
s = "\nWarning (from warnings module):\n"
s += ' File \"%s\", line %s\n' % (filename, lineno)
if line is None:
line = linecache.getline(filename, lineno)
line = line.strip()
if line:
s += " %s\n" % line
s += "%s: %s\n" % (category.__name__, message)
return s
def idle_showwarning( def idle_showwarning(
message, category, filename, lineno, file=None, line=None): message, category, filename, lineno, file=None, line=None):
"""Show Idle-format warning (after replacing warnings.showwarning). """Show Idle-format warning (after replacing warnings.showwarning).
...@@ -1316,92 +1303,6 @@ class PyShell(OutputWindow): ...@@ -1316,92 +1303,6 @@ class PyShell(OutputWindow):
return 'disabled' return 'disabled'
return super().rmenu_check_paste() return super().rmenu_check_paste()
class PseudoFile(io.TextIOBase):
def __init__(self, shell, tags, encoding=None):
self.shell = shell
self.tags = tags
self._encoding = encoding
@property
def encoding(self):
return self._encoding
@property
def name(self):
return '<%s>' % self.tags
def isatty(self):
return True
class PseudoOutputFile(PseudoFile):
def writable(self):
return True
def write(self, s):
if self.closed:
raise ValueError("write to closed file")
if type(s) is not str:
if not isinstance(s, str):
raise TypeError('must be str, not ' + type(s).__name__)
# See issue #19481
s = str.__str__(s)
return self.shell.write(s, self.tags)
class PseudoInputFile(PseudoFile):
def __init__(self, shell, tags, encoding=None):
PseudoFile.__init__(self, shell, tags, encoding)
self._line_buffer = ''
def readable(self):
return True
def read(self, size=-1):
if self.closed:
raise ValueError("read from closed file")
if size is None:
size = -1
elif not isinstance(size, int):
raise TypeError('must be int, not ' + type(size).__name__)
result = self._line_buffer
self._line_buffer = ''
if size < 0:
while True:
line = self.shell.readline()
if not line: break
result += line
else:
while len(result) < size:
line = self.shell.readline()
if not line: break
result += line
self._line_buffer = result[size:]
result = result[:size]
return result
def readline(self, size=-1):
if self.closed:
raise ValueError("read from closed file")
if size is None:
size = -1
elif not isinstance(size, int):
raise TypeError('must be int, not ' + type(size).__name__)
line = self._line_buffer or self.shell.readline()
if size < 0:
size = len(line)
eol = line.find('\n', 0, size)
if eol >= 0:
size = eol + 1
self._line_buffer = line[size:]
return line[:size]
def close(self):
self.shell.close()
def fix_x11_paste(root): def fix_x11_paste(root):
"Make paste replace selection on x11. See issue #5124." "Make paste replace selection on x11. See issue #5124."
......
import sys import io
import linecache import linecache
import time import queue
import traceback import sys
import _thread as thread import _thread as thread
import threading import threading
import queue import time
import traceback
import tkinter import tkinter
from idlelib import calltips from idlelib import calltips
...@@ -14,7 +15,6 @@ from idlelib import debugger_r ...@@ -14,7 +15,6 @@ from idlelib import debugger_r
from idlelib import debugobj_r from idlelib import debugobj_r
from idlelib import stackviewer from idlelib import stackviewer
from idlelib import rpc from idlelib import rpc
from idlelib import pyshell
from idlelib import iomenu from idlelib import iomenu
import __main__ import __main__
...@@ -23,6 +23,19 @@ LOCALHOST = '127.0.0.1' ...@@ -23,6 +23,19 @@ LOCALHOST = '127.0.0.1'
import warnings import warnings
def idle_formatwarning(message, category, filename, lineno, line=None):
"""Format warnings the IDLE way."""
s = "\nWarning (from warnings module):\n"
s += ' File \"%s\", line %s\n' % (filename, lineno)
if line is None:
line = linecache.getline(filename, lineno)
line = line.strip()
if line:
s += " %s\n" % line
s += "%s: %s\n" % (category.__name__, message)
return s
def idle_showwarning_subproc( def idle_showwarning_subproc(
message, category, filename, lineno, file=None, line=None): message, category, filename, lineno, file=None, line=None):
"""Show Idle-format warning after replacing warnings.showwarning. """Show Idle-format warning after replacing warnings.showwarning.
...@@ -32,7 +45,7 @@ def idle_showwarning_subproc( ...@@ -32,7 +45,7 @@ def idle_showwarning_subproc(
if file is None: if file is None:
file = sys.stderr file = sys.stderr
try: try:
file.write(pyshell.idle_formatwarning( file.write(idle_formatwarning(
message, category, filename, lineno, line)) message, category, filename, lineno, line))
except IOError: except IOError:
pass # the file (probably stderr) is invalid - this warning gets lost. pass # the file (probably stderr) is invalid - this warning gets lost.
...@@ -291,6 +304,96 @@ class MyRPCServer(rpc.RPCServer): ...@@ -291,6 +304,96 @@ class MyRPCServer(rpc.RPCServer):
quitting = True quitting = True
thread.interrupt_main() thread.interrupt_main()
# Pseudofiles for shell-remote communication (also used in pyshell)
class PseudoFile(io.TextIOBase):
def __init__(self, shell, tags, encoding=None):
self.shell = shell
self.tags = tags
self._encoding = encoding
@property
def encoding(self):
return self._encoding
@property
def name(self):
return '<%s>' % self.tags
def isatty(self):
return True
class PseudoOutputFile(PseudoFile):
def writable(self):
return True
def write(self, s):
if self.closed:
raise ValueError("write to closed file")
if type(s) is not str:
if not isinstance(s, str):
raise TypeError('must be str, not ' + type(s).__name__)
# See issue #19481
s = str.__str__(s)
return self.shell.write(s, self.tags)
class PseudoInputFile(PseudoFile):
def __init__(self, shell, tags, encoding=None):
PseudoFile.__init__(self, shell, tags, encoding)
self._line_buffer = ''
def readable(self):
return True
def read(self, size=-1):
if self.closed:
raise ValueError("read from closed file")
if size is None:
size = -1
elif not isinstance(size, int):
raise TypeError('must be int, not ' + type(size).__name__)
result = self._line_buffer
self._line_buffer = ''
if size < 0:
while True:
line = self.shell.readline()
if not line: break
result += line
else:
while len(result) < size:
line = self.shell.readline()
if not line: break
result += line
self._line_buffer = result[size:]
result = result[:size]
return result
def readline(self, size=-1):
if self.closed:
raise ValueError("read from closed file")
if size is None:
size = -1
elif not isinstance(size, int):
raise TypeError('must be int, not ' + type(size).__name__)
line = self._line_buffer or self.shell.readline()
if size < 0:
size = len(line)
eol = line.find('\n', 0, size)
if eol >= 0:
size = eol + 1
self._line_buffer = line[size:]
return line[:size]
def close(self):
self.shell.close()
class MyHandler(rpc.RPCHandler): class MyHandler(rpc.RPCHandler):
def handle(self): def handle(self):
...@@ -298,11 +401,11 @@ class MyHandler(rpc.RPCHandler): ...@@ -298,11 +401,11 @@ class MyHandler(rpc.RPCHandler):
executive = Executive(self) executive = Executive(self)
self.register("exec", executive) self.register("exec", executive)
self.console = self.get_remote_proxy("console") self.console = self.get_remote_proxy("console")
sys.stdin = pyshell.PseudoInputFile(self.console, "stdin", sys.stdin = PseudoInputFile(self.console, "stdin",
iomenu.encoding) iomenu.encoding)
sys.stdout = pyshell.PseudoOutputFile(self.console, "stdout", sys.stdout = PseudoOutputFile(self.console, "stdout",
iomenu.encoding) iomenu.encoding)
sys.stderr = pyshell.PseudoOutputFile(self.console, "stderr", sys.stderr = PseudoOutputFile(self.console, "stderr",
iomenu.encoding) iomenu.encoding)
sys.displayhook = rpc.displayhook sys.displayhook = rpc.displayhook
......
...@@ -6,7 +6,6 @@ import tkinter as tk ...@@ -6,7 +6,6 @@ import tkinter as tk
from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas
from idlelib.debugobj import ObjectTreeItem, make_objecttreeitem from idlelib.debugobj import ObjectTreeItem, make_objecttreeitem
from idlelib.pyshell import PyShellFileList
def StackBrowser(root, flist=None, tb=None, top=None): def StackBrowser(root, flist=None, tb=None, top=None):
if top is None: if top is None:
...@@ -121,6 +120,7 @@ class VariablesTreeItem(ObjectTreeItem): ...@@ -121,6 +120,7 @@ class VariablesTreeItem(ObjectTreeItem):
return sublist return sublist
def _stack_viewer(parent): # htest # def _stack_viewer(parent): # htest #
from idlelib.pyshell import PyShellFileList
top = tk.Toplevel(parent) top = tk.Toplevel(parent)
top.title("Test StackViewer") top.title("Test StackViewer")
x, y = map(int, parent.geometry().split('+')[1:]) x, y = map(int, parent.geometry().split('+')[1:])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment