Skip to content

Commit e794226

Browse files
committed
added __name__ to scope
1 parent d650476 commit e794226

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

tests/snippets/import_name.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def import_func():
2+
assert __name__ == "import_name"

tests/snippets/name.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#when name.py is run __name__ should equal to __main__
2+
assert __name__ == "__main__"
3+
4+
from import_name import import_func
5+
6+
#__name__ should be set to import_func
7+
import_func()
8+
9+
assert __name__ == "__main__"

vm/src/builtins.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pub fn builtin_print(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
339339
Ok(v) => objstr::get_value(&v),
340340
Err(err) => return Err(err),
341341
};
342-
print!("{} ", s);
342+
print!("{}", s);
343343
}
344344
println!();
345345
io::stdout().flush().unwrap();
@@ -395,6 +395,11 @@ fn builtin_setattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
395395
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
396396
// scope[String::from("print")] = print;
397397
let mut dict = HashMap::new();
398+
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
399+
dict.insert(
400+
String::from("__name__"),
401+
ctx.new_str(String::from("__main__")),
402+
);
398403
dict.insert(String::from("abs"), ctx.new_rustfunc(builtin_abs));
399404
dict.insert(String::from("all"), ctx.new_rustfunc(builtin_all));
400405
dict.insert(String::from("any"), ctx.new_rustfunc(builtin_any));

0 commit comments

Comments
 (0)