Inconsistent handling of the modulo argument for ternary pow() (pure-Python vs extension)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong Objects/typeobject.c tại slot_nb_power và tái hiện các ví dụ decimal/_pydecimal từ báo cáo trên một bản build CPython main. So sánh hành vi của ternary pow() giữa các triển khai Pure-Python và extension; được xem là hoàn tất khi cả hai xử lý đối số modulo một cách nhất quán mà không làm hồi quy hành vi fallback hiện có.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- c, python
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100