Skip to content

Commit 11a0752

Browse files
committed
Fix mappingproxy builtins.
1 parent f09c523 commit 11a0752

File tree

4 files changed

+49
-55
lines changed

4 files changed

+49
-55
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_mappingproxy.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ def fn_del(mp):
6666

6767

6868
def test_views():
69-
d = dict()
70-
d['a'] = 1
71-
d['b'] = 2
72-
d['c'] = 3
69+
d = {"a": 1, "b": 2, "c": 3}
7370
mp = _mappingproxy(d)
7471

7572
assert len(mp) == 3
76-
# assert d.keys() == {'a', 'b', 'c'}
73+
assert d.keys() == {'a', 'b', 'c'}
74+
assert mp.keys() == d.keys()
7775
assert len(mp.keys()) == 3, "keys view has invalid length"
7876
assert set(mp.keys()) == {'a', 'b', 'c'}, "keys view invalid"
7977
assert len(mp.values()) == 3, "values view has invalid length"
@@ -122,3 +120,12 @@ def mp_init(*args):
122120
return _mappingproxy(*args)
123121
assert_raises(TypeError, mp_init)
124122
assert_raises(TypeError, mp_init, None)
123+
124+
125+
def test_iter():
126+
d = {"a": 1, "b": 2, "c": 3}
127+
mp = _mappingproxy(d)
128+
129+
mp_keys = set([k for k in mp])
130+
assert d.keys() == mp_keys
131+

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mappingproxy/MappingproxyBuiltins.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.mappingproxy;
2727

28+
import static com.oracle.graal.python.nodes.SpecialMethodNames.ITEMS;
2829
import static com.oracle.graal.python.nodes.SpecialMethodNames.KEYS;
2930
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CONTAINS__;
3031
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELITEM__;
3132
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
3233
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__;
34+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
3335
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
3436
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
3537
import static com.oracle.graal.python.runtime.exception.PythonErrorType.KeyError;
@@ -66,12 +68,21 @@ public abstract static class InitNode extends PythonBinaryBuiltinNode {
6668

6769
@Specialization
6870
@SuppressWarnings("unused")
69-
Object doPDict(PMappingproxy self, Object mapping) {
71+
Object doPMappingproxy(PMappingproxy self, Object mapping) {
7072
// nothing to do
7173
return PNone.NONE;
7274
}
7375
}
7476

77+
@Builtin(name = __ITER__, fixedNumOfArguments = 1)
78+
@GenerateNodeFactory
79+
public abstract static class IterNode extends PythonUnaryBuiltinNode {
80+
@Specialization
81+
Object run(PMappingproxy self) {
82+
return factory().createDictKeysIterator(self);
83+
}
84+
}
85+
7586
// keys()
7687
@Builtin(name = KEYS, fixedNumOfArguments = 1)
7788
@GenerateNodeFactory
@@ -83,6 +94,28 @@ public PDictView keys(PMappingproxy self) {
8394
}
8495
}
8596

97+
// items()
98+
@Builtin(name = ITEMS, fixedNumOfArguments = 1)
99+
@GenerateNodeFactory
100+
public abstract static class ItemsNode extends PythonUnaryBuiltinNode {
101+
102+
@Specialization
103+
public PDictView items(PMappingproxy self) {
104+
return factory().createDictItemsView(self);
105+
}
106+
}
107+
108+
// values()
109+
@Builtin(name = "values", fixedNumOfArguments = 1)
110+
@GenerateNodeFactory
111+
public abstract static class ValuesNode extends PythonUnaryBuiltinNode {
112+
113+
@Specialization
114+
public PDictView values(PMappingproxy self) {
115+
return factory().createDictValuesView(self);
116+
}
117+
}
118+
86119
// get(key[, default])
87120
@Builtin(name = "get", minNumOfArguments = 2, maxNumOfArguments = 3)
88121
@GenerateNodeFactory

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,11 @@ public PDictView createDictKeysView(PHashingCollection dict) {
473473
return trace(new PDictKeysView(lookupClass(PythonBuiltinClassType.PDictKeysView), dict));
474474
}
475475

476-
public PDictView createDictValuesView(PDict dict) {
476+
public PDictView createDictValuesView(PHashingCollection dict) {
477477
return trace(new PDictValuesView(lookupClass(PythonBuiltinClassType.PDictValuesView), dict));
478478
}
479479

480-
public PDictView createDictItemsView(PDict dict) {
480+
public PDictView createDictItemsView(PHashingCollection dict) {
481481
return trace(new PDictItemsView(lookupClass(PythonBuiltinClassType.PDictItemsView), dict));
482482
}
483483

graalpython/lib-graalpython/_mappingproxy.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,9 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
class mappingproxy_iterator(object):
41-
def __init__(self, proxy):
42-
self.items = proxy.keys()
43-
self.index = 0
44-
45-
def __iter__(self):
46-
return self
47-
48-
def __length_hint__(self):
49-
return len(self.items)
50-
51-
def __next__(self):
52-
if self.index >= len(self.items):
53-
raise StopIteration
54-
else:
55-
index = self.index
56-
self.index += 1
57-
return self.items[index]
58-
59-
6040
mappingproxy = type(type.__dict__)
6141

6242

63-
def __iter__(self):
64-
return mappingproxy_iterator(self)
65-
mappingproxy.__iter__ = __iter__
66-
67-
68-
def clear(self):
69-
for k in self.keys():
70-
delete_attribute(self.obj, k)
71-
mappingproxy.clear = clear
72-
73-
74-
def items(self):
75-
items = []
76-
for k in self.keys():
77-
items.append(tuple([k, self.get(k)]))
78-
return items
79-
mappingproxy.items = items
80-
81-
82-
def values(self):
83-
values = []
84-
for k in self.keys():
85-
values.append(self.get(k))
86-
return values
87-
mappingproxy.values = values
88-
89-
9043
def __repr__(self):
9144
d = []
9245
for k in self.keys():
@@ -98,3 +51,4 @@ def __repr__(self):
9851

9952

10053
mappingproxy.update = dict.update
54+
mappingproxy.__hash__ = dict.__hash__

0 commit comments

Comments
 (0)