Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple comparison masking exception #48079

Closed
freddrake opened this issue Sep 10, 2008 · 3 comments
Closed

Tuple comparison masking exception #48079

freddrake opened this issue Sep 10, 2008 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@freddrake
Copy link
Member

BPO 3829
Nosy @freddrake, @rhettinger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2009-04-24.23:48:49.521>
created_at = <Date 2008-09-10.20:32:35.303>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'Tuple comparison masking exception'
updated_at = <Date 2009-04-24.23:48:49.517>
user = 'https://github.com/freddrake'

bugs.python.org fields:

activity = <Date 2009-04-24.23:48:49.517>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2009-04-24.23:48:49.521>
closer = 'rhettinger'
components = ['Interpreter Core']
creation = <Date 2008-09-10.20:32:35.303>
creator = 'fdrake'
dependencies = []
files = []
hgrepos = []
issue_num = 3829
keywords = []
message_count = 3.0
messages = ['72981', '84438', '86440']
nosy_count = 4.0
nosy_names = ['fdrake', 'rhettinger', 'stutzbach', 'gary']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue3829'
versions = ['Python 2.6', 'Python 2.5', 'Python 2.4', 'Python 2.7']

@freddrake
Copy link
Member Author

There's a strange condition where cmp() of tuples of unorderable values
returns -1 even though using the unorderable values raises an exception.

If I have these two unorderable values, cmp() raises an expected exception:

  >>> s0 = frozenset(['testing 0'])
  >>> s1 = frozenset(['testing 1'])
  >>> cmp(s0, s1)
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  TypeError: cannot compare sets using cmp()

Comparing tuples of the values returns -1:

  >>> cmp((s0,), (s1,))
  -1

Py3k does raise a TypeError, but the message is indecipherable:

  >>> cmp((s0,), (s1,))
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: unorderable types: 'tuple' != 'tuple'

(The Py3k message for the set comparison is the same as for Python 2.)

I believe that this is an error; the exception from the underlying item
comparison should be propagated.

@freddrake freddrake added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 10, 2008
@stutzbach
Copy link
Mannequin

stutzbach mannequin commented Mar 29, 2009

I don't think the compare is actually masking an exception.

The set type defines a tp_richcompare routine that gets called when
comparing them as members of a tuple, but the set type also defines a
tp_compare routine that does nothing but raise an exception.

Another side effect is that sets are comparable using < etc., but not
with cmp():

>>> s0 = frozenset(['testing 0'])
>>> s1 = frozenset(['testing 1'])
>>> s0 < s1
False
>>> cmp(s0, s1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: cannot compare sets using cmp()

cmp() is gone in 3.0.1 so I've removed Python 3.0 from the versions.

I'm not sure why tp_compare and tp_richcompare work differently. Maybe
Raymond could shed some light?

@rhettinger
Copy link
Contributor

Daniel, you're basically on the money. No exception is getting masked.

The rich comparison operations for sets are defined as subset/superset
operations. So, those operations are not useful for total orderings
used by min/max/sorted/bisect/heapq/cmp and tuple comparisons. We can't
stop those tools from calling the rich comparisons but we can and do
raise a TypeError when cmp() is called directly on two sets.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants