@@ -24,6 +24,14 @@ PYBIND11_NAMESPACE_BEGIN(detail)
2424# define PYBIND11_SET_OLDPY_QUALNAME (obj, nameobj ) setattr((PyObject *) obj, " __qualname__" , nameobj)
2525#endif
2626
27+ inline std::string get_tp_name (PyTypeObject *type) {
28+ #if !defined(PYPY_VERSION)
29+ return type->tp_name ;
30+ #else
31+ return handle ((PyObject *) type).attr (" __module__" ).cast <std::string>() + " ." + type->tp_name ;
32+ #endif
33+ }
34+
2735inline PyTypeObject *type_incref (PyTypeObject *type) {
2836 Py_INCREF (type);
2937 return type;
@@ -172,7 +180,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
172180 for (const auto &vh : values_and_holders (instance)) {
173181 if (!vh.holder_constructed ()) {
174182 PyErr_Format (PyExc_TypeError, " %.200s.__init__() must be called when overriding __init__" ,
175- vh.type ->type -> tp_name );
183+ get_tp_name ( vh.type ->type ). c_str () );
176184 Py_DECREF (self);
177185 return nullptr ;
178186 }
@@ -304,12 +312,7 @@ extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *,
304312// / following default function will be used which simply throws an exception.
305313extern " C" inline int pybind11_object_init (PyObject *self, PyObject *, PyObject *) {
306314 PyTypeObject *type = Py_TYPE (self);
307- std::string msg;
308- #if defined(PYPY_VERSION)
309- msg += handle ((PyObject *) type).attr (" __module__" ).cast <std::string>() + " ." ;
310- #endif
311- msg += type->tp_name ;
312- msg += " : No constructor defined!" ;
315+ std::string msg = get_tp_name (type) + " : No constructor defined!" ;
313316 PyErr_SetString (PyExc_TypeError, msg.c_str ());
314317 return -1 ;
315318}
@@ -448,7 +451,7 @@ extern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) {
448451extern " C" inline int pybind11_set_dict (PyObject *self, PyObject *new_dict, void *) {
449452 if (!PyDict_Check (new_dict)) {
450453 PyErr_Format (PyExc_TypeError, " __dict__ must be set to a dictionary, not a '%.200s'" ,
451- Py_TYPE (new_dict)-> tp_name );
454+ get_tp_name ( Py_TYPE (new_dict)). c_str () );
452455 return -1 ;
453456 }
454457 PyObject *&dict = *_PyObject_GetDictPtr (self);
@@ -476,9 +479,8 @@ extern "C" inline int pybind11_clear(PyObject *self) {
476479inline void enable_dynamic_attributes (PyHeapTypeObject *heap_type) {
477480 auto type = &heap_type->ht_type ;
478481#if defined(PYPY_VERSION) && (PYPY_VERSION_NUM < 0x06000000)
479- pybind11_fail (std::string (type->tp_name ) + " : dynamic attributes are "
480- " currently not supported in "
481- " conjunction with PyPy!" );
482+ pybind11_fail (get_tp_type (type) + " : dynamic attributes are currently not "
483+ " supported in conjunction with PyPy!" );
482484#endif
483485 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
484486 type->tp_dictoffset = type->tp_basicsize ; // place dict at the end
0 commit comments