Skip to content

Commit 7d7dee7

Browse files
committed
[GR-11082] Harmonize sequence operations and move to nodes.
PullRequest: graalpython/177
2 parents 1d04ab1 + bebfafe commit 7d7dee7

File tree

71 files changed

+2987
-3600
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2987
-3600
lines changed

graalpython/com.oracle.graal.python.tck/src/com/oracle/graal/python/tck/PythonProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ protected static boolean isMapping(Value par0) {
388388
private static boolean hasMemoryError(SnippetRun snippetRun) {
389389
PolyglotException exception = snippetRun.getException();
390390
if (exception != null && exception.isGuestException()) {
391-
return "MemoryError".equals(exception.getMessage());
391+
return "MemoryError".equals(exception.getMessage()) || exception.getMessage().contains("OverflowError");
392392
}
393393
return false;
394394
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ public void parseNumbers() {
181181
@Test
182182
public void parseLiteralList() {
183183
PList list = literalAs("[1, 2]", ListLiteralNode.class, PList.class);
184-
assertEquals(2, list.len());
184+
assertEquals(2, list.getSequenceStorage().length());
185185
assertEquals(1, list.getSequenceStorage().getItemNormalized(0));
186186
assertEquals(2, list.getSequenceStorage().getItemNormalized(1));
187187
list = literalAs("[1]", ListLiteralNode.class, PList.class);
188-
assertEquals(1, list.len());
188+
assertEquals(1, list.getSequenceStorage().length());
189189
assertEquals(1, list.getSequenceStorage().getItemNormalized(0));
190190
list = literalAs("[]", ListLiteralNode.class, PList.class);
191-
assertEquals(0, list.len());
191+
assertEquals(0, list.getSequenceStorage().length());
192192
}
193193

194194
@Test

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/runtime/SequenceStorageTests.java

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -55,43 +55,6 @@ public void objectsGetSlice() {
5555
}
5656
}
5757

58-
@Test
59-
public void objectsSetSlice() {
60-
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
61-
ObjectSequenceStorage slice = new ObjectSequenceStorage(new Object[]{42, 42, 42});
62-
63-
store.setSliceInBound(1, 4, 1, slice);
64-
65-
for (int i = 1; i < 4; i++) {
66-
assertEquals(42, store.getItemNormalized(i));
67-
}
68-
}
69-
70-
@Test
71-
public void objectsSetSliceOutOfBound() {
72-
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
73-
ObjectSequenceStorage slice = new ObjectSequenceStorage(new Object[]{42, 42, 42});
74-
75-
store.setSliceInBound(5, 8, 1, slice);
76-
77-
for (int i = 5; i < 8; i++) {
78-
assertEquals(42, store.getItemNormalized(i));
79-
}
80-
}
81-
82-
@Test
83-
public void objectsDel() {
84-
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
85-
store.delItemInBound(4);
86-
87-
for (int i = 0; i < 4; i++) {
88-
assertEquals(i + 1, store.getItemNormalized(i));
89-
}
90-
91-
assertEquals(6, store.getItemNormalized(4));
92-
assertEquals(5, store.length());
93-
}
94-
9558
@Test
9659
public void objectsInsert() {
9760
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
@@ -101,27 +64,6 @@ public void objectsInsert() {
10164
assertEquals(7, store.length());
10265
}
10366

104-
@Test
105-
public void objectAppend() {
106-
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
107-
store.append(42);
108-
assertEquals(42, store.getItemNormalized(6));
109-
assertEquals(7, store.length());
110-
}
111-
112-
@Test
113-
public void objectExtend() {
114-
ObjectSequenceStorage store = new ObjectSequenceStorage(getObjectValues());
115-
ObjectSequenceStorage other = new ObjectSequenceStorage(getObjectValues());
116-
store.extend(other);
117-
118-
for (int i = 6; i < 12; i++) {
119-
assertEquals(i - 5, store.getItemNormalized(i));
120-
}
121-
122-
assertEquals(12, store.length());
123-
}
124-
12567
/**
12668
* IntSequenceStorage tests.
12769
*/
@@ -147,43 +89,6 @@ public void intGetSlice() {
14789
}
14890
}
14991

150-
@Test
151-
public void intSetSlice() throws SequenceStoreException {
152-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
153-
IntSequenceStorage slice = new IntSequenceStorage(new int[]{42, 42, 42});
154-
155-
store.setSliceInBound(1, 4, 1, slice);
156-
157-
for (int i = 1; i < 4; i++) {
158-
assertEquals(42, store.getItemNormalized(i));
159-
}
160-
}
161-
162-
@Test
163-
public void intSetSliceOutOfBound() throws SequenceStoreException {
164-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
165-
IntSequenceStorage slice = new IntSequenceStorage(new int[]{42, 42, 42});
166-
167-
store.setSliceInBound(5, 8, 1, slice);
168-
169-
for (int i = 5; i < 8; i++) {
170-
assertEquals(42, store.getItemNormalized(i));
171-
}
172-
}
173-
174-
@Test
175-
public void intDel() {
176-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
177-
store.delItemInBound(4);
178-
179-
for (int i = 0; i < 4; i++) {
180-
assertEquals(i + 1, store.getItemNormalized(i));
181-
}
182-
183-
assertEquals(6, store.getItemNormalized(4));
184-
assertEquals(5, store.length());
185-
}
186-
18792
@Test
18893
public void intInsert() throws SequenceStoreException {
18994
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
@@ -192,26 +97,4 @@ public void intInsert() throws SequenceStoreException {
19297
assertEquals(6, store.getItemNormalized(6));
19398
assertEquals(7, store.length());
19499
}
195-
196-
@Test
197-
public void intAppend() throws SequenceStoreException {
198-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
199-
store.append(42);
200-
assertEquals(42, store.getItemNormalized(6));
201-
assertEquals(7, store.length());
202-
}
203-
204-
@Test
205-
public void intExtend() throws SequenceStoreException {
206-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
207-
IntSequenceStorage other = new IntSequenceStorage(getIntValues());
208-
store.extend(other);
209-
210-
for (int i = 6; i < 12; i++) {
211-
assertEquals(i - 5, store.getItemNormalized(i));
212-
}
213-
214-
assertEquals(12, store.length());
215-
}
216-
217100
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,22 @@ def test_import():
5858

5959
def test_create():
6060
from array import array
61-
a = array('b', b'x'*10)
61+
a = array('b', b'x' * 10)
6262
assert str(a) == "array('b', [120, 120, 120, 120, 120, 120, 120, 120, 120, 120])"
63+
64+
65+
def test_add():
66+
from array import array
67+
a0 = array("b", b"hello")
68+
a1 = array("b", b"world")
69+
assert a0 + a1 == array("b", b"helloworld")
70+
71+
a0 = array("b", b"hello")
72+
a1 = array("l", b"abcdabcd")
73+
try:
74+
res = a0 + a1
75+
except TypeError:
76+
assert True
77+
else:
78+
assert False
79+

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

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,82 @@ def test_setitem():
183183
pass
184184

185185

186+
def test_setslice():
187+
# whole sequence
188+
b = bytearray(b"hello")
189+
b[0:5] = b"HELLO"
190+
assert b == bytearray(b"HELLO")
191+
192+
# whole same length as slice
193+
b = bytearray(b"hellohellohello")
194+
b[5:10] = b"HELLO"
195+
assert b == bytearray(b"helloHELLOhello")
196+
197+
# shrink
198+
b = bytearray(b"hellohellohello")
199+
b[5:10] = b"hi"
200+
assert b == bytearray(b"hellohihello")
201+
202+
# extend
203+
b = bytearray(b"hellohelloworld")
204+
b[5:10] = b"beautiful"
205+
assert b == bytearray(b"hellobeautifulworld")
206+
207+
# assign list with integers
208+
b = bytearray(b"hellohellohello")
209+
b[5:10] = [4, 5, 6, 7, 8]
210+
assert b == bytearray(b"hello\x04\x05\x06\x07\x08hello")
211+
212+
# assign range
213+
b = bytearray(b"hellohellohello")
214+
b[5:10] = range(5)
215+
assert b == bytearray(b'hello\x00\x01\x02\x03\x04hello')
216+
217+
b = bytearray(range(10))
218+
assert list(b) == list(range(10))
219+
220+
b[0:5] = bytearray([1, 1, 1, 1, 1])
221+
assert b == bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9])
222+
223+
# TODO: seq storage does not yet support deletion ...
224+
# del b[0:-5]
225+
# assert b == bytearray([5, 6, 7, 8, 9])
226+
b = bytearray([5, 6, 7, 8, 9])
227+
228+
b[0:0] = bytearray([0, 1, 2, 3, 4])
229+
assert b == bytearray(range(10))
230+
b = bytearray(range(10))
231+
232+
b[-7:-3] = bytearray([100, 101])
233+
assert b == bytearray([0, 1, 2, 100, 101, 7, 8, 9])
234+
235+
b[3:5] = [3, 4, 5, 6]
236+
assert b == bytearray(range(10))
237+
238+
b[3:0] = [42, 42, 42]
239+
assert b == bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])
240+
241+
b[3:] = b'foo'
242+
assert b == bytearray([0, 1, 2, 102, 111, 111])
243+
244+
b[:3] = memoryview(b'foo')
245+
assert b == bytearray([102, 111, 111, 102, 111, 111])
246+
247+
b[3:4] = []
248+
assert b == bytearray([102, 111, 111, 111, 111])
249+
250+
for elem in [5, -5, 0, int(10e20), 'str', 2.3,
251+
['a', 'b'], [b'a', b'b'], [[]]]:
252+
def assign():
253+
b[3:4] = elem
254+
assert_raises(TypeError, assign)
255+
256+
for elem in [[254, 255, 256], [-256, 9000]]:
257+
def assign():
258+
b[3:4] = elem
259+
assert_raises(ValueError, assign)
260+
261+
186262
def test_delitem():
187263
b = bytearray(range(10))
188264
del b[0]
@@ -263,53 +339,6 @@ def test_join():
263339
assert b"--".join([b"hello"]) == b"hello"
264340

265341

266-
def test_setslice():
267-
b = bytearray(range(10))
268-
assert list(b) == list(range(10))
269-
270-
b[0:5] = bytearray([1, 1, 1, 1, 1])
271-
assert b == bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9])
272-
273-
# TODO: seq storage does not yet support deletion ...
274-
# del b[0:-5]
275-
# assert b == bytearray([5, 6, 7, 8, 9])
276-
b = bytearray([5, 6, 7, 8, 9])
277-
278-
# TODO: seq setSlice is broken ...
279-
# b[0:0] = bytearray([0, 1, 2, 3, 4])
280-
# assert b == bytearray(range(10))
281-
b = bytearray(range(10))
282-
283-
b[-7:-3] = bytearray([100, 101])
284-
assert b == bytearray([0, 1, 2, 100, 101, 7, 8, 9])
285-
286-
b[3:5] = [3, 4, 5, 6]
287-
assert b == bytearray(range(10))
288-
289-
b[3:0] = [42, 42, 42]
290-
assert b == bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])
291-
292-
b[3:] = b'foo'
293-
assert b == bytearray([0, 1, 2, 102, 111, 111])
294-
295-
b[:3] = memoryview(b'foo')
296-
assert b == bytearray([102, 111, 111, 102, 111, 111])
297-
298-
b[3:4] = []
299-
assert b == bytearray([102, 111, 111, 111, 111])
300-
301-
for elem in [5, -5, 0, int(10e20), 'str', 2.3,
302-
['a', 'b'], [b'a', b'b'], [[]]]:
303-
def assign():
304-
b[3:4] = elem
305-
assert_raises(TypeError, assign)
306-
307-
for elem in [[254, 255, 256], [-256, 9000]]:
308-
def assign():
309-
b[3:4] = elem
310-
assert_raises(ValueError, assign)
311-
312-
313342
def test_concat():
314343
a = b'0'
315344
b = b'1'

0 commit comments

Comments
 (0)