[Python-checkins] bpo-26353: IDLE adds an unneeded newline when saving a shell window (GH-17103)
Miss Islington (bot)
webhook-mailer at python.org
Tue Nov 12 06:13:41 EST 2019
https://github.com/python/cpython/commit/177b12682cad7edf9cdea91382acc4232c0167e6
commit: 177b12682cad7edf9cdea91382acc4232c0167e6
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-11-12T03:13:32-08:00
summary:
bpo-26353: IDLE adds an unneeded newline when saving a shell window (GH-17103)
(cherry picked from commit c8b53dc3d8f721ed8519aa5a35530a42fbfb9424)
Co-authored-by: Zackery Spytz <zspytz at gmail.com>
files:
A Misc/NEWS.d/next/IDLE/2019-11-09-23-55-59.bpo-26353.duYZiF.rst
M Lib/idlelib/NEWS.txt
M Lib/idlelib/idle_test/test_iomenu.py
M Lib/idlelib/iomenu.py
diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
index fb22fb5e2bce0..81bf746a5cba3 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -3,6 +3,8 @@ Released on 2019-12-16?
======================================
+bop-26353: Stop adding newline when saving an IDLE shell window.
+
bpo-38598: Do not try to compile IDLE shell or output windows.
diff --git a/Lib/idlelib/idle_test/test_iomenu.py b/Lib/idlelib/idle_test/test_iomenu.py
index 743a05b3c3134..99f4048796712 100644
--- a/Lib/idlelib/idle_test/test_iomenu.py
+++ b/Lib/idlelib/idle_test/test_iomenu.py
@@ -1,14 +1,13 @@
-"Test , coverage 16%."
+"Test , coverage 17%."
from idlelib import iomenu
import unittest
from test.support import requires
from tkinter import Tk
-
from idlelib.editor import EditorWindow
-class IOBindigTest(unittest.TestCase):
+class IOBindingTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
@@ -16,9 +15,11 @@ def setUpClass(cls):
cls.root = Tk()
cls.root.withdraw()
cls.editwin = EditorWindow(root=cls.root)
+ cls.io = iomenu.IOBinding(cls.editwin)
@classmethod
def tearDownClass(cls):
+ cls.io.close()
cls.editwin._close()
del cls.editwin
cls.root.update_idletasks()
@@ -28,9 +29,20 @@ def tearDownClass(cls):
del cls.root
def test_init(self):
- io = iomenu.IOBinding(self.editwin)
- self.assertIs(io.editwin, self.editwin)
- io.close
+ self.assertIs(self.io.editwin, self.editwin)
+
+ 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__':
diff --git a/Lib/idlelib/iomenu.py b/Lib/idlelib/iomenu.py
index b5533be79f996..4b2833b8ca56f 100644
--- a/Lib/idlelib/iomenu.py
+++ b/Lib/idlelib/iomenu.py
@@ -371,10 +371,7 @@ def save_a_copy(self, event):
return "break"
def writefile(self, filename):
- self.fixlastline()
- text = self.text.get("1.0", "end-1c")
- if self.eol_convention != "\n":
- text = text.replace("\n", self.eol_convention)
+ text = self.fixnewlines()
chars = self.encode(text)
try:
with open(filename, "wb") as f:
@@ -387,6 +384,16 @@ def writefile(self, filename):
parent=self.text)
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):
if isinstance(chars, bytes):
# This is either plain ASCII, or Tk was returning mixed-encoding
@@ -426,11 +433,6 @@ def encode(self, chars):
# declared encoding
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):
confirm = tkMessageBox.askokcancel(
title="Print",
diff --git a/Misc/NEWS.d/next/IDLE/2019-11-09-23-55-59.bpo-26353.duYZiF.rst b/Misc/NEWS.d/next/IDLE/2019-11-09-23-55-59.bpo-26353.duYZiF.rst
new file mode 100644
index 0000000000000..fd0a2a3682100
--- /dev/null
+++ b/Misc/NEWS.d/next/IDLE/2019-11-09-23-55-59.bpo-26353.duYZiF.rst
@@ -0,0 +1,2 @@
+Stop adding newline when saving an IDLE shell window.
+
More information about the Python-checkins
mailing list