mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
- 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
1 changed files with 16 additions and 21 deletions
|
@ -34,20 +34,15 @@ saferepr()
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from types import DictType, ListType, TupleType, StringType
|
import sys as _sys
|
||||||
import sys
|
|
||||||
|
|
||||||
try:
|
from cStringIO import StringIO as _StringIO
|
||||||
from cStringIO import StringIO
|
|
||||||
except ImportError:
|
|
||||||
from StringIO import StringIO
|
|
||||||
|
|
||||||
__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
|
__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
|
||||||
"PrettyPrinter"]
|
"PrettyPrinter"]
|
||||||
|
|
||||||
# cache these for faster access:
|
# cache these for faster access:
|
||||||
_commajoin = ", ".join
|
_commajoin = ", ".join
|
||||||
_sys_modules = sys.modules
|
|
||||||
_id = id
|
_id = id
|
||||||
_len = len
|
_len = len
|
||||||
_type = type
|
_type = type
|
||||||
|
@ -104,21 +99,21 @@ class PrettyPrinter:
|
||||||
if stream is not None:
|
if stream is not None:
|
||||||
self._stream = stream
|
self._stream = stream
|
||||||
else:
|
else:
|
||||||
self._stream = sys.stdout
|
self._stream = _sys.stdout
|
||||||
|
|
||||||
def pprint(self, object):
|
def pprint(self, object):
|
||||||
self._stream.write(self.pformat(object) + "\n")
|
self._stream.write(self.pformat(object) + "\n")
|
||||||
|
|
||||||
def pformat(self, object):
|
def pformat(self, object):
|
||||||
sio = StringIO()
|
sio = _StringIO()
|
||||||
self._format(object, sio, 0, 0, {}, 0)
|
self._format(object, sio, 0, 0, {}, 0)
|
||||||
return sio.getvalue()
|
return sio.getvalue()
|
||||||
|
|
||||||
def isrecursive(self, object):
|
def isrecursive(self, object):
|
||||||
return self.format(object, {}, 0)[2]
|
return self.format(object, {}, 0, 0)[2]
|
||||||
|
|
||||||
def isreadable(self, object):
|
def isreadable(self, object):
|
||||||
s, readable, recursive = self.format(object, {}, 0)
|
s, readable, recursive = self.format(object, {}, 0, 0)
|
||||||
return readable and not recursive
|
return readable and not recursive
|
||||||
|
|
||||||
def _format(self, object, stream, indent, allowance, context, level):
|
def _format(self, object, stream, indent, allowance, context, level):
|
||||||
|
@ -135,7 +130,7 @@ class PrettyPrinter:
|
||||||
write = stream.write
|
write = stream.write
|
||||||
|
|
||||||
if sepLines:
|
if sepLines:
|
||||||
if typ is DictType:
|
if typ is dict:
|
||||||
write('{')
|
write('{')
|
||||||
if self._indent_per_level > 1:
|
if self._indent_per_level > 1:
|
||||||
write((self._indent_per_level - 1) * ' ')
|
write((self._indent_per_level - 1) * ' ')
|
||||||
|
@ -162,8 +157,8 @@ class PrettyPrinter:
|
||||||
write('}')
|
write('}')
|
||||||
return
|
return
|
||||||
|
|
||||||
if typ is ListType or typ is TupleType:
|
if typ is list or typ is tuple:
|
||||||
if typ is ListType:
|
if typ is list:
|
||||||
write('[')
|
write('[')
|
||||||
endchar = ']'
|
endchar = ']'
|
||||||
else:
|
else:
|
||||||
|
@ -184,7 +179,7 @@ class PrettyPrinter:
|
||||||
allowance + 1, context, level)
|
allowance + 1, context, level)
|
||||||
indent = indent - self._indent_per_level
|
indent = indent - self._indent_per_level
|
||||||
del context[objid]
|
del context[objid]
|
||||||
if typ is TupleType and length == 1:
|
if typ is tuple and length == 1:
|
||||||
write(',')
|
write(',')
|
||||||
write(endchar)
|
write(endchar)
|
||||||
return
|
return
|
||||||
|
@ -212,8 +207,8 @@ class PrettyPrinter:
|
||||||
|
|
||||||
def _safe_repr(object, context, maxlevels, level):
|
def _safe_repr(object, context, maxlevels, level):
|
||||||
typ = _type(object)
|
typ = _type(object)
|
||||||
if typ is StringType:
|
if typ is str:
|
||||||
if 'locale' not in _sys_modules:
|
if 'locale' not in _sys.modules:
|
||||||
return `object`, True, False
|
return `object`, True, False
|
||||||
if "'" in object and '"' not in object:
|
if "'" in object and '"' not in object:
|
||||||
closure = '"'
|
closure = '"'
|
||||||
|
@ -222,7 +217,7 @@ def _safe_repr(object, context, maxlevels, level):
|
||||||
closure = "'"
|
closure = "'"
|
||||||
quotes = {"'": "\\'"}
|
quotes = {"'": "\\'"}
|
||||||
qget = quotes.get
|
qget = quotes.get
|
||||||
sio = StringIO()
|
sio = _StringIO()
|
||||||
write = sio.write
|
write = sio.write
|
||||||
for char in object:
|
for char in object:
|
||||||
if char.isalpha():
|
if char.isalpha():
|
||||||
|
@ -231,7 +226,7 @@ def _safe_repr(object, context, maxlevels, level):
|
||||||
write(qget(char, `char`[1:-1]))
|
write(qget(char, `char`[1:-1]))
|
||||||
return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
|
return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
|
||||||
|
|
||||||
if typ is DictType:
|
if typ is dict:
|
||||||
if not object:
|
if not object:
|
||||||
return "{}", True, False
|
return "{}", True, False
|
||||||
objid = _id(object)
|
objid = _id(object)
|
||||||
|
@ -256,8 +251,8 @@ def _safe_repr(object, context, maxlevels, level):
|
||||||
del context[objid]
|
del context[objid]
|
||||||
return "{%s}" % _commajoin(components), readable, recursive
|
return "{%s}" % _commajoin(components), readable, recursive
|
||||||
|
|
||||||
if typ is ListType or typ is TupleType:
|
if typ is list or typ is tuple:
|
||||||
if typ is ListType:
|
if typ is list:
|
||||||
if not object:
|
if not object:
|
||||||
return "[]", True, False
|
return "[]", True, False
|
||||||
format = "[%s]"
|
format = "[%s]"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue