Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9543b34

Browse files
committed
SF patch #670423: Add missing identity tests to operator.c
1 parent 18acea7 commit 9543b34

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

Doc/lib/liboperator.tex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ \section{\module{operator} ---
4848

4949

5050
The logical operations are also generally applicable to all objects,
51-
and support truth tests and Boolean operations:
51+
and support truth tests, identity tests, and Boolean operations:
5252

5353
\begin{funcdesc}{not_}{o}
5454
\funcline{__not__}{o}
@@ -64,6 +64,14 @@ \section{\module{operator} ---
6464
constructor.
6565
\end{funcdesc}
6666

67+
\begin{funcdesc}{is_}{a, b}
68+
Return \code{\var{a} is \var{b}}. Tests object identity.
69+
\end{funcdesc}
70+
71+
\begin{funcdesc}{is_not}{a, b}
72+
Return \code{\var{a} is not \var{b}}. Tests object identity.
73+
\end{funcdesc}
74+
6775

6876
The mathematical and bitwise operations are the most numerous:
6977

Lib/test/test_operator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ def test_truth(self):
215215
def test_bitwise_xor(self):
216216
self.failUnless(operator.xor(0xb, 0xc) == 0x7)
217217

218+
def test_is(self):
219+
a = b = 'xyzpdq'
220+
c = a[:3] + b[3:]
221+
self.failUnless(operator.is_(a, b))
222+
self.failIf(operator.is_(a,c))
223+
224+
def test_is_not(self):
225+
a = b = 'xyzpdq'
226+
c = a[:3] + b[3:]
227+
self.failIf(operator.is_not(a, b))
228+
self.failUnless(operator.is_not(a,c))
218229

219230
def test_main():
220231
test_support.run_unittest(OperatorTestCase)

Misc/NEWS

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,11 @@ Core and builtins
517517
Extension modules
518518
-----------------
519519

520+
- Added three operators to the operator module:
521+
operator.pow(a,b) which is equivalent to: a**b.
522+
operator.is_(a,b) which is equivalent to: a is b.
523+
operator.is_not(a,b) which is equivalent to: a is not b.
524+
520525
- posix.openpty now works on all systems that have /dev/ptmx.
521526

522527
- A module zipimport exists to support importing code from zip
@@ -733,8 +738,6 @@ Library
733738
or when you need to use sets as dict keys, and a class BaseSet which
734739
is the base class of the two.
735740

736-
- Added operator.pow(a,b) which is equivalent to a**b.
737-
738741
- Added random.sample(population,k) for random sampling without replacement.
739742
Returns a k length list of unique elements chosen from the population.
740743

Modules/operator.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ op_pow(PyObject *s, PyObject *a)
107107
return NULL;
108108
}
109109

110+
static PyObject*
111+
is_(PyObject *s, PyObject *a)
112+
{
113+
PyObject *a1, *a2, *result = NULL;
114+
if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
115+
result = (a1 == a2) ? Py_True : Py_False;
116+
Py_INCREF(result);
117+
}
118+
return result;
119+
}
120+
121+
static PyObject*
122+
is_not(PyObject *s, PyObject *a)
123+
{
124+
PyObject *a1, *a2, *result = NULL;
125+
if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
126+
result = (a1 != a2) ? Py_True : Py_False;
127+
Py_INCREF(result);
128+
}
129+
return result;
130+
}
131+
110132
static PyObject*
111133
op_getslice(PyObject *s, PyObject *a)
112134
{
@@ -182,6 +204,8 @@ spam1(countOf,
182204
spam1o(isMappingType,
183205
"isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
184206

207+
spam1(is_, "is_(a, b) -- Same as a is b.")
208+
spam1(is_not, "is_not(a, b) -- Same as a is not b.")
185209
spam2(add,__add__, "add(a, b) -- Same as a + b.")
186210
spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
187211
spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")

0 commit comments

Comments
 (0)