[Python-checkins] CVS: python/dist/src/Lib pprint.py,1.14,1.15

Fred L. Drake fdrake@users.sourceforge.net
Tue, 04 Sep 2001 12:43:28 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv6780

Modified Files:
	pprint.py 
Log Message:
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
This closes SF patch #451538.


Index: pprint.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pprint.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** pprint.py	2001/05/14 18:39:41	1.14
--- pprint.py	2001/09/04 19:43:26	1.15
***************
*** 35,39 ****
  """
  
! from types import DictType, ListType, TupleType
  
  try:
--- 35,40 ----
  """
  
! from types import DictType, ListType, TupleType, StringType
! import sys
  
  try:
***************
*** 96,100 ****
              self.__stream = stream
          else:
-             import sys
              self.__stream = sys.stdout
  
--- 97,100 ----
***************
*** 188,197 ****
  # Return triple (repr_string, isreadable, isrecursive).
  
  def _safe_repr(object, context, maxlevels=None, level=0):
      level += 1
      typ = type(object)
!     if not (typ in (DictType, ListType, TupleType) and object):
          rep = `object`
          return rep, (rep and (rep[0] != '<')), 0
  
      if context.has_key(id(object)):
--- 188,215 ----
  # 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, 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)):