From c40d8c617f10f04d03b2ef13e6fa032afc9b6ce3 Mon Sep 17 00:00:00 2001 From: Yung-Yu Chen Date: Mon, 26 Dec 2016 18:25:42 +0800 Subject: [PATCH] Fix segfault when repr() with pybind11 type with metaclass (#571) * Fixed a regression that was introduced in the PyPy patch: use ht_qualname_meta instead of ht_qualname to fix PyHeapTypeObject->ht_qualname field. * Added a qualname/repr test that works in both Python 3.3+ and previous versions --- include/pybind11/pybind11.h | 2 +- tests/test_python_types.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 84b60afd8..9caf35e13 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -701,7 +701,7 @@ protected: #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 /* Qualified names for Python >= 3.3 */ - type->ht_qualname = ht_qualname.release().ptr(); + type->ht_qualname = ht_qualname_meta.release().ptr(); #endif type->ht_type.tp_name = strdup(meta_name_.c_str()); type->ht_type.tp_base = &PyType_Type; diff --git a/tests/test_python_types.py b/tests/test_python_types.py index 347abaaee..cb28e1ff1 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -3,6 +3,12 @@ import pytest from pybind11_tests import ExamplePythonTypes, ConstructorStats, has_optional, has_exp_optional +def test_repr(): + # In Python 3.3+, repr() accesses __qualname__ + assert "ExamplePythonTypes__Meta" in repr(type(ExamplePythonTypes)) + assert "ExamplePythonTypes" in repr(ExamplePythonTypes) + + def test_static(): ExamplePythonTypes.value = 15 assert ExamplePythonTypes.value == 15