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

Aperta
#6,025 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
cpp, python

Direzione di ricerca

Iniziate costruendo l’esempio C++ riproducibile con pybind11 ed eseguendo le chiamate Python mostrate che coinvolgono Num, MoreNum, new, mult2 e mult4. Tracciate il modo in cui gli oggetti py::class_ creati tramite new entrano nel registro degli oggetti di pybind11; il lavoro è completato quando questi oggetti sono registrati e le chiamate concatenate preservano il comportamento effettivo del sottotipo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
C++
Stelle
18k
Fork
2.3k
Merge medio
5g 7h
PR unite (30g)
53

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pybind/pybind11

Tutte le issue di pybind/pybind11

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.