[BUG]: Setting the `__name__` attribute changes the behaviour of error reporting

Abierto
#5,551 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
cpp, python

Línea de trabajo

Reproduce el comportamiento con el binding de example.cpp y la sesión interactiva de Python mostrados en el issue. Empieza leyendo include/pybind11/detail/class.h y sigue cómo se seleccionan los nombres de tipo para los errores de atributos; se considera terminado cuando los errores siguen incluyendo el módulo al sobrescribirse name, sin cambiar el comportamiento predeterminado de Person.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

triage
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 called example. If an attribute reference fails, the error message is AttributeError: 'example.Pet' object has no attribute .... However, if the __name__ attribute is specified using pet.attr("__name__") = "PseudoPet"; then the attribute errors look like AttributeError: 'PseudoPet' object has no attribute .... Notice that, in the latter, there is no module name example.

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 to PyUnicode_FromFormat() which may also be useful here?

Reproducible example code

In example.cpp:

#include <pybind11/pybind11.h>

namespace py = pybind11;

struct Pet {
  Pet(const std::string &name) : name(name) {}
  std::string name;
};

struct Person {
  Person(const std::string &name) : name(name) {}
  std::string name;
};

PYBIND11_MODULE(example, m) {
  // Class with explicitly set __name__
  py::class_<Pet> pet(m, "Pet");
  pet.def(py::init<const std::string &>());
  pet.attr("__name__") = "PseudoPet";

  // Class with default name
  py::class_<Person> person(m, "Person");
  person.def(py::init<const std::string &>());
}

In an interactive Python session:

>>> from example import Pet, Person

>>> my_pet = Pet("John")
>>> my_pet.age
AttributeError: 'PseudoPet' object has no attribute 'age'

>>> my_person = Person("Jane")
>>> my_person.age
AttributeError: 'example.Person' object has no attribute 'age'
Is this a regression? Put the last known working version here if it is.

Not a regression

Lenguaje dominante
C++
Estrellas
18k
Forks
2.3k
Merge medio
5 d 7 h
PR fusionados (30 d)
53

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de pybind/pybind11

Todos los issues de pybind/pybind11

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.