This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pprint chokes on set containing frozenset
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: jbylund, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2014-01-08 17:37 by jbylund, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg207693 - (view) Author: Joseph Bylund (jbylund) Date: 2014-01-08 17:37
Expected: pprint the object
Observed: crash with:
set([Traceback (most recent call last):
  File "./test.py", line 7, in <module>
    pp.pprint(myset)
  File "/usr/lib/python2.7/pprint.py", line 117, in pprint
    self._format(object, self._stream, 0, 0, {}, 0)
  File "/usr/lib/python2.7/pprint.py", line 194, in _format
    object = _sorted(object)
  File "/usr/lib/python2.7/pprint.py", line 82, in _sorted
    return sorted(iterable)
TypeError: can only compare to a set


Steps to repro:
#!/usr/bin/python
import pprint
pp = pprint.PrettyPrinter(indent=4)

myset = set(xrange(3))
myset.add(frozenset(xrange(10)))
pp.pprint(myset)
msg207695 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-01-08 18:00
FYI, 'crash' is for when the CPython interpreter segfaults, not when python produces a traceback.

Sets and frozensets are not comparable to anything except themselves, unlike most other Python2 datatypes.  In Python3, most disparate types are not comparable, including sets and frozensets.

To fix this issue in 2.7's pprint I think we would have to backport the fix from issue 3976.
msg231981 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-02 09:15
For now sets and frozensets are comparable with other types in Python 2.

>>> frozenset(xrange(10)) < 1
False
>>> set(xrange(10)) < 1
False

The only known to me uncomparable types in Python 2 are naive and aware datetime.
msg240086 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-04-04 18:43
Here is reproducible on 2.7 example:

>>> import pprint, datetime, test.test_datetime
>>> naive = datetime.datetime.utcnow()
>>> aware = datetime.datetime.utcnow().replace(tzinfo=test.test_datetime.FixedOffset(-300, "EST", 1))
>>> pprint.pprint({naive, aware})
set([Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 59, in pprint
    printer.pprint(object)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 117, in pprint
    self._format(object, self._stream, 0, 0, {}, 0)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 199, in _format
    object = _sorted(object)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 82, in _sorted
    return sorted(iterable)
TypeError: can't compare offset-naive and offset-aware datetimes
>>> pprint.pprint({naive: 'naive', aware: 'aware'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 59, in pprint
    printer.pprint(object)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 117, in pprint
    self._format(object, self._stream, 0, 0, {}, 0)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 140, in _format
    rep = self._repr(object, context, level - 1)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 226, in _repr
    self._depth, level)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 238, in format
    return _safe_repr(object, context, maxlevels, level)
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 280, in _safe_repr
    for k, v in _sorted(object.items()):
  File "/home/serhiy/py/cpython2.7/Lib/pprint.py", line 82, in _sorted
    return sorted(iterable)
TypeError: can't compare offset-naive and offset-aware datetimes
msg370476 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-31 14:59
Python 2.7 is no longer supported.
History
Date User Action Args
2022-04-11 14:57:56adminsetgithub: 64391
2020-05-31 14:59:08serhiy.storchakasetstatus: open -> closed
resolution: out of date
messages: + msg370476

stage: resolved
2015-04-04 18:43:48serhiy.storchakasetmessages: + msg240086
2014-12-02 09:15:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg231981
2014-01-08 18:00:29r.david.murraysetnosy: + r.david.murray
messages: + msg207695

components: + Library (Lib)
type: crash -> behavior
2014-01-08 17:37:11jbylundcreate