Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9869ef7

Browse files
committed
Add delattr builtin method.
1 parentb7a0b0f commit9869ef7

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

‎vm/src/builtins.rs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,14 @@ fn builtin_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
125125
compile::compile(vm,&source, mode,None)
126126
}
127127

128-
// builtin_delattr
128+
fnbuiltin_delattr(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
129+
arg_check!(
130+
vm,
131+
args,
132+
required =[(obj,None),(attr,Some(vm.ctx.str_type()))]
133+
);
134+
vm.del_attr(obj, attr.clone())
135+
}
129136

130137
fnbuiltin_dir(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
131138
if args.args.is_empty(){
@@ -506,12 +513,9 @@ fn builtin_setattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
506513
args,
507514
required =[(obj,None),(attr,Some(vm.ctx.str_type())),(value,None)]
508515
);
509-
ifletPyObjectKind::String{value:ref name} = attr.borrow().kind{
510-
obj.clone().set_attr(name, value.clone());
511-
Ok(vm.get_none())
512-
}else{
513-
panic!("argument checking failure: attr not string")
514-
}
516+
let name = objstr::get_value(attr);
517+
obj.clone().set_attr(&name, value.clone());
518+
Ok(vm.get_none())
515519
}
516520

517521
// builtin_slice
@@ -540,6 +544,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
540544
dict.insert(String::from("chr"), ctx.new_rustfunc(builtin_chr));
541545
dict.insert(String::from("compile"), ctx.new_rustfunc(builtin_compile));
542546
dict.insert(String::from("complex"), ctx.complex_type());
547+
dict.insert(String::from("delattr"), ctx.new_rustfunc(builtin_delattr));
543548
dict.insert(String::from("dict"), ctx.dict_type());
544549
dict.insert(String::from("divmod"), ctx.new_rustfunc(builtin_divmod));
545550
dict.insert(String::from("dir"), ctx.new_rustfunc(builtin_dir));

‎vm/src/frame.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ impl Frame {
10211021
fndelete_attr(&mutself,vm:&mutVirtualMachine,attr_name:&str) ->FrameResult{
10221022
let parent =self.pop_value();
10231023
let name = vm.ctx.new_str(attr_name.to_string());
1024-
vm.call_method(&parent,"__delattr__",vec![name])?;
1024+
vm.del_attr(&parent, name)?;
10251025
Ok(None)
10261026
}
10271027

‎vm/src/import.rs‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,18 @@ fn import_uncached_module(
3030
let source = parser::read_file(filepath.as_path())
3131
.map_err(|e| vm.new_exception(import_error.clone(), e))?;
3232

33-
let code_obj =matchcompile::compile(
33+
let code_obj = compile::compile(
3434
vm,
3535
&source,
3636
compile::Mode::Exec,
3737
Some(filepath.to_str().unwrap().to_string()),
38-
){
39-
Ok(bytecode) =>{
40-
debug!("Code object: {:?}", bytecode);
41-
bytecode
42-
}
43-
Err(value) =>returnErr(value),
44-
};
38+
)?;
39+
debug!("Code object: {:?}", code_obj);
4540

4641
let builtins = vm.get_builtin_scope();
4742
let scope = vm.ctx.new_scope(Some(builtins));
4843
scope.set_item(&"__name__".to_string(), vm.new_str(module.to_string()));
49-
match vm.run_code_obj(code_obj, scope.clone()){
50-
Ok(_) =>{}
51-
Err(value) =>returnErr(value),
52-
}
44+
vm.run_code_obj(code_obj, scope.clone())?;
5345
Ok(vm.ctx.new_module(module, scope))
5446
}
5547

‎vm/src/obj/objtype.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ pub fn type_call(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
116116
debug!("type_call: {:?}", args);
117117
let typ = args.shift();
118118
let new = typ.get_attr("__new__").unwrap();
119-
let obj =match vm.invoke(new, args.insert(typ.clone())){
120-
Ok(res) => res,
121-
Err(err) =>returnErr(err),
122-
};
119+
let obj = vm.invoke(new, args.insert(typ.clone()))?;
123120

124121
ifletSome(init) = obj.typ().get_attr("__init__"){
125122
let res = vm.invoke(init, args.insert(obj.clone()))?;

‎vm/src/vm.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ impl VirtualMachine {
420420
self.call_method(&obj,"__getattribute__",vec![attr_name])
421421
}
422422

423+
pubfndel_attr(&mutself,obj:&PyObjectRef,attr_name:PyObjectRef) ->PyResult{
424+
self.call_method(&obj,"__delattr__",vec![attr_name])
425+
}
426+
423427
// get_method should be used for internal access to magic methods (by-passing
424428
// the full getattribute look-up.
425429
pubfnget_method(&mutself,obj:PyObjectRef,method_name:&str) ->PyResult{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp