mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Only call sq_repeat if the object does not have a nb_multiply slot. One
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.
This commit is contained in:
parent
a350270302
commit
3bc3f28dbe
1 changed files with 8 additions and 6 deletions
|
@ -341,6 +341,12 @@ one that can lose catastrophic amounts of information, it's the native long
|
||||||
product that must have overflowed.
|
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 *
|
static PyObject *
|
||||||
int_mul(PyObject *v, PyObject *w)
|
int_mul(PyObject *v, PyObject *w)
|
||||||
{
|
{
|
||||||
|
@ -349,16 +355,12 @@ int_mul(PyObject *v, PyObject *w)
|
||||||
double doubled_longprod; /* (double)longprod */
|
double doubled_longprod; /* (double)longprod */
|
||||||
double doubleprod; /* (double)a * (double)b */
|
double doubleprod; /* (double)a * (double)b */
|
||||||
|
|
||||||
if (!PyInt_Check(v) &&
|
if (USE_SQ_REPEAT(v)) {
|
||||||
v->ob_type->tp_as_sequence &&
|
|
||||||
v->ob_type->tp_as_sequence->sq_repeat) {
|
|
||||||
/* sequence * int */
|
/* sequence * int */
|
||||||
a = PyInt_AsLong(w);
|
a = PyInt_AsLong(w);
|
||||||
return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
|
return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
|
||||||
}
|
}
|
||||||
if (!PyInt_Check(w) &&
|
if (USE_SQ_REPEAT(w)) {
|
||||||
w->ob_type->tp_as_sequence &&
|
|
||||||
w->ob_type->tp_as_sequence->sq_repeat) {
|
|
||||||
/* int * sequence */
|
/* int * sequence */
|
||||||
a = PyInt_AsLong(v);
|
a = PyInt_AsLong(v);
|
||||||
return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
|
return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue