From ba647ebc0d8a45e26324e1d9c625f2dc09a89784 Mon Sep 17 00:00:00 2001 From: vfdev-5 Date: Mon, 20 Jan 2025 11:53:12 +0000 Subject: [PATCH] Added test case for issue #867 --- tests/test_thread.cpp | 29 +++++++++++++++++++++++++++++ tests/test_thread.py | 14 +++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/test_thread.cpp b/tests/test_thread.cpp index 54181b7b..97e82960 100644 --- a/tests/test_thread.cpp +++ b/tests/test_thread.cpp @@ -16,6 +16,23 @@ struct GlobalData {} global_data; nb::ft_mutex mutex; +struct ClassWithProperty { +public: + ClassWithProperty(int value): value_(value) {} + int get_prop() const { return value_; } +private: + int value_; +}; + +class ClassWithClassProperty { +public: + ClassWithClassProperty(ClassWithProperty value) : value_(std::move(value)) {}; + const ClassWithProperty& get_prop() const { return value_; } +private: + ClassWithProperty value_; +}; + + NB_MODULE(test_thread_ext, m) { nb::class_(m, "Counter") .def(nb::init<>()) @@ -39,4 +56,16 @@ NB_MODULE(test_thread_ext, m) { nb::class_(m, "GlobalData") .def_static("get", [] { return &global_data; }, nb::rv_policy::reference); + + nb::class_(m, "ClassWithProperty") + .def(nb::init(), nb::arg("value")) + .def_prop_ro("prop2", &ClassWithProperty::get_prop); + + nb::class_(m, "ClassWithClassProperty") + .def( + "__init__", + [](ClassWithClassProperty* self, ClassWithProperty value) { + new (self) ClassWithClassProperty(std::move(value)); + }, nb::arg("value")) + .def_prop_ro("prop1", &ClassWithClassProperty::get_prop); } diff --git a/tests/test_thread.py b/tests/test_thread.py index 19e425d1..1d5992ee 100644 --- a/tests/test_thread.py +++ b/tests/test_thread.py @@ -1,5 +1,5 @@ import test_thread_ext as t -from test_thread_ext import Counter, GlobalData +from test_thread_ext import Counter, GlobalData, ClassWithProperty, ClassWithClassProperty from common import parallelize def test01_object_creation(n_threads=8): @@ -88,3 +88,15 @@ def f(): GlobalData.get() parallelize(f, n_threads=n_threads) + + +def test07_access_attributes(n_threads=8): + n = 1000 + c1 = ClassWithProperty(123) + c2 = ClassWithClassProperty(c1) + + def f(): + for i in range(n): + _ = c2.prop1.prop2 + + parallelize(f, n_threads=n_threads)