- PrettyPrinter.isreadable(), .isrecursive():
Pass the right number of args to .format(). (Caught by pychecker.) - Protect the global namespace more carefully. - Don't use the types module now that we don't need to.
This commit is contained in:
parent
5478219e11
commit
397b615056
|
@ -34,20 +34,15 @@ saferepr()
|
|||
|
||||
"""
|
||||
|
||||
from types import DictType, ListType, TupleType, StringType
|
||||
import sys
|
||||
import sys as _sys
|
||||
|
||||
try:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
from StringIO import StringIO
|
||||
from cStringIO import StringIO as _StringIO
|
||||
|
||||
__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
|
||||
"PrettyPrinter"]
|
||||
|
||||
# cache these for faster access:
|
||||
_commajoin = ", ".join
|
||||
_sys_modules = sys.modules
|
||||
_id = id
|
||||
_len = len
|
||||
_type = type
|
||||
|
@ -104,21 +99,21 @@ class PrettyPrinter:
|
|||
if stream is not None:
|
||||
self._stream = stream
|
||||
else:
|
||||
self._stream = sys.stdout
|
||||
self._stream = _sys.stdout
|
||||
|
||||
def pprint(self, object):
|
||||
self._stream.write(self.pformat(object) + "\n")
|
||||
|
||||
def pformat(self, object):
|
||||
sio = StringIO()
|
||||
sio = _StringIO()
|
||||
self._format(object, sio, 0, 0, {}, 0)
|
||||
return sio.getvalue()
|
||||
|
||||
def isrecursive(self, object):
|
||||
return self.format(object, {}, 0)[2]
|
||||
return self.format(object, {}, 0, 0)[2]
|
||||
|
||||
def isreadable(self, object):
|
||||
s, readable, recursive = self.format(object, {}, 0)
|
||||
s, readable, recursive = self.format(object, {}, 0, 0)
|
||||
return readable and not recursive
|
||||
|
||||
def _format(self, object, stream, indent, allowance, context, level):
|
||||
|
@ -135,7 +130,7 @@ class PrettyPrinter:
|
|||
write = stream.write
|
||||
|
||||
if sepLines:
|
||||
if typ is DictType:
|
||||
if typ is dict:
|
||||
write('{')
|
||||
if self._indent_per_level > 1:
|
||||
write((self._indent_per_level - 1) * ' ')
|
||||
|
@ -162,8 +157,8 @@ class PrettyPrinter:
|
|||
write('}')
|
||||
return
|
||||
|
||||
if typ is ListType or typ is TupleType:
|
||||
if typ is ListType:
|
||||
if typ is list or typ is tuple:
|
||||
if typ is list:
|
||||
write('[')
|
||||
endchar = ']'
|
||||
else:
|
||||
|
@ -184,7 +179,7 @@ class PrettyPrinter:
|
|||
allowance + 1, context, level)
|
||||
indent = indent - self._indent_per_level
|
||||
del context[objid]
|
||||
if typ is TupleType and length == 1:
|
||||
if typ is tuple and length == 1:
|
||||
write(',')
|
||||
write(endchar)
|
||||
return
|
||||
|
@ -212,8 +207,8 @@ class PrettyPrinter:
|
|||
|
||||
def _safe_repr(object, context, maxlevels, level):
|
||||
typ = _type(object)
|
||||
if typ is StringType:
|
||||
if 'locale' not in _sys_modules:
|
||||
if typ is str:
|
||||
if 'locale' not in _sys.modules:
|
||||
return `object`, True, False
|
||||
if "'" in object and '"' not in object:
|
||||
closure = '"'
|
||||
|
@ -222,7 +217,7 @@ def _safe_repr(object, context, maxlevels, level):
|
|||
closure = "'"
|
||||
quotes = {"'": "\\'"}
|
||||
qget = quotes.get
|
||||
sio = StringIO()
|
||||
sio = _StringIO()
|
||||
write = sio.write
|
||||
for char in object:
|
||||
if char.isalpha():
|
||||
|
@ -231,7 +226,7 @@ def _safe_repr(object, context, maxlevels, level):
|
|||
write(qget(char, `char`[1:-1]))
|
||||
return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
|
||||
|
||||
if typ is DictType:
|
||||
if typ is dict:
|
||||
if not object:
|
||||
return "{}", True, False
|
||||
objid = _id(object)
|
||||
|
@ -256,8 +251,8 @@ def _safe_repr(object, context, maxlevels, level):
|
|||
del context[objid]
|
||||
return "{%s}" % _commajoin(components), readable, recursive
|
||||
|
||||
if typ is ListType or typ is TupleType:
|
||||
if typ is ListType:
|
||||
if typ is list or typ is tuple:
|
||||
if typ is list:
|
||||
if not object:
|
||||
return "[]", True, False
|
||||
format = "[%s]"
|
||||
|
|
Loading…
Reference in New Issue