From 5959c559df777eb61b10d986e45ed4bbf269b36d Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 19 Aug 2002 03:19:09 +0000 Subject: Added __pow__(a,b) to the operator module. Completes the pattern of all operators having a counterpart in the operator module. Closes SF bug #577513. --- Doc/lib/liboperator.tex | 7 +++++++ Lib/test/test_operator.py | 6 ++++++ Modules/operator.c | 10 ++++++++++ 3 files changed, 23 insertions(+) diff --git a/Doc/lib/liboperator.tex b/Doc/lib/liboperator.tex index 7d34a0f..84450a4 100644 --- a/Doc/lib/liboperator.tex +++ b/Doc/lib/liboperator.tex @@ -131,6 +131,11 @@ Return the bitwise or of \var{a} and \var{b}. Return \var{o} positive. \end{funcdesc} +\begin{funcdesc}{pow}{a, b} +\funcline{__pow__}{a, b} +Return \var{a} \code{**} \var{b}, for \var{a} and \var{b} numbers. +\end{funcdesc} + \begin{funcdesc}{rshift}{a, b} \funcline{__rshift__}{a, b} Return \var{a} shifted right by \var{b}. @@ -310,6 +315,8 @@ symbols in the Python syntax and the functions in the {\code{invert(\var{a})}} \lineiii{Bitwise Or}{\code{\var{a} | \var{b}}} {\code{or_(\var{a}, \var{b})}} + \lineiii{Exponentiation}{\code{\var{a} ** \var{b}}} + {\code{pow(\var{a}, \var{b})}} \lineiii{Indexed Assignment}{\code{\var{o}[\var{k}] = \var{v}}} {\code{setitem(\var{o}, \var{k}, \var{v})}} \lineiii{Indexed Deletion}{\code{del \var{o}[\var{k}]}} diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index f1e2959..611e427 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -161,6 +161,12 @@ class OperatorTestCase(unittest.TestCase): self.failUnless(operator.pos(0) == 0) self.failUnless(operator.pos(-0) == 0) + def test_pow(self): + self.failUnless(operator.pow(3,5) == 3**5) + self.failUnless(operator.__pow__(3,5) == 3**5) + self.assertRaises(TypeError, operator.pow, 1) + self.assertRaises(TypeError, operator.pow, 1, 2, 3) + def test_repeat(self): a = range(3) self.failUnless(operator.repeat(a, 2) == a+a) diff --git a/Modules/operator.c b/Modules/operator.c index 229fbac..55c26df 100644 --- a/Modules/operator.c +++ b/Modules/operator.c @@ -102,6 +102,15 @@ spamrc(op_gt , Py_GT) spamrc(op_ge , Py_GE) static PyObject* +op_pow(PyObject *s, PyObject *a) +{ + PyObject *a1, *a2; + if (PyArg_ParseTuple(a,"OO:pow",&a1,&a2)) + return PyNumber_Power(a1, a2, Py_None); + return NULL; +} + +static PyObject* op_getslice(PyObject *s, PyObject *a) { PyObject *a1; @@ -199,6 +208,7 @@ spam2(setitem,__setitem__, "setitem(a, b, c) -- Same as a[b] = c.") spam2(delitem,__delitem__, "delitem(a, b) -- Same as del a[b].") +spam2(pow,__pow__, "pow(a, b) -- Same as a**b.") spam2(getslice,__getslice__, "getslice(a, b, c) -- Same as a[b:c].") spam2(setslice,__setslice__, -- cgit v0.12