File tree Expand file tree Collapse file tree 3 files changed +26
-2
lines changed
Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -1013,7 +1013,7 @@ class generic_type : public object {
10131013 PYBIND11_OBJECT_DEFAULT (generic_type, object, PyType_Check)
10141014protected:
10151015 void initialize (const type_record &rec) {
1016- if (rec.scope && hasattr (rec.scope , rec.name ))
1016+ if (rec.scope && hasattr (rec.scope , " __dict__ " ) && rec. scope . attr ( " __dict__ " ). contains ( rec.name ))
10171017 pybind11_fail (" generic_type: cannot initialize type \" " + std::string (rec.name ) +
10181018 " \" : an object with that name is already defined" );
10191019
@@ -1931,7 +1931,7 @@ class exception : public object {
19311931 std::string full_name = scope.attr (" __name__" ).cast <std::string>() +
19321932 std::string (" ." ) + name;
19331933 m_ptr = PyErr_NewException (const_cast <char *>(full_name.c_str ()), base.ptr (), NULL );
1934- if (hasattr (scope, name))
1934+ if (hasattr (scope, " __dict__ " ) && scope. attr ( " __dict__ " ). contains ( name))
19351935 pybind11_fail (" Error during initialization: multiple incompatible "
19361936 " definitions with name \" " + std::string (name) + " \" " );
19371937 scope.attr (name) = *this ;
Original file line number Diff line number Diff line change @@ -429,6 +429,22 @@ TEST_SUBMODULE(class_, m) {
429429 py::class_<Empty>(m, " Empty" )
430430 .def (py::init<>());
431431
432+ // test_base_and_derived_nested_scope
433+ struct BaseWithNested {
434+ struct Nested {};
435+ };
436+
437+ struct DerivedWithNested : BaseWithNested {
438+ struct Nested {};
439+ };
440+
441+ py::class_<BaseWithNested> baseWithNested_class (m, " BaseWithNested" );
442+ py::class_<DerivedWithNested, BaseWithNested> derivedWithNested_class (m, " DerivedWithNested" );
443+ py::class_<BaseWithNested::Nested>(baseWithNested_class, " Nested" )
444+ .def_static (" get_name" , []() { return " BaseWithNested::Nested" ; });
445+ py::class_<DerivedWithNested::Nested>(derivedWithNested_class, " Nested" )
446+ .def_static (" get_name" , []() { return " DerivedWithNested::Nested" ; });
447+
432448 // test_register_duplicate_class
433449 struct Duplicate {};
434450 struct OtherDuplicate {};
Original file line number Diff line number Diff line change @@ -385,6 +385,14 @@ def test_multiple_instances_with_same_pointer(capture):
385385 # and just completes without crashing, we're good.
386386
387387
388+ # https://github.com/pybind/pybind11/issues/1624
389+ def test_base_and_derived_nested_scope ():
390+ assert issubclass (m .DerivedWithNested , m .BaseWithNested )
391+ assert m .BaseWithNested .Nested != m .DerivedWithNested .Nested
392+ assert m .BaseWithNested .Nested .get_name () == "BaseWithNested::Nested"
393+ assert m .DerivedWithNested .Nested .get_name () == "DerivedWithNested::Nested"
394+
395+
388396def test_register_duplicate_class ():
389397 import types
390398 module_scope = types .ModuleType ("module_scope" )
You can’t perform that action at this time.
0 commit comments