Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand',
'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul',
'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift',
'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le',
'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod',
'is_', 'is_none', 'is_not', 'is_not_none', 'isub', 'itemgetter', 'itruediv',
'ixor', 'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod',
'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift',
'setitem', 'sub', 'truediv', 'truth', 'xor']

Expand Down Expand Up @@ -66,6 +66,14 @@ def is_not(a, b):
"Same as a is not b."
return a is not b

def is_none(a):
"Same as a is None."
return a is None

def is_not_none(a):
"Same as a is not None."
return a is not None

# Mathematical/Bitwise Operations *********************************************#

def abs(a):
Expand Down Expand Up @@ -415,7 +423,7 @@ def ixor(a, b):
except ImportError:
pass
else:
from _operator import __doc__
from _operator import __doc__ # noqa: F401

# All of these "__func__ = func" assignments have to happen after importing
# from _operator to make sure they're set to the right function
Expand Down
49 changes: 40 additions & 9 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,26 @@ def test_is_not(self):
self.assertFalse(operator.is_not(a, b))
self.assertTrue(operator.is_not(a,c))

def test_is_none(self):
operator = self.module
a = 'xyzpdq'
b = ''
c = None
self.assertRaises(TypeError, operator.is_none)
self.assertFalse(operator.is_none(a))
self.assertFalse(operator.is_none(b))
self.assertTrue(operator.is_none(c))

def test_is_not_none(self):
operator = self.module
a = 'xyzpdq'
b = ''
c = None
self.assertRaises(TypeError, operator.is_not_none)
self.assertTrue(operator.is_not_none(a))
self.assertTrue(operator.is_not_none(b))
self.assertFalse(operator.is_not_none(c))

def test_attrgetter(self):
operator = self.module
class A:
Expand Down Expand Up @@ -462,6 +482,8 @@ def bar(self, f=42):
return f
def baz(*args, **kwds):
return kwds['name'], kwds['self']
def return_arguments(self, *args, **kwds):
return args, kwds
a = A()
f = operator.methodcaller('foo')
self.assertRaises(IndexError, f, a)
Expand All @@ -478,6 +500,17 @@ def baz(*args, **kwds):
f = operator.methodcaller('baz', name='spam', self='eggs')
self.assertEqual(f(a), ('spam', 'eggs'))

many_positional_arguments = tuple(range(10))
many_kw_arguments = dict(zip('abcdefghij', range(10)))
f = operator.methodcaller('return_arguments', *many_positional_arguments)
self.assertEqual(f(a), (many_positional_arguments, {}))

f = operator.methodcaller('return_arguments', **many_kw_arguments)
self.assertEqual(f(a), ((), many_kw_arguments))

f = operator.methodcaller('return_arguments', *many_positional_arguments, **many_kw_arguments)
self.assertEqual(f(a), (many_positional_arguments, many_kw_arguments))

def test_inplace(self):
operator = self.module
class C(object):
Expand Down Expand Up @@ -635,22 +668,20 @@ class PyOperatorTestCase(OperatorTestCase, unittest.TestCase):
class COperatorTestCase(OperatorTestCase, unittest.TestCase):
module = c_operator

# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_attrgetter_signature(self):
super().test_attrgetter_signature()
return super().test_attrgetter_signature()

# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_itemgetter_signature(self):
super().test_itemgetter_signature()
return super().test_itemgetter_signature()

# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_methodcaller_signature(self):
super().test_methodcaller_signature()
return super().test_methodcaller_signature()


@support.thread_unsafe("swaps global operator module")
class OperatorPickleTestCase:
def copy(self, obj, proto):
with support.swap_item(sys.modules, 'operator', self.module):
Expand Down
Loading