Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


dictionary having multiple keys having frozenset fails to print on iPython #148

shahjapan opened this Issue · 2 comments

3 participants

In [59]: d1 = { frozenset([1,2,3]) : [1,2,3], 'key1' : 'value1' }

In [60]: d1
Out[60]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/.../ in ()

/usr/lib/pymodules/python2.6/IPython/Prompts.pyc in __call__(self, arg)
    551             # and now call a possibly user-defined print mechanism

--> 552             manipulated_val = self.display(arg)
    554             # user display hooks can change the variable to be stored in

/usr/lib/pymodules/python2.6/IPython/Prompts.pyc in _display(self, arg)
    576             return IPython.generics.result_display(arg)
    577         except TryNext:
--> 578             return
    580     # Assign the default display method:

/usr/lib/pymodules/python2.6/IPython/hooks.pyc in __call__(self, *args, **kw)
    139             #print "prio",prio,"cmd",cmd #dbg

    140             try:
--> 141                 ret = cmd(*args, **kw)
    142                 return ret
    143             except ipapi.TryNext, exc:

/usr/lib/pymodules/python2.6/IPython/hooks.pyc in result_display(self, arg)
    170     if self.rc.pprint:
--> 171         out = pformat(arg)
    172         if '\n' in out:
    173             # So that multi-line strings line up with the left column of

/usr/lib/python2.6/pprint.pyc in pformat(self, object)
    109     def pformat(self, object):
    110         sio = _StringIO()
--> 111         self._format(object, sio, 0, 0, {}, 0)
    112         return sio.getvalue()

/usr/lib/python2.6/pprint.pyc in _format(self, object, stream, indent, allowance, context, level)
    127             self._readable = False
    128             return
--> 129         rep = self._repr(object, context, level - 1)
    130         typ = _type(object)
    131         sepLines = _len(rep) > (self._width - 1 - indent - allowance)

/usr/lib/python2.6/pprint.pyc in _repr(self, object, context, level)
    221     def _repr(self, object, context, level):
    222         repr, readable, recursive = self.format(object, context.copy(),
--> 223                                                 self._depth, level)
    224         if not readable:
    225             self._readable = False

/usr/lib/python2.6/pprint.pyc in format(self, object, context, maxlevels, level)
    233         and whether the object represents a recursive construct.
    234         """
--> 235         return _safe_repr(object, context, maxlevels, level)

/usr/lib/python2.6/pprint.pyc in _safe_repr(object, context, maxlevels, level)
    275         level += 1
    276         saferepr = _safe_repr
--> 277         for k, v in sorted(object.items()):
    278             krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
    279             vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)

TypeError: can only compare to a set

This is a bug in the standard library's pprint module. Their safe_repr isn't as safe as it could be. I will note that the "pretty" extension distributed with IPython does handle this case correctly. Perhaps it should be promoted to the default pretty-printing for IPython.

Failing that, we should wrap pformat() in a try: except TypeError: block that falls back to a regular repr().


Work around bug in pprint.pformat. Closed by a55b74a

@jtriley jtriley referenced this issue from a commit in jtriley/ipython
@fperez fperez Work around bug in pprint.pformat. Closes gh-148 a55b74a
@fperez fperez referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@mattvonrocketstein mattvonrocketstein referenced this issue from a commit in mattvonrocketstein/ipython
@fperez fperez Work around bug in pprint.pformat. Closes gh-148 2403858
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.