Skip to content

Commit 5c5d272

Browse files
committed
Support for all co_* that can implemented currently.
1 parent 6539f07 commit 5c5d272

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

tests/snippets/code.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22

33
code_class = type(c1)
44

5-
def f(x, y, power=1):
5+
def f(x, y, *args, power=1, **kwargs):
6+
assert code_class == type(c1)
67
z = x * y
78
return z ** power
89

910
c2 = f.__code__
1011
# print(c2)
1112
assert type(c2) == code_class
12-
# print(dir(c2))
13-
assert c2.co_argcount == 3
14-
assert c2.co_cellvars == ()
13+
print(dir(c2))
14+
assert c2.co_argcount == 2
15+
# assert c2.co_cellvars == ()
1516
# assert isinstance(c2.co_code, bytes)
16-
assert c2.co_consts == (None,)
17+
# assert c2.co_consts == (None,)
1718
assert "code.py" in c2.co_filename
1819
assert c2.co_firstlineno == 5, str(c2.co_firstlineno)
1920
# assert isinstance(c2.co_flags, int) # 'OPTIMIZED, NEWLOCALS, NOFREE'
20-
# assert c2.co_freevars == ()
21-
# assert c2.co_kwonlyargcount == 0
21+
# assert c2.co_freevars == (), str(c2.co_freevars)
22+
assert c2.co_kwonlyargcount == 1, (c2.co_kwonlyargcount)
2223
# assert c2.co_lnotab == 0, c2.co_lnotab # b'\x00\x01' # Line number table
23-
# assert c2.co_name == 'f', c2.co_name
24-
# assert c2.co_names == (), c2.co_names # , c2.co_names
24+
assert c2.co_name == 'f', c2.co_name
25+
# assert c2.co_names == ('code_class', 'type', 'c1', 'AssertionError'), c2.co_names # , c2.co_names
2526
# assert c2.co_nlocals == 4, c2.co_nlocals #
26-
# assert c2.co_stacksize == ... 2 'co_stacksize',
27+
# assert c2.co_stacksize == 2, 'co_stacksize',
2728
# assert c2.co_varnames == ('x', 'y', 'power', 'z'), c2.co_varnames

vm/src/obj/objcode.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,19 @@ pub fn init(context: &PyContext) {
1313
let code_type = &context.code_type;
1414
context.set_attr(code_type, "__new__", context.new_rustfunc(code_new));
1515
context.set_attr(code_type, "__repr__", context.new_rustfunc(code_repr));
16-
context.set_attr(
17-
code_type,
18-
"co_argcount",
19-
context.new_member_descriptor(code_co_argcount),
20-
);
21-
context.set_attr(
22-
code_type,
23-
"co_cellvars",
24-
context.new_member_descriptor(code_co_cellvars),
25-
);
26-
context.set_attr(
27-
code_type,
28-
"co_consts",
29-
context.new_member_descriptor(code_co_consts),
30-
);
31-
context.set_attr(
32-
code_type,
33-
"co_filename",
34-
context.new_member_descriptor(code_co_filename),
35-
);
36-
context.set_attr(
37-
code_type,
38-
"co_firstlineno",
39-
context.new_member_descriptor(code_co_firstlineno),
40-
);
16+
17+
for (name, f) in vec![
18+
(
19+
"co_argcount",
20+
code_co_argcount as fn(&mut VirtualMachine, PyFuncArgs) -> PyResult,
21+
),
22+
("co_filename", code_co_filename),
23+
("co_firstlineno", code_co_firstlineno),
24+
("co_kwonlyargcount", code_co_kwonlyargcount),
25+
("co_name", code_co_name),
26+
] {
27+
context.set_attr(code_type, name, context.new_member_descriptor(f))
28+
}
4129
}
4230

4331
pub fn get_value(obj: &PyObjectRef) -> bytecode::CodeObject {
@@ -87,16 +75,6 @@ fn code_co_argcount(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8775
Ok(vm.ctx.new_int(code_obj.arg_names.len()))
8876
}
8977

90-
fn code_co_cellvars(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
91-
let _code_obj = member_code_obj(vm, args)?;
92-
Ok(vm.ctx.new_tuple(vec![]))
93-
}
94-
95-
fn code_co_consts(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
96-
let _code_obj = member_code_obj(vm, args)?;
97-
Ok(vm.ctx.new_tuple(vec![vm.get_none()]))
98-
}
99-
10078
fn code_co_filename(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
10179
let code_obj = member_code_obj(vm, args)?;
10280
let source_path = code_obj.source_path;
@@ -107,3 +85,13 @@ fn code_co_firstlineno(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
10785
let code_obj = member_code_obj(vm, args)?;
10886
Ok(vm.ctx.new_int(code_obj.first_line_number))
10987
}
88+
89+
fn code_co_kwonlyargcount(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
90+
let code_obj = member_code_obj(vm, args)?;
91+
Ok(vm.ctx.new_int(code_obj.kwonlyarg_names.len()))
92+
}
93+
94+
fn code_co_name(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
95+
let code_obj = member_code_obj(vm, args)?;
96+
Ok(vm.new_str(code_obj.obj_name))
97+
}

0 commit comments

Comments
 (0)