Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
import pickle
import sys
from decimal import Decimal
from fractions import Fraction

from test import support
from test.support import import_helper
Expand Down Expand Up @@ -508,6 +510,44 @@ def __getitem__(self, other): return 5 # so that C is a sequence
self.assertEqual(operator.ixor (c, 5), "ixor")
self.assertEqual(operator.iconcat (c, c), "iadd")

def test_iconcat_without_getitem(self):
operator = self.module

msg = "'int' object can't be concatenated"
with self.assertRaisesRegex(TypeError, msg):
operator.iconcat(1, 0.5)

def test_index(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, it seems like this one has some additional behavior in the C version that isn't reflected in the Python version.

  1. Returning a subclass of int raises a DeprecationWarning:
>>> class X:
...     def __index__(self): return True
... 
>>> operator.index(X())
<stdin>-3:1: DeprecationWarning: __index__ returned non-int (type bool).  The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.
  operator.index(X())
1

But the Python version returns True.

  1. If a subclass of int overrides __index__, the C version ignores it:
>>> class X(int):
...     def __index__(self): return 42
... 
>>> index(X())
42
>>> operator.index(X())
0

Arguably these are bugs, but I am not sure it's worth complicating the Python implementation for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a known bug, and there is an open issue for this.

operator = self.module
class X:
def __index__(self):
return 1

self.assertEqual(operator.index(X()), 1)
self.assertEqual(operator.index(0), 0)
self.assertEqual(operator.index(1), 1)
self.assertEqual(operator.index(2), 2)
with self.assertRaises((AttributeError, TypeError)):
operator.index(1.5)
with self.assertRaises((AttributeError, TypeError)):
operator.index(Fraction(3, 7))
with self.assertRaises((AttributeError, TypeError)):
operator.index(Decimal(1))
with self.assertRaises((AttributeError, TypeError)):
operator.index(None)

def test_not_(self):
operator = self.module
class C:
def __bool__(self):
raise SyntaxError
self.assertRaises(TypeError, operator.not_)
self.assertRaises(SyntaxError, operator.not_, C())
self.assertFalse(operator.not_(5))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I prefer to write this like

self.assertIs(operator.not_(5), False)

to ensure that the result is boolean.

self.assertFalse(operator.not_([0]))
self.assertTrue(operator.not_(0))
self.assertTrue(operator.not_([]))

def test_length_hint(self):
operator = self.module
class X(object):
Expand All @@ -533,6 +573,13 @@ def __length_hint__(self):
with self.assertRaises(LookupError):
operator.length_hint(X(LookupError))

class Y: pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the Python and C versions will behave differently if __length_hint__ returns an instance of a subclass of int.

Python:

>>> class X:
...     def __length_hint__(self): return True
... 
>>> operator.length_hint(X())
True

C:

>>> class X:
...     def __length_hint__(self): return True
... 
>>> operator.length_hint(X())
1

Arguably a bug but again I don't know if it's worth complicating the code for.


msg = "'str' object cannot be interpreted as an integer"
with self.assertRaisesRegex(TypeError, msg):
operator.length_hint(X(2), "abc")
self.assertEqual(operator.length_hint(Y(), 10), 10)

def test_call(self):
operator = self.module

Expand Down