summaryrefslogtreecommitdiffstats
path: root/Lib/pprint.py
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-09-04 19:43:26 (GMT)
committerFred Drake <fdrake@acm.org>2001-09-04 19:43:26 (GMT)
commit1ef106c94d7ec70f6f4ca756fa47852404556b6d (patch)
tree6bb8f9443aff1919651830e53210922f892a0ca0 /Lib/pprint.py
parent4a596e3bee104e482353c099ed91a515b8cf21e3 (diff)
downloadcpython-1ef106c94d7ec70f6f4ca756fa47852404556b6d.zip
cpython-1ef106c94d7ec70f6f4ca756fa47852404556b6d.tar.gz
cpython-1ef106c94d7ec70f6f4ca756fa47852404556b6d.tar.bz2
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
This closes SF patch #451538.
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r--Lib/pprint.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 351323b..a91559f 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -34,7 +34,8 @@ saferepr()
"""
-from types import DictType, ListType, TupleType
+from types import DictType, ListType, TupleType, StringType
+import sys
try:
from cStringIO import StringIO
@@ -95,7 +96,6 @@ class PrettyPrinter:
if stream:
self.__stream = stream
else:
- import sys
self.__stream = sys.stdout
def pprint(self, object):
@@ -187,12 +187,30 @@ class PrettyPrinter:
# 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