Inconsistent handling of the modulo argument for ternary pow() (pure-Python vs extension)
オープン
まだ誰も着手していません。
interpreter-core
type-bug
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Objects/typeobject.c の slot_nb_power から始め、CPython main ビルドで報告にある decimal/_pydecimal の例を再現します。Pure-Python 実装と拡張実装で ternary pow() の動作を比較します。完了条件は、既存の fallback 動作を後退させることなく、両方が modulo 引数を一貫して処理することです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100