Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 7016d5b

Browse files
S-YOUtrotterdylan
authored andcommitted
Add builtin setattr (#193)
1 parent e0aa8c8 commit 7016d5b

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

runtime/builtin_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,13 @@ func builtinRepr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
575575
return s.ToObject(), nil
576576
}
577577

578+
func builtinSetAttr(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
579+
if raised := checkFunctionArgs(f, "setattr", args, ObjectType, StrType, ObjectType); raised != nil {
580+
return nil, raised
581+
}
582+
return None, SetAttr(f, args[0], toStrUnsafe(args[1]), args[2])
583+
}
584+
578585
func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
579586
// TODO: Support (cmp=None, key=None, reverse=False)
580587
if raised := checkFunctionArgs(f, "sorted", args, ObjectType); raised != nil {
@@ -663,6 +670,7 @@ func init() {
663670
"print": newBuiltinFunction("print", builtinPrint).ToObject(),
664671
"range": newBuiltinFunction("range", builtinRange).ToObject(),
665672
"repr": newBuiltinFunction("repr", builtinRepr).ToObject(),
673+
"setattr": newBuiltinFunction("setattr", builtinSetAttr).ToObject(),
666674
"sorted": newBuiltinFunction("sorted", builtinSorted).ToObject(),
667675
"True": True.ToObject(),
668676
"unichr": newBuiltinFunction("unichr", builtinUniChr).ToObject(),

runtime/builtin_types_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,31 @@ func TestBuiltinPrint(t *testing.T) {
357357
}
358358
}
359359
}
360+
361+
func TestBuiltinSetAttr(t *testing.T) {
362+
fun := wrapFuncForTest(func(f *Frame, args ...*Object) (*Object, *BaseException) {
363+
fooObject := newTestClass("Foo", []*Type{ObjectType}, newStringDict(map[string]*Object{})).ToObject()
364+
result, raised := builtinSetAttr(f, append(Args{fooObject}, args...), nil)
365+
if raised != nil {
366+
return nil, raised
367+
}
368+
val, raised := GetAttr(f, fooObject, toStrUnsafe(args[0]), nil)
369+
if raised != nil {
370+
return nil, raised
371+
}
372+
return newTestTuple(result, val).ToObject(), nil
373+
})
374+
cases := []invokeTestCase{
375+
{args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'setattr' requires 3 arguments")},
376+
{args: wrapArgs("foo", "bar"), want: newTestTuple(None, "bar").ToObject()},
377+
{args: wrapArgs("foo", 123), want: newTestTuple(None, 123).ToObject()},
378+
{args: wrapArgs("foo"), wantExc: mustCreateException(TypeErrorType, "'setattr' requires 3 arguments")},
379+
{args: wrapArgs("foo", 123, None), wantExc: mustCreateException(TypeErrorType, "'setattr' requires 3 arguments")},
380+
{args: wrapArgs(123, 123), wantExc: mustCreateException(TypeErrorType, "'setattr' requires a 'str' object but received a \"int\"")},
381+
}
382+
for _, cas := range cases {
383+
if err := runInvokeTestCase(fun, &cas); err != "" {
384+
t.Error(err)
385+
}
386+
}
387+
}

testing/builtin_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,26 @@ def __init__(self, x):
247247
assert cmp(a, b) == -1
248248
assert b.cmp_called
249249

250+
# Test setattr
251+
252+
class Foo(object):
253+
pass
254+
255+
setattr(Foo, "a", 1)
256+
assert Foo.a == 1 # pylint: disable=no-member
257+
258+
try:
259+
setattr(Foo, 1, "a")
260+
assert AssertionError
261+
except TypeError:
262+
pass
263+
264+
try:
265+
setattr(Foo)
266+
assert AssertionError
267+
except TypeError:
268+
pass
269+
250270
# Test sorted
251271

252272
assert sorted([3, 2, 4, 1]) == [1, 2, 3, 4]

0 commit comments

Comments
 (0)