python / python/cpython

Inconsistent handling of the modulo argument for ternary pow() (pure-Python vs extension)

Aperta
#151,118 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

This is a follow-up of the https://github.com/python/cpython/issues/130104.

An example with the stdlib:

>>> import decimal, _pydecimal
>>> pow(2, 3, decimal.Decimal(4))
Decimal('0')
>>> pow(2, 3, _pydecimal.Decimal(4))
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    pow(2, 3, _pydecimal.Decimal(4))
    ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for ** or pow(): 'int', 'int', 'Decimal'

IIUIC, pow() it just calls the __pow__() for the third argument with original order of ops as a fallback. So, we can just do something like this:

diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 14bc5a4bc4..911c890fcb 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -10377,7 +10377,27 @@ slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
         stack[2] = modulus;
         return vectorcall_maybe(tstate, &_Py_ID(__rpow__), stack, 3);
     }
-    Py_RETURN_NOTIMPLEMENTED;
+    stack[0] = self;
+    stack[1] = other;
+    stack[2] = modulus;
+
+    _PyCStackRef cref;
+    _PyThreadState_PushCStackRef(tstate, &cref);
+    int unbound = lookup_maybe_method(modulus, &_Py_ID(__pow__), &cref.ref);
+    PyObject *func = PyStackRef_AsPyObjectBorrow(cref.ref);
+
+    if (func == NULL) {
+        _PyThreadState_PopCStackRef(tstate, &cref);
+        if (!PyErr_Occurred()) {
+            Py_RETURN_NOTIMPLEMENTED;
+        }
+        return NULL;
+    }
+
+    PyObject *retval = vectorcall_unbound(tstate, unbound, func, stack, 3);
+
+    _PyThreadState_PopCStackRef(tstate, &cref);
+    return retval;
 }
 
 SLOT0(slot_nb_negative, __neg__)

Full patch: https://github.com/skirpichev/cpython/pull/9

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in Objects/typeobject.c, nello slot slot_nb_power, e riproduci gli esempi di decimal/_pydecimal riportati nel report su una build CPython main. Confronta il comportamento di ternary pow() tra le implementazioni Pure-Python e di estensione; il lavoro è completato quando entrambe gestiscono l'argomento modulo in modo coerente senza regredire nel comportamento di fallback esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.