bpo-26353: IDLE adds an unneeded newline when saving a shell window (GH-17103)

This commit is contained in:
Zackery Spytz 2019-11-12 03:54:10 -07:00 committed by Terry Jan Reedy
parent 733b9a308e
commit c8b53dc3d8
4 changed files with 33 additions and 15 deletions

View file

@ -3,6 +3,8 @@ Released on 2020-10-05?
====================================== ======================================
bop-26353: Stop adding newline when saving an IDLE shell window.
bpo-38598: Do not try to compile IDLE shell or output windows. bpo-38598: Do not try to compile IDLE shell or output windows.

View file

@ -1,14 +1,13 @@
"Test , coverage 16%." "Test , coverage 17%."
from idlelib import iomenu from idlelib import iomenu
import unittest import unittest
from test.support import requires from test.support import requires
from tkinter import Tk from tkinter import Tk
from idlelib.editor import EditorWindow from idlelib.editor import EditorWindow
class IOBindigTest(unittest.TestCase): class IOBindingTest(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@ -16,9 +15,11 @@ class IOBindigTest(unittest.TestCase):
cls.root = Tk() cls.root = Tk()
cls.root.withdraw() cls.root.withdraw()
cls.editwin = EditorWindow(root=cls.root) cls.editwin = EditorWindow(root=cls.root)
cls.io = iomenu.IOBinding(cls.editwin)
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
cls.io.close()
cls.editwin._close() cls.editwin._close()
del cls.editwin del cls.editwin
cls.root.update_idletasks() cls.root.update_idletasks()
@ -28,9 +29,20 @@ class IOBindigTest(unittest.TestCase):
del cls.root del cls.root
def test_init(self): def test_init(self):
io = iomenu.IOBinding(self.editwin) self.assertIs(self.io.editwin, self.editwin)
self.assertIs(io.editwin, self.editwin)
io.close def test_fixnewlines_end(self):
eq = self.assertEqual
io = self.io
fix = io.fixnewlines
text = io.editwin.text
self.editwin.interp = None
eq(fix(), '')
del self.editwin.interp
text.insert(1.0, 'a')
eq(fix(), 'a'+io.eol_convention)
eq(text.get('1.0', 'end-1c'), 'a\n')
eq(fix(), 'a'+io.eol_convention)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -371,10 +371,7 @@ class IOBinding:
return "break" return "break"
def writefile(self, filename): def writefile(self, filename):
self.fixlastline() text = self.fixnewlines()
text = self.text.get("1.0", "end-1c")
if self.eol_convention != "\n":
text = text.replace("\n", self.eol_convention)
chars = self.encode(text) chars = self.encode(text)
try: try:
with open(filename, "wb") as f: with open(filename, "wb") as f:
@ -387,6 +384,16 @@ class IOBinding:
parent=self.text) parent=self.text)
return False return False
def fixnewlines(self):
"Return text with final \n if needed and os eols."
if (self.text.get("end-2c") != '\n'
and not hasattr(self.editwin, "interp")): # Not shell.
self.text.insert("end-1c", "\n")
text = self.text.get("1.0", "end-1c")
if self.eol_convention != "\n":
text = text.replace("\n", self.eol_convention)
return text
def encode(self, chars): def encode(self, chars):
if isinstance(chars, bytes): if isinstance(chars, bytes):
# This is either plain ASCII, or Tk was returning mixed-encoding # This is either plain ASCII, or Tk was returning mixed-encoding
@ -426,11 +433,6 @@ class IOBinding:
# declared encoding # declared encoding
return BOM_UTF8 + chars.encode("utf-8") return BOM_UTF8 + chars.encode("utf-8")
def fixlastline(self):
c = self.text.get("end-2c")
if c != '\n':
self.text.insert("end-1c", "\n")
def print_window(self, event): def print_window(self, event):
confirm = tkMessageBox.askokcancel( confirm = tkMessageBox.askokcancel(
title="Print", title="Print",

View file

@ -0,0 +1,2 @@
Stop adding newline when saving an IDLE shell window.