mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
This closes SF patch #451538.
This commit is contained in:
parent
4a596e3bee
commit
1ef106c94d
1 changed files with 21 additions and 3 deletions
|
@ -34,7 +34,8 @@ saferepr()
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from types import DictType, ListType, TupleType
|
from types import DictType, ListType, TupleType, StringType
|
||||||
|
import sys
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
@ -95,7 +96,6 @@ class PrettyPrinter:
|
||||||
if stream:
|
if stream:
|
||||||
self.__stream = stream
|
self.__stream = stream
|
||||||
else:
|
else:
|
||||||
import sys
|
|
||||||
self.__stream = sys.stdout
|
self.__stream = sys.stdout
|
||||||
|
|
||||||
def pprint(self, object):
|
def pprint(self, object):
|
||||||
|
@ -187,12 +187,30 @@ class PrettyPrinter:
|
||||||
|
|
||||||
# Return triple (repr_string, isreadable, isrecursive).
|
# Return triple (repr_string, isreadable, isrecursive).
|
||||||
|
|
||||||
|
_have_module = sys.modules.has_key
|
||||||
|
|
||||||
def _safe_repr(object, context, maxlevels=None, level=0):
|
def _safe_repr(object, context, maxlevels=None, level=0):
|
||||||
level += 1
|
level += 1
|
||||||
typ = type(object)
|
typ = type(object)
|
||||||
if not (typ in (DictType, ListType, TupleType) and object):
|
if not (typ in (DictType, ListType, TupleType, StringType) and object):
|
||||||
rep = `object`
|
rep = `object`
|
||||||
return rep, (rep and (rep[0] != '<')), 0
|
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)):
|
if context.has_key(id(object)):
|
||||||
return `_Recursion(object)`, 0, 1
|
return `_Recursion(object)`, 0, 1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue