[BUG]: Objects created using __new__ are not registered for casting

Đang mở
#6,025 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Công nghệ
cpp, python
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ả

triage
Required prerequisites
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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pybind/pybind11

Tất cả issue của pybind/pybind11

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.