Skip to content

Add __repr__ and __new__ to NoneType. #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2019
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
4 changes: 4 additions & 0 deletions tests/snippets/none.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ def none2():
assert none() is x

assert none() is none2()

assert str(None) == 'None'
assert repr(None) == 'None'
assert type(None)() is None
1 change: 1 addition & 0 deletions vm/src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod objiter;
pub mod objlist;
pub mod objmap;
pub mod objmemory;
pub mod objnone;
pub mod objobject;
pub mod objproperty;
pub mod objrange;
Expand Down
23 changes: 23 additions & 0 deletions vm/src/obj/objnone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use super::super::vm::VirtualMachine;
use super::objtype;

pub fn init(context: &PyContext) {
let none_type = &context.none.typ();
context.set_attr(&none_type, "__new__", context.new_rustfunc(none_new));
context.set_attr(&none_type, "__repr__", context.new_rustfunc(none_repr));
}

fn none_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.type_type.clone()))]
);
Ok(vm.get_none())
}

fn none_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
Ok(vm.ctx.new_str("None".to_string()))
}
2 changes: 2 additions & 0 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::obj::objiter;
use super::obj::objlist;
use super::obj::objmap;
use super::obj::objmemory;
use super::obj::objnone;
use super::obj::objobject;
use super::obj::objproperty;
use super::obj::objrange;
Expand Down Expand Up @@ -318,6 +319,7 @@ impl PyContext {
objbool::init(&context);
objcode::init(&context);
objframe::init(&context);
objnone::init(&context);
exceptions::init(&context);
context
}
Expand Down