Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix classmethod descr_get to match CPython behavior
  Simplify classmethod.__get__ to always create a PyBoundMethod binding
  the callable to the class, matching CPython's cm_descr_get which simply
  calls PyMethod_New(cm->cm_callable, type).

  The previous implementation incorrectly tried to call __get__ on the
  wrapped callable, which broke when a bound method was passed to
  classmethod() (e.g. classmethod(A().foo)).
  • Loading branch information
ever0de committed Mar 13, 2026
commit aa1650046e6035f6ac158cfa6d8c0fce804903e6
1 change: 0 additions & 1 deletion Lib/test/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ def bar(): return 42
self.assertEqual(bar(), 42)
self.assertEqual(actions, expected_actions)

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_bound_function_inside_classmethod(self):
class A:
def foo(self, cls):
Expand Down
7 changes: 1 addition & 6 deletions crates/vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ impl GetDescriptor for PyClassMethod {
) -> PyResult {
let (zelf, _obj) = Self::_unwrap(&zelf, obj, vm)?;
let cls = cls.unwrap_or_else(|| _obj.class().to_owned().into());
// Clone and release lock before calling Python code to prevent deadlock
let callable = zelf.callable.lock().clone();
let call_descr_get: PyResult<PyObjectRef> = callable.get_attr("__get__", vm);
match call_descr_get {
Err(_) => Ok(PyBoundMethod::new(cls, callable).into_ref(&vm.ctx).into()),
Ok(call_descr_get) => call_descr_get.call((cls.clone(), cls), vm),
}
Ok(PyBoundMethod::new(cls, callable).into_ref(&vm.ctx).into())
}
}

Expand Down
Loading