Skip to content

Commit a2ce4c8

Browse files
committed
Add special case handling for __get__(None, NoneType)
1 parent 7bf8378 commit a2ce4c8

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

vm/src/obj/objfunction.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::super::pyobject::{
2-
AttributeProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
3-
TypeProtocol,
2+
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
3+
PyResult, TypeProtocol,
44
};
55
use super::super::vm::VirtualMachine;
66
use super::objtype;
@@ -22,7 +22,17 @@ pub fn init(context: &PyContext) {
2222
}
2323

2424
fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
25-
Ok(vm.new_bound_method(args.args[0].clone(), args.args[1].clone()))
25+
arg_check!(
26+
vm,
27+
args,
28+
required = [(function, None), (obj, None), (cls, None)]
29+
);
30+
31+
if obj.is(&vm.get_none()) && !cls.is(&obj.typ()) {
32+
Ok(function.clone())
33+
} else {
34+
Ok(vm.ctx.new_bound_method(function.clone(), obj.clone()))
35+
}
2636
}
2737

2838
fn member_get(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
@@ -50,7 +60,7 @@ fn classmethod_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5060
match cls.get_attr("function") {
5161
Some(function) => {
5262
let py_obj = owner.clone();
53-
let py_method = vm.new_bound_method(function, py_obj);
63+
let py_method = vm.ctx.new_bound_method(function, py_obj);
5464
Ok(py_method)
5565
}
5666
None => {

vm/src/obj/objproperty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn property_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3131

3232
match cls.get_attr("fget") {
3333
Some(getter) => {
34-
let py_method = vm.new_bound_method(getter, inst.clone());
34+
let py_method = vm.ctx.new_bound_method(getter, inst.clone());
3535
vm.invoke(py_method, PyFuncArgs::default())
3636
}
3737
None => {

vm/src/vm.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use super::obj::objsequence;
1717
use super::obj::objstr;
1818
use super::obj::objtype;
1919
use super::pyobject::{
20-
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef,
21-
PyResult, TypeProtocol,
20+
AttributeProtocol, DictProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult,
21+
TypeProtocol,
2222
};
2323
use super::stdlib;
2424
use super::sysmodule;
@@ -92,14 +92,6 @@ impl VirtualMachine {
9292
self.ctx.none()
9393
}
9494

95-
pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef {
96-
if object.is(&self.get_none()) {
97-
function
98-
} else {
99-
self.ctx.new_bound_method(function, object)
100-
}
101-
}
102-
10395
pub fn get_type(&self) -> PyObjectRef {
10496
self.ctx.type_type()
10597
}

0 commit comments

Comments
 (0)