Skip to content

Commit bf2c5ef

Browse files
committed
Cleaning up obsolete methods 'clear', 'setSliceInBound', and 'delSlice'.
1 parent 70c69ee commit bf2c5ef

23 files changed

+67
-770
lines changed

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

Lines changed: 0 additions & 62 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());
@@ -126,31 +89,6 @@ public void intGetSlice() {
12689
}
12790
}
12891

129-
@Test
130-
public void intSetSliceOutOfBound() throws SequenceStoreException {
131-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
132-
IntSequenceStorage slice = new IntSequenceStorage(new int[]{42, 42, 42});
133-
134-
store.setSliceInBound(5, 8, 1, slice);
135-
136-
for (int i = 5; i < 8; i++) {
137-
assertEquals(42, store.getItemNormalized(i));
138-
}
139-
}
140-
141-
@Test
142-
public void intDel() {
143-
IntSequenceStorage store = new IntSequenceStorage(getIntValues());
144-
store.delItemInBound(4);
145-
146-
for (int i = 0; i < 4; i++) {
147-
assertEquals(i + 1, store.getItemNormalized(i));
148-
}
149-
150-
assertEquals(6, store.getItemNormalized(4));
151-
assertEquals(5, store.length());
152-
}
153-
15492
@Test
15593
public void intInsert() throws SequenceStoreException {
15694
IntSequenceStorage store = new IntSequenceStorage(getIntValues());

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,27 @@ def test_del_item(self):
198198
self.assertEqual([2, 3], l)
199199
l.__delitem__(-1)
200200
self.assertEqual([2], l)
201+
201202
l = [1, 2, 3]
202203
del(l[1])
203204
self.assertEqual([1, 3], l)
204205
del(l[False])
205206
self.assertEqual([3], l)
207+
206208
l = [1.1, 2.1, 3.1]
207209
l.__delitem__(0)
208210
self.assertEqual([2.1, 3.1], l)
209211
l.__delitem__(-1)
210212
self.assertEqual([2.1], l)
213+
211214
l = [1.1, 2.1, 3.1]
212215
del(l[1])
213216
self.assertEqual([1.1, 3.1], l)
214217

218+
l = ["1", "2", "3", "4", "5", "6"]
219+
del l[4]
220+
self.assertEqual(["1", "2", "3", "4", "6"], l)
221+
215222
def test_del_border(self):
216223
l = [1, 2, 3]
217224
self.assertRaises(IndexError, l.__delitem__, 3)
@@ -332,6 +339,18 @@ def test_set_slice(self):
332339
a[1:4:1] = [42, 42, 42]
333340
self.assertEqual([1, 42, 42, 42, 5, 6], a)
334341

342+
a = [1, 2, 3, 4, 5, 6]
343+
a[5:8:1] = [42, 42, 42]
344+
self.assertEqual([1, 2, 3, 4, 5, 42, 42, 42], a)
345+
346+
a = ["1", "2", "3", "4", "5", "6"]
347+
a[1:4:1] = ["42", "42", "42"]
348+
self.assertEqual(['1', '42', '42', '42', '5', '6'], a)
349+
350+
a = ["1", "2", "3", "4", "5", "6"]
351+
a[5:8:1] = ["42", "42", "42"]
352+
self.assertEqual(["1", "2", "3", "4", "5", '42', '42', '42'], a)
353+
335354
def test_set_slice_class_iter(self):
336355

337356
class MyIter():

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/ByteArrayBuiltins.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
4747
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4848
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
49+
import static com.oracle.graal.python.runtime.sequence.SequenceUtil.MISSING_INDEX;
4950

5051
import java.util.List;
5152

@@ -391,11 +392,12 @@ public PNone reverse(PByteArray byteArray) {
391392
// bytearray.clear()
392393
@Builtin(name = "clear", fixedNumOfPositionalArgs = 1)
393394
@GenerateNodeFactory
394-
public abstract static class ByteArrayClearNode extends PythonBuiltinNode {
395+
public abstract static class ByteArrayClearNode extends PythonUnaryBuiltinNode {
395396

396397
@Specialization
397-
public PNone clear(PByteArray byteArray) {
398-
byteArray.clear();
398+
public PNone clear(PByteArray byteArray,
399+
@Cached("create()") SequenceStorageNodes.DeleteNode deleteNode) {
400+
deleteNode.execute(byteArray.getSequenceStorage(), factory().createSlice(MISSING_INDEX, MISSING_INDEX, 1));
399401
return PNone.NONE;
400402
}
401403
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/PByteArray.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@
2727

2828
import java.util.Arrays;
2929

30-
import com.oracle.graal.python.builtins.objects.slice.PSlice;
3130
import com.oracle.graal.python.builtins.objects.type.PythonClass;
3231
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
3332
import com.oracle.graal.python.runtime.sequence.PSequence;
34-
import com.oracle.graal.python.runtime.sequence.SequenceUtil;
3533
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
3634
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
3735
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
@@ -105,21 +103,10 @@ public final void reverse() {
105103
store.reverse();
106104
}
107105

108-
public final void clear() {
109-
store.clear();
110-
}
111-
112106
public PByteArray copy() {
113107
return new PByteArray(this.getPythonClass(), store.copy());
114108
}
115109

116-
public final void delSlice(PSlice slice) {
117-
int start = Math.max(0, SequenceUtil.normalizeSliceStart(slice, store.length()));
118-
final int stop = Math.min(store.length(), SequenceUtil.normalizeSliceStop(slice, store.length()));
119-
final int step = SequenceUtil.normalizeSliceStep(slice);
120-
store.delSlice(start, stop, step);
121-
}
122-
123110
@Override
124111
public PIBytesLike createFromBytes(PythonObjectFactory factory, byte[] bytes) {
125112
return factory.createByteArray(bytes);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,11 +809,21 @@ abstract static class SetItemScalarNode extends SequenceStorageBaseNode {
809809

810810
public abstract void execute(SequenceStorage s, int idx, Object value);
811811

812+
@Specialization
813+
protected void doBoolean(BoolSequenceStorage storage, int idx, boolean value) {
814+
storage.setBoolItemNormalized(idx, value);
815+
}
816+
812817
@Specialization
813818
protected void doByte(ByteSequenceStorage storage, int idx, Object value) {
814819
storage.setByteItemNormalized(idx, getCastToByteNode().execute(value));
815820
}
816821

822+
@Specialization
823+
protected void doChar(CharSequenceStorage storage, int idx, char value) {
824+
storage.setCharItemNormalized(idx, value);
825+
}
826+
817827
@Specialization
818828
protected void doInt(IntSequenceStorage storage, int idx, int value) {
819829
storage.setIntItemNormalized(idx, value);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -479,111 +479,50 @@ protected boolean isNotSpecialCase(PList list, Object value) {
479479
@GenerateNodeFactory
480480
public abstract static class ListPopNode extends PythonBuiltinNode {
481481

482+
private static final String POP_INDEX_OUT_OF_RANGE = "pop index out of range";
483+
482484
@Child private SequenceStorageNodes.GetItemNode getItemNode;
483485

484486
@CompilationFinal private ValueProfile storeProfile;
485487

486-
@Specialization(guards = "isIntStorage(list)")
487-
public int popInt(PList list, @SuppressWarnings("unused") PNone none,
488-
@Cached("createBinaryProfile()") ConditionProfile isEmpty) {
489-
raiseIndexError(isEmpty.profile(list.len() == 0));
490-
IntSequenceStorage store = (IntSequenceStorage) list.getSequenceStorage();
491-
return store.popInt();
492-
}
493-
494-
@Specialization(guards = "isLongStorage(list)")
495-
public long popLong(PList list, @SuppressWarnings("unused") PNone none,
496-
@Cached("createBinaryProfile()") ConditionProfile isEmpty) {
497-
raiseIndexError(isEmpty.profile(list.len() == 0));
498-
LongSequenceStorage store = (LongSequenceStorage) list.getSequenceStorage();
499-
return store.popLong();
500-
}
501-
502-
@Specialization(guards = "isDoubleStorage(list)")
503-
public double popDouble(PList list, @SuppressWarnings("unused") PNone none,
504-
@Cached("createBinaryProfile()") ConditionProfile isEmpty) {
505-
raiseIndexError(isEmpty.profile(list.len() == 0));
506-
DoubleSequenceStorage store = (DoubleSequenceStorage) list.getSequenceStorage();
507-
return store.popDouble();
508-
}
509-
510-
@Specialization(guards = "isObjectStorage(list)")
511-
public Object popObject(PList list, @SuppressWarnings("unused") PNone none,
512-
@Cached("createBinaryProfile()") ConditionProfile isEmpty) {
513-
raiseIndexError(isEmpty.profile(list.len() == 0));
514-
ObjectSequenceStorage store = (ObjectSequenceStorage) list.getSequenceStorage();
515-
return store.popObject();
516-
}
517-
518488
@Specialization
519-
public Object popLast(PList list, @SuppressWarnings("unused") PNone none) {
520-
SequenceStorage store = getStoreProfile().profile(list.getSequenceStorage());
521-
int len = store.length();
489+
public Object popLast(PList list, @SuppressWarnings("unused") PNone none,
490+
@Cached("createDelete()") SequenceStorageNodes.DeleteNode deleteNode) {
491+
SequenceStorage store = list.getSequenceStorage();
522492
Object ret = getGetItemNode().execute(store, -1);
523-
store.delItemInBound(len - 1);
493+
deleteNode.execute(store, -1);
524494
return ret;
525495
}
526496

527-
@Specialization
528-
public Object pop(PList list, boolean bindex,
529-
@Cached("createBinaryProfile()") ConditionProfile isOutOfRange) {
530-
int index = bindex ? 1 : 0;
531-
return popOnIndex(list.getSequenceStorage(), index, isOutOfRange);
532-
}
533-
534-
@Specialization
535-
public Object pop(PList list, int index,
536-
@Cached("createBinaryProfile()") ConditionProfile isOutOfRange) {
537-
return popOnIndex(list.getSequenceStorage(), index, isOutOfRange);
538-
}
539-
540-
@Specialization
541-
@SuppressWarnings("unused")
542-
public Object pop(PList list, long arg) {
543-
raiseIndexError(true);
544-
return null;
545-
}
546-
547-
@Specialization
548-
@SuppressWarnings("unused")
549-
public Object pop(PList list, Object arg) {
550-
throw raise(TypeError, "integer argument expected, got %p", arg);
551-
}
552-
553-
protected void raiseIndexError(boolean con) {
554-
if (con) {
555-
throw raise(PythonErrorType.IndexError, "pop index out of range");
556-
}
557-
}
558-
559-
private Object popOnIndex(SequenceStorage store, int index, ConditionProfile cp) {
560-
561-
SequenceStorage profiled = getStoreProfile().profile(store);
562-
int len = profiled.length();
563-
if (cp.profile((index < 0 && (index + len) < 0) || index >= len)) {
564-
throw raise(PythonErrorType.IndexError, "pop index out of range");
565-
}
566-
Object ret = getGetItemNode().execute(profiled, index);
567-
// this is safe because index is already verified by 'GetItemNode'
568-
profiled.delItemInBound(index);
497+
@Specialization(guards = {"!isNoValue(idx)", "!isPSlice(idx)"})
498+
public Object doIndex(PList list, Object idx,
499+
@Cached("createDelete()") SequenceStorageNodes.DeleteNode deleteNode) {
500+
SequenceStorage store = list.getSequenceStorage();
501+
Object ret = getGetItemNode().execute(store, idx);
502+
deleteNode.execute(store, idx);
569503
return ret;
570504
}
571505

572-
private ValueProfile getStoreProfile() {
573-
if (storeProfile == null) {
574-
CompilerDirectives.transferToInterpreterAndInvalidate();
575-
storeProfile = ValueProfile.createClassProfile();
576-
}
577-
return storeProfile;
506+
@Fallback
507+
public Object doError(@SuppressWarnings("unused") Object list, Object arg) {
508+
throw raise(TypeError, "'%p' object cannot be interpreted as an integer", arg);
578509
}
579510

580511
private SequenceStorageNodes.GetItemNode getGetItemNode() {
581512
if (getItemNode == null) {
582513
CompilerDirectives.transferToInterpreterAndInvalidate();
583-
getItemNode = insert(SequenceStorageNodes.GetItemNode.create(NormalizeIndexNode.create("pop index out of range")));
514+
getItemNode = insert(SequenceStorageNodes.GetItemNode.create(createNormalize()));
584515
}
585516
return getItemNode;
586517
}
518+
519+
protected static SequenceStorageNodes.DeleteNode createDelete() {
520+
return SequenceStorageNodes.DeleteNode.create(createNormalize());
521+
}
522+
523+
private static NormalizeIndexNode createNormalize() {
524+
return NormalizeIndexNode.create(POP_INDEX_OUT_OF_RANGE);
525+
}
587526
}
588527

589528
// list.index(x)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/PList.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.list;
2727

28-
import com.oracle.graal.python.builtins.objects.slice.PSlice;
2928
import com.oracle.graal.python.builtins.objects.type.PythonClass;
3029
import com.oracle.graal.python.runtime.sequence.PSequence;
3130
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
@@ -51,15 +50,6 @@ public final void setSequenceStorage(SequenceStorage newStorage) {
5150
this.store = newStorage;
5251
}
5352

54-
public final void delSlice(PSlice slice) {
55-
PSlice.SliceInfo sliceInfo = slice.computeActualIndices(this.len());
56-
store.delSlice(sliceInfo.start, sliceInfo.stop, sliceInfo.step);
57-
}
58-
59-
public final void clear() {
60-
store.delSlice(0, store.length(), 1);
61-
}
62-
6353
@Override
6454
public final String toString() {
6555
StringBuilder buf = new StringBuilder("[");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/slice/PSlice.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,4 @@ public SliceInfo computeActualIndices(int len) {
153153
}
154154
return new SliceInfo(newStart, newStop, newStep, newLen);
155155
}
156-
157-
/**
158-
* Make step a long in case adding the start, stop and step together overflows an int.
159-
*/
160-
public static final int sliceLength(int start, int stop, long step) {
161-
int ret;
162-
if (step > 0) {
163-
ret = (int) ((stop - start + step - 1) / step);
164-
} else {
165-
ret = (int) ((stop - start + step + 1) / step);
166-
}
167-
168-
if (ret < 0) {
169-
return 0;
170-
}
171-
172-
return ret;
173-
}
174156
}

0 commit comments

Comments
 (0)