Skip to content

Commit a75eea5

Browse files
Merge pull request #446 from RustPython/none_str
Add __repr__ and __new__ to NoneType.
2 parents 8835b47 + a735bb3 commit a75eea5

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

tests/snippets/none.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ def none2():
1414
assert none() is x
1515

1616
assert none() is none2()
17+
18+
assert str(None) == 'None'
19+
assert repr(None) == 'None'
20+
assert type(None)() is None

vm/src/obj/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod objiter;
1717
pub mod objlist;
1818
pub mod objmap;
1919
pub mod objmemory;
20+
pub mod objnone;
2021
pub mod objobject;
2122
pub mod objproperty;
2223
pub mod objrange;

vm/src/obj/objnone.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
2+
use super::super::vm::VirtualMachine;
3+
use super::objtype;
4+
5+
pub fn init(context: &PyContext) {
6+
let none_type = &context.none.typ();
7+
context.set_attr(&none_type, "__new__", context.new_rustfunc(none_new));
8+
context.set_attr(&none_type, "__repr__", context.new_rustfunc(none_repr));
9+
}
10+
11+
fn none_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
12+
arg_check!(
13+
vm,
14+
args,
15+
required = [(_zelf, Some(vm.ctx.type_type.clone()))]
16+
);
17+
Ok(vm.get_none())
18+
}
19+
20+
fn none_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
21+
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
22+
Ok(vm.ctx.new_str("None".to_string()))
23+
}

vm/src/pyobject.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use super::obj::objiter;
1818
use super::obj::objlist;
1919
use super::obj::objmap;
2020
use super::obj::objmemory;
21+
use super::obj::objnone;
2122
use super::obj::objobject;
2223
use super::obj::objproperty;
2324
use super::obj::objrange;
@@ -318,6 +319,7 @@ impl PyContext {
318319
objbool::init(&context);
319320
objcode::init(&context);
320321
objframe::init(&context);
322+
objnone::init(&context);
321323
exceptions::init(&context);
322324
context
323325
}

0 commit comments

Comments
 (0)