[BUG]: Setting the __name__
attribute changes the behaviour of error reporting
#5551
Open
3 tasks done
Labels
triage
New bug, unverified
Required prerequisites
What version (or hash if on master) of pybind11 are you using?
2.13.6
Problem description
Suppose that a class called
Pet
is implemented in a module calledexample
. If an attribute reference fails, the error message isAttributeError: 'example.Pet' object has no attribute ...
. However, if the__name__
attribute is specified usingpet.attr("__name__") = "PseudoPet";
then the attribute errors look likeAttributeError: 'PseudoPet' object has no attribute ...
. Notice that, in the latter, there is no module nameexample
.I know that the
PyTypeObject.tp_name
member is different depending on whether the class/type was implemented in C or Python, and I think this gets used in include/pybind11/detail/class.h to determine various strings used for reporting. Perhaps this has something to do with the issue?PEP 737 introduces some functions like
PyType_GetModuleName()
, and adds formats toPyUnicode_FromFormat()
which may also be useful here?Reproducible example code
In example.cpp:
In an interactive Python session:
Is this a regression? Put the last known working version here if it is.
Not a regression
The text was updated successfully, but these errors were encountered: