]> granicus.if.org Git - python/commitdiff
Convert characters from the locale's encoding on output.
authorMartin v. Löwis <martin@v.loewis.de>
Sat, 10 Aug 2002 12:22:12 +0000 (12:22 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sat, 10 Aug 2002 12:22:12 +0000 (12:22 +0000)
Reject characters outside the locale's encoding on input.

Tools/idle/OutputWindow.py
Tools/idle/PyShell.py

index f429f2e8bfb61550b0420a9b1867a8fc8aba43be..0e7fba231ab55cf3aa91aea2c8ab1a53ab10a78b 100644 (file)
@@ -2,6 +2,7 @@ from Tkinter import *
 from EditorWindow import EditorWindow
 import re
 import tkMessageBox
+import IOBinding
 
 class OutputWindow(EditorWindow):
 
@@ -34,6 +35,14 @@ class OutputWindow(EditorWindow):
     # Act as output file
 
     def write(self, s, tags=(), mark="insert"):
+        # Tk assumes that byte strings are Latin-1;
+        # we assume that they are in the locale's encoding
+        if isinstance(s, str):
+            try:
+                s = unicode(s, IOBinding.encoding)
+            except UnicodeError:
+                # some other encoding; let Tcl deal with it
+                pass
         self.text.insert(mark, s, tags)
         self.text.see(mark)
         self.text.update()
index 31a89402cf02880ee0e1acecbafffb362264348e..e71a9a17a5f61364b9d53ab3cbeac1f2076ea956 100644 (file)
@@ -191,7 +191,12 @@ class ModifiedInterpreter(InteractiveInterpreter):
         warnings.filterwarnings(action="error", category=SyntaxWarning)
         if isinstance(source, types.UnicodeType):
             import IOBinding
-            source = source.encode(IOBinding.encoding)
+            try:
+                source = source.encode(IOBinding.encoding)
+            except UnicodeError:
+                self.tkconsole.resetoutput()
+                self.write("Unsupported characters in input")
+                return
         try:
             return InteractiveInterpreter.runsource(self, source, filename)
         finally: