summaryrefslogtreecommitdiff
blob: 38a98579409a503c687417a3e66501e567f62f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
diff --git a/src/gmpy2_mpz.c b/src/gmpy2_mpz.c
index e5087fa..66a297a 100644
--- a/src/gmpy2_mpz.c
+++ b/src/gmpy2_mpz.c
@@ -69,7 +69,7 @@ static PyNumberMethods GMPy_MPZ_number_methods =
     (binaryfunc) GMPy_MPZ_ISub_Slot,       /* nb_inplace_subtract     */
     (binaryfunc) GMPy_MPZ_IMul_Slot,       /* nb_inplace_multiply     */
     (binaryfunc) GMPy_MPZ_IRem_Slot,       /* nb_inplace_remainder    */
-    (ternaryfunc) GMPy_MPZ_IPow_Slot,      /* nb_inplace_power        */
+    0,      /* nb_inplace_power        */
     (binaryfunc) GMPy_MPZ_ILshift_Slot,    /* nb_inplace_lshift       */
     (binaryfunc) GMPy_MPZ_IRshift_Slot,    /* nb_inplace_rshift       */
         0,                                 /* nb_inplace_and          */
@@ -113,7 +113,7 @@ static PyNumberMethods GMPy_MPZ_number_methods =
     (binaryfunc) GMPy_MPZ_IMul_Slot,       /* nb_inplace_multiply     */
         0,                                 /* nb_inplace_divide       */
     (binaryfunc) GMPy_MPZ_IRem_Slot,       /* nb_inplace_remainder    */
-    (ternaryfunc) GMPy_MPZ_IPow_Slot,      /* nb_inplace_power        */
+    0,      /* nb_inplace_power        */
     (binaryfunc) GMPy_MPZ_ILshift_Slot,    /* nb_inplace_lshift       */
     (binaryfunc) GMPy_MPZ_IRshift_Slot,    /* nb_inplace_rshift       */
         0,                                 /* nb_inplace_and          */
@@ -229,4 +229,3 @@ static PyTypeObject MPZ_Type =
     GMPy_MPZ_NewInit,                       /* tp_new           */
         0,                                  /* tp_free          */
 };
-
diff --git a/src/gmpy2_pow.c b/src/gmpy2_pow.c
index ddcb43a..cf96470 100644
--- a/src/gmpy2_pow.c
+++ b/src/gmpy2_pow.c
@@ -98,8 +98,11 @@ GMPy_Integer_Pow(PyObject *b, PyObject *e, PyObject *m, CTXT_Object *context)
         unsigned long el;
 
         if (mpz_sgn(tempe->z) < 0) {
-            VALUE_ERROR("pow() exponent cannot be negative");
-            goto err;
+            Py_DECREF((PyObject*)result);
+            Py_DECREF((PyObject*)tempb);
+            Py_DECREF((PyObject*)tempe);
+
+            return GMPy_Real_Pow(b, e, m, context);
         }
 
         /* Catch -1, 0, 1 getting raised to large exponents. */
diff --git a/src/gmpy2_xmpz_inplace.c b/src/gmpy2_xmpz_inplace.c
index bbcd977..e5bbf09 100644
--- a/src/gmpy2_xmpz_inplace.c
+++ b/src/gmpy2_xmpz_inplace.c
@@ -271,14 +271,14 @@ GMPy_XMPZ_IPow_Slot(PyObject *self, PyObject *other, PyObject *mod)
     mp_bitcnt_t exp;
 
     exp = mp_bitcnt_t_From_Integer(other);
-    if (exp == (mp_bitcnt_t)(-1) && PyErr_Occurred()) {
-        PyErr_Clear();
-        Py_RETURN_NOTIMPLEMENTED;
-    }
+    if (exp == (mp_bitcnt_t)(-1) && PyErr_Occurred())
+        return NULL;
 
     mpz_pow_ui(MPZ(self), MPZ(self), exp);
     Py_INCREF((PyObject*)self);
     return (PyObject*)self;
+
+    Py_RETURN_NOTIMPLEMENTED;
 }
 
 /* Inplace xmpz and.
@@ -346,4 +346,3 @@ GMPy_XMPZ_IIor_Slot(PyObject *self, PyObject *other)
 
     Py_RETURN_NOTIMPLEMENTED;
 }
-
diff --git a/test/test_gmpy2_mpz_inplace.txt b/test/test_gmpy2_mpz_inplace.txt
index e7a8b96..147118c 100644
--- a/test/test_gmpy2_mpz_inplace.txt
+++ b/test/test_gmpy2_mpz_inplace.txt
@@ -147,18 +147,16 @@ Test ipow operator
 mpz(25)
 >>> x **= xmpz(2); x
 mpz(625)
->>> x **= -2
-Traceback (most recent call last):
-  File "<stdin>", line 1, in <module>
-TypeError: unsupported operand type(s) for ** or pow(): 'mpz' and 'int'
+>>> x **= -2; x
+mpfr('2.5600000000000001e-06')
+>>> x = mpz(625)
 >>> x **= 2; x
 mpz(390625)
->>> x **= mpfr(2)
-Traceback (most recent call last):
-  File "<stdin>", line 1, in <module>
-TypeError: unsupported operand type(s) for ** or pow(): 'mpz' and 'mpfr'
->>> 1
-1
+>>> x **= mpfr(2); x
+mpfr('152587890625.0')
+>>> x = mpz(390625)
+>>> x **= mpfr(-2); x
+mpfr('6.5535999999999999e-12')
 
 Test iand operator
 ------------------
diff --git a/test/test_gmpy2_pow.txt b/test/test_gmpy2_pow.txt
index 89bd876..d5b1f45 100644
--- a/test/test_gmpy2_pow.txt
+++ b/test/test_gmpy2_pow.txt
@@ -15,9 +15,7 @@ mpz(25)
 >>> ctx.pow(z1, z2)
 mpz(25)
 >>> z1 ** -z2
-Traceback (most recent call last):
-  File "<stdin>", line 1, in <module>
-ValueError: pow() exponent cannot be negative
+mpfr('0.040000000000000001')
 >>> z1 ** 0
 mpz(1)
 >>> mpz(0) ** 32
diff --git a/test/test_gmpy2_xmpz_inplace.txt b/test/test_gmpy2_xmpz_inplace.txt
index 94f86b7..c02f966 100644
--- a/test/test_gmpy2_xmpz_inplace.txt
+++ b/test/test_gmpy2_xmpz_inplace.txt
@@ -135,7 +135,7 @@ xmpz(625)
 >>> x **= -2
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
-TypeError: unsupported operand type(s) for ** or pow(): 'xmpz' and 'int'
+ValueError: a non-negative value is required
 >>> x **= 2; x
 xmpz(390625)
 >>> x **= mpfr(2)