Skip to content
Open
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
1 change: 1 addition & 0 deletions py/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const (
InternalMethodImport
InternalMethodEval
InternalMethodExec
InternalMethodVars
)

var MethodType = NewType("method", "method object")
Expand Down
7 changes: 6 additions & 1 deletion stdlib/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func init() {
py.MustNewMethod("setattr", builtin_setattr, 0, setattr_doc),
py.MustNewMethod("sorted", builtin_sorted, 0, sorted_doc),
py.MustNewMethod("sum", builtin_sum, 0, sum_doc),
// py.MustNewMethod("vars", builtin_vars, 0, vars_doc),
py.MustNewMethod("vars", py.InternalMethodVars, 0, vars_doc),
}
globals := py.StringDict{
"None": py.None,
Expand Down Expand Up @@ -1189,6 +1189,11 @@ const globals_doc = `globals() -> dictionary

Return the dictionary containing the current scope's global variables.`

const vars_doc = `vars([object]) -> dictionary

Without an argument, equivalent to locals().
With an argument, equivalent to object.__dict__.`

const sum_doc = `sum($module, iterable, start=0, /)
--
Return the sum of a \'start\' value (default: 0) plus an iterable of numbers
Expand Down
33 changes: 33 additions & 0 deletions stdlib/builtin/tests/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ def fn(x):
assert locals()["x"] == 1
fn(1)

doc="vars"
def fn(x):
assert vars()["x"] == 1
fn(1)

# Test vars() with an object that has __dict__ (function objects have __dict__)
def test_func():
pass

assert vars(test_func) == test_func.__dict__
assert isinstance(vars(test_func), dict)

ok = False
try:
vars(test_func, test_func)
except TypeError:
ok = True
assert ok, "TypeError not raised for too many arguments"

ok = False
try:
vars(x=1)
except TypeError:
ok = True
assert ok, "TypeError not raised for keyword arguments"

ok = False
try:
vars(test_func, y=1)
except TypeError:
ok = True
assert ok, "TypeError not raised for keyword arguments with object"

def func(p):
return p[1]

Expand Down
17 changes: 17 additions & 0 deletions vm/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,23 @@ func callInternal(fn py.Object, args py.Tuple, kwargs py.StringDict, f *py.Frame
case py.InternalMethodExec:
f.FastToLocals()
return builtinExec(f.Context, args, kwargs, f.Locals, f.Globals, f.Builtins)
case py.InternalMethodVars:
if len(kwargs) > 0 {
return nil, py.ExceptionNewf(py.TypeError, "vars() takes no keyword arguments")
}
switch len(args) {
case 0:
f.FastToLocals()
return f.Locals, nil
case 1:
attr, err := py.GetAttrString(args[0], "__dict__")
if err != nil {
return nil, err
}
return attr, nil
default:
return nil, py.ExceptionNewf(py.TypeError, "vars() takes at most 1 argument (%d given)", len(args))
}
default:
return nil, py.ExceptionNewf(py.SystemError, "Internal method %v not found", x)
}
Expand Down
Loading