Skip to content

bpo-26353: IDLE adds an unneeded newline when saving a shell window #17103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/idlelib/NEWS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
24 changes: 18 additions & 6 deletions Lib/idlelib/idle_test/test_iomenu.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
"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):
requires('gui')
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()
Expand All @@ -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__':
Expand Down
20 changes: 11 additions & 9 deletions Lib/idlelib/iomenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Stop adding newline when saving an IDLE shell window.