Skip to content

Commit e3178ab

Browse files
committed
allow setting the __dict__ attribute
- add unit test
1 parent b94882c commit e3178ab

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
41+
def assert_raises(err, fn, *args, **kwargs):
42+
raised = False
43+
try:
44+
fn(*args, **kwargs)
45+
except err:
46+
raised = True
47+
assert raised
48+
49+
50+
def test_set_dict_attr():
51+
o = object()
52+
53+
def get_dict_attr():
54+
return o.__dict__
55+
56+
def set_dict_attr():
57+
o.__dict__ = {'a': 10}
58+
59+
assert_raises(AttributeError, get_dict_attr)
60+
assert_raises(AttributeError, set_dict_attr)
61+
62+
class MyClass(object):
63+
pass
64+
65+
m = MyClass()
66+
assert m.__dict__ == {}
67+
m.__dict__ = {'a': 10}
68+
assert m.__dict__ == {'a': 10}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/getsetdescriptor/GetSetDescriptorTypeBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Object set(GetSetDescriptor descr, Object obj, Object value) {
158158
return callNode.executeObject(descr.getSet(), obj, value);
159159
} else {
160160
branchProfile.enter();
161-
throw raise(AttributeError, "attribute '%s' of '%s' objects is not writable", descr.getName(), descr.getType().getName());
161+
throw raise(AttributeError, "attribute '%s' of '%s' object is not writable", descr.getName(), descr.getType().getName());
162162
}
163163
}
164164
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
5959
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
6060
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
61+
import com.oracle.graal.python.builtins.objects.dict.PDict;
6162
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
6263
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6364
import com.oracle.graal.python.builtins.objects.function.PythonCallable;
@@ -474,21 +475,22 @@ protected PNone doIt(Object object, Object key,
474475
}
475476
}
476477

477-
@Builtin(name = __DICT__, fixedNumOfPositionalArgs = 1, isGetter = true)
478+
@Builtin(name = __DICT__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
478479
@GenerateNodeFactory
479-
static abstract class DictNode extends PythonUnaryBuiltinNode {
480+
static abstract class DictNode extends PythonBinaryBuiltinNode {
481+
protected boolean isExactObjectInstance(PythonObject self) {
482+
return lookupClass(PythonBuiltinClassType.PythonObject) == self.getPythonClass();
483+
}
484+
485+
@SuppressWarnings("unused")
480486
@Specialization
481-
Object dict(@SuppressWarnings("unused") PythonClass self) {
487+
Object dict(PythonClass self, PNone none) {
482488
CompilerDirectives.transferToInterpreter();
483489
throw new AssertionError();
484490
}
485491

486-
protected boolean isExactObjectInstance(PythonObject self) {
487-
return lookupClass(PythonBuiltinClassType.PythonObject) == self.getPythonClass();
488-
}
489-
490492
@Specialization(guards = {"!isBuiltinObject(self)", "!isClass(self)", "!isExactObjectInstance(self)"})
491-
Object dict(PythonObject self) {
493+
Object dict(PythonObject self, @SuppressWarnings("unused") PNone none) {
492494
PHashingCollection dict = self.getDict();
493495
if (dict == null) {
494496
dict = factory().createDictFixedStorage(self);
@@ -497,8 +499,14 @@ Object dict(PythonObject self) {
497499
return dict;
498500
}
499501

502+
@Specialization(guards = {"!isBuiltinObject(self)", "!isClass(self)", "!isExactObjectInstance(self)"})
503+
Object dict(PythonObject self, PDict dict) {
504+
self.setDict(dict);
505+
return PNone.NONE;
506+
}
507+
500508
@Fallback
501-
Object dict(Object self) {
509+
Object dict(Object self, @SuppressWarnings("unused") Object dict) {
502510
throw raise(AttributeError, "'%p' object has no attribute '__dict__'", self);
503511
}
504512

0 commit comments

Comments
 (0)