Skip to content

Commit 98a7cea

Browse files
committed
CachedProperty.__set_name__ will now raise a RuntimeError if it was already bound to a class.
1 parent 2add203 commit 98a7cea

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/core/modules/core/core_cache.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ object CCachedProperty::bind(object self, object owner, str name)
247247

248248
void CCachedProperty::__set_name__(object owner, str name)
249249
{
250+
if (m_name && !m_owner.is_none())
251+
{
252+
const char *szName = extract<const char *>(str(".").join(make_tuple(m_owner().attr("__qualname__"), m_name)));
253+
BOOST_RAISE_EXCEPTION(
254+
PyExc_RuntimeError,
255+
"This property was already bound as \"%s\".",
256+
szName
257+
)
258+
}
259+
250260
m_name = name;
251261
m_owner = object(handle<>(PyWeakref_NewRef(owner.ptr(), NULL)));
252262
}

src/core/modules/core/core_cache_wrap.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ void export_cached_property(scope _cache)
269269
":param class owner:\n"
270270
" The class this property is being bound to.\n"
271271
":param str name:\n"
272-
" The name this property is being bound as.",
272+
" The name this property is being bound as."
273+
"\n"
274+
":raises RuntimeError:\n"
275+
" If this property was already bound to a class.",
273276
args("self", "owner", "name")
274277
);
275278

0 commit comments

Comments
 (0)