[BUG]: Objects created using __new__ are not registered for casting
Maintainer thường phản hồi trong vòng 3 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Bắt đầu bằng việc xây dựng ví dụ C++ pybind11 có thể tái lập và chạy các lời gọi Python được hiển thị liên quan đến Num, MoreNum, new, mult2 và mult4. Theo dõi cách các đối tượng py::class_ được tạo thông qua new đi vào registry đối tượng của pybind11; hoàn tất khi các đối tượng này được đăng ký và các lời gọi nối tiếp bảo toàn hành vi của kiểu con thực tế.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
3.0.1
Problem description
When a pybind-based object or a subclass of one is created using new, it is not added to the registry of pybind objects for casting. If the object has methods than require casting to return the correct type information for its actual subtype, then this silently breaks those methods.
This is somewhat related to issue #1693, as using the listed workaround is how I found this problem.
Reproducible example code
Suppose we have:
#include <pybind11/pybind11.h>
using namespace py = pybind11;
struct Num {
double n;
Num(double n) : n(n) {}
inline Num Mult2() { return Num(2 * n) }
}
PYBIND11_MODULE(this_example, m) {
py::class_<Num> pyNum(m, "Num");
pyNum
.def(py::init([]() { return Num(0); }))
.def("mult2", [](const Num *self) {
py::object selfObj = py::cast(self);
PyTypeObject *const ty = Py_TYPE(selfObj.ptr());
auto newObj = py::reinterpret_steal<py::object>(ty->tp_new(ty, py::list().ptr(), py::dict().ptr()));
*newObj.cast<Num*>() = self->Mult2();
return newObj;
})
.def_readwrite("n", &Num::n);
}
from this_example import Num
class MoreNum(Num):
def mult4(self):
ret = self.__class__.__new__(self.__class__)
ret.n = self.n * self.n
return ret
# Works, but returned object is not registered.
MoreNum().mult2()
MoreNum().mult4()
MoreNum().mult2().mult4()
# mult4 is not implemented on Num
MoreNum().mult2().mult2().mult4()
Is this a regression? Put the last known working version here if it is.
Not a regression
- Ngôn ngữ chính
- C++
- Star
- 18k
- Fork
- 2.3k
- Merge trung bình
- 5 ngày 7 giờ
- Pull request đã merge (30 ngày)
- 53
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc 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.
Issue khác của pybind/pybind11
-
triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 3 ngày
-
triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
pybind/pybind11#5604 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 3 ngày
-
docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
pybind/pybind11#747 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
array_t::at() returns a misaligned reference when the stride is not a multiple of alignof(T)Có thể đã có người làm @henryiii đã nhận hôm nay. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của pybind/pybind11
Issue tương tự
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
raspberrypi/rpi-imager#1804 ·
-
difficulty: easy docs good first issue help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Ryan-Millard/Img2Num#842 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Unit converter reads out of bounds when the saved UnitConverterPreferences setting is malformedĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
microsoft/calculator#2532 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
-
bug difficulty:easy prio:normal
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
NatronGitHub/openfx-misc#411 ·
Maintainer thường phản hồi trong vòng 1 ngày