python / python/cpython

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

Abierto
#151,118 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

interpreter-core type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en Objects/typeobject.c, en slot_nb_power, y reproduce los ejemplos de decimal/_pydecimal del informe en una compilación de CPython main. Compara el comportamiento de ternary pow() entre las implementaciones de Pure-Python y de la extensión; se considera terminado cuando ambas manejan el argumento de módulo de forma coherente sin hacer retroceder el comportamiento de fallback existente.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
c, python
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.