[BUG]: Objects created using __new__ are not registered for casting
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
- Ambito
- backend-api-design
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
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
- Lingua principale
- C++
- Stelle
- 18k
- Fork
- 2.3k
- Merge medio
- 5g 7h
- PR unite (30g)
- 53
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pybind/pybind11
-
triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 3 giorni
-
triage
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 62/100
pybind/pybind11#5604 · 1 commento ·
I maintainer di solito rispondono entro 3 giorni
-
triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 3 giorni
-
docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 60/100
pybind/pybind11#747 · 2 commenti ·
I maintainer di solito rispondono entro 3 giorni
-
array_t::at() returns a misaligned reference when the stride is not a multiple of alignof(T)Forse già presa @henryiii l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 3 giorni
Tutte le issue di pybind/pybind11
Issue simili
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
raspberrypi/rpi-imager#1804 ·
-
difficulty: easy docs good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
Ryan-Millard/Img2Num#842 ·
I maintainer di solito rispondono entro 12 giorni
-
Unit converter reads out of bounds when the saved UnitConverterPreferences setting is malformedAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
microsoft/calculator#2532 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
-
bug difficulty:easy prio:normal
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
NatronGitHub/openfx-misc#411 ·
I maintainer di solito rispondono entro 1 giorno