Commit 5c28e9f8 authored by Terry Jan Reedy's avatar Terry Jan Reedy

Issue #23672: Allow Idle to edit and run files with astral chars in name.

Patch by Mohd Sanad Zaki Rizvi.
parent 8c125eb4
...@@ -344,19 +344,19 @@ class EditorWindow(object): ...@@ -344,19 +344,19 @@ class EditorWindow(object):
def _filename_to_unicode(self, filename): def _filename_to_unicode(self, filename):
"""convert filename to unicode in order to display it in Tk""" """Return filename as BMP unicode so diplayable in Tk."""
if isinstance(filename, str) or not filename: # Decode bytes to unicode.
return filename if isinstance(filename, bytes):
else:
try: try:
return filename.decode(self.filesystemencoding) filename = filename.decode(self.filesystemencoding)
except UnicodeDecodeError: except UnicodeDecodeError:
# XXX
try: try:
return filename.decode(self.encoding) filename = filename.decode(self.encoding)
except UnicodeDecodeError: except UnicodeDecodeError:
# byte-to-byte conversion # byte-to-byte conversion
return filename.decode('iso8859-1') filename = filename.decode('iso8859-1')
# Replace non-BMP char with diamond questionmark.
return re.sub('[\U00010000-\U0010FFFF]', '\ufffd', filename)
def new_callback(self, event): def new_callback(self, event):
dirname, basename = self.io.defaultfilename() dirname, basename = self.io.defaultfilename()
......
...@@ -36,6 +36,7 @@ To fix case 2, change all tabs to spaces by using Edit->Select All followed \ ...@@ -36,6 +36,7 @@ To fix case 2, change all tabs to spaces by using Edit->Select All followed \
by Format->Untabify Region and specify the number of columns used by each tab. by Format->Untabify Region and specify the number of columns used by each tab.
""" """
class ScriptBinding: class ScriptBinding:
menudefs = [ menudefs = [
...@@ -142,7 +143,8 @@ class ScriptBinding: ...@@ -142,7 +143,8 @@ class ScriptBinding:
return 'break' return 'break'
interp = self.shell.interp interp = self.shell.interp
if PyShell.use_subprocess: if PyShell.use_subprocess:
interp.restart_subprocess(with_cwd=False, filename=code.co_filename) interp.restart_subprocess(with_cwd=False, filename=
self.editwin._filename_to_unicode(filename))
dirname = os.path.dirname(filename) dirname = os.path.dirname(filename)
# XXX Too often this discards arguments the user just set... # XXX Too often this discards arguments the user just set...
interp.runcommand("""if 1: interp.runcommand("""if 1:
......
import unittest
from tkinter import Tk, Text
from idlelib.EditorWindow import EditorWindow
from test.support import requires
class Editor_func_test(unittest.TestCase):
def test_filename_to_unicode(self):
func = EditorWindow._filename_to_unicode
class dummy(): filesystemencoding = 'utf-8'
pairs = (('abc', 'abc'), ('a\U00011111c', 'a\ufffdc'),
(b'abc', 'abc'), (b'a\xf0\x91\x84\x91c', 'a\ufffdc'))
for inp, out in pairs:
self.assertEqual(func(dummy, inp), out)
if __name__ == '__main__':
unittest.main(verbosity=2)
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