]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Only call sq_repeat if the object does not have a nb_multiply slot. One
authorNeil Schemenauer <nascheme@enme.ucalgary.ca>
Fri, 9 Aug 2002 15:46:50 +0000 (15:46 +0000)
committerNeil Schemenauer <nascheme@enme.ucalgary.ca>
Fri, 9 Aug 2002 15:46:50 +0000 (15:46 +0000)
example of where this changes behavior is when a new-style instance
defines '__mul__' and '__rmul__' and is multiplied by an int.  Before
the change the '__rmul__' method is never called, even if the int is the
left operand.

Objects/intobject.c

index 8d4aa2cbb0231a352943e044530d2393910e40b1..2be563180fd6c84d070faa5de2b01332b0dbb83e 100644 (file)
@@ -344,6 +344,12 @@ one that can lose catastrophic amounts of information, it's the native long
 product that must have overflowed.
 */
 
+/* Return true if the sq_repeat method should be used */
+#define USE_SQ_REPEAT(o) (!PyInt_Check(o) && \
+                         o->ob_type->tp_as_sequence && \
+                         o->ob_type->tp_as_sequence->sq_repeat && \
+                         (!o->ob_type->tp_as_number || \
+                          !o->ob_type->tp_as_number->nb_multiply))
 static PyObject *
 int_mul(PyObject *v, PyObject *w)
 {
@@ -352,16 +358,12 @@ int_mul(PyObject *v, PyObject *w)
        double doubled_longprod;        /* (double)longprod */
        double doubleprod;              /* (double)a * (double)b */
 
-       if (!PyInt_Check(v) &&
-           v->ob_type->tp_as_sequence &&
-           v->ob_type->tp_as_sequence->sq_repeat) {
+       if (USE_SQ_REPEAT(v)) {
                /* sequence * int */
                a = PyInt_AsLong(w);
                return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
        }
-       if (!PyInt_Check(w) &&
-           w->ob_type->tp_as_sequence &&
-           w->ob_type->tp_as_sequence->sq_repeat) {
+       if (USE_SQ_REPEAT(w)) {
                /* int * sequence */
                a = PyInt_AsLong(v);
                return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);