diff Lib/pprint.py @ 19697:bc6bd35f5998 legacy-trunk

Make pprint more locale-friendly; patch contributed by Denis S. Otkidach. This closes SF patch #451538.
author Fred Drake <fdrake@acm.org>
date Tue, 04 Sep 2001 19:43:26 +0000
parents 26745053f840
children 425c6c558fac
line wrap: on
line diff
--- a/Lib/pprint.py	Tue Sep 04 19:34:32 2001 +0000
+++ b/Lib/pprint.py	Tue Sep 04 19:43:26 2001 +0000
@@ -34,7 +34,8 @@
 
 """
 
-from types import DictType, ListType, TupleType
+from types import DictType, ListType, TupleType, StringType
+import sys
 
 try:
     from cStringIO import StringIO
@@ -95,7 +96,6 @@
         if stream:
             self.__stream = stream
         else:
-            import sys
             self.__stream = sys.stdout
 
     def pprint(self, object):
@@ -187,12 +187,30 @@
 
 # Return triple (repr_string, isreadable, isrecursive).
 
+_have_module = sys.modules.has_key
+
 def _safe_repr(object, context, maxlevels=None, level=0):
     level += 1
     typ = type(object)
-    if not (typ in (DictType, ListType, TupleType) and object):
+    if not (typ in (DictType, ListType, TupleType, StringType) and object):
         rep = `object`
         return rep, (rep and (rep[0] != '<')), 0
+    elif typ is StringType:
+        if not _have_module('locale'):
+            return `object`, 1, 0
+        if "'" in object and '"' not in object:
+            closure = '"'
+            quotes = {'"': '\\"'}
+        else:
+            closure = "'"
+            quotes = {"'": "\\'"}
+        sio = StringIO()
+        for char in object:
+            if char.isalpha():
+                sio.write(char)
+            else:
+                sio.write(quotes.get(char, `char`[1:-1]))
+        return closure + sio.getvalue() + closure, 1, 0
 
     if context.has_key(id(object)):
         return `_Recursion(object)`, 0, 1