Skip to content

Commit

Permalink
Merge pull request beeware#744 from student-06/slice_fix
Browse files Browse the repository at this point in the history
This fixes all tests for slice datatype.
  • Loading branch information
eliasdorneles authored Feb 28, 2018
2 parents a2b24af + b1663e7 commit e2fc0d2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 41 deletions.
11 changes: 11 additions & 0 deletions python/common/org/python/types/Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -1122,4 +1122,15 @@ private static org.python.Object invokeComparison(org.python.Object x, org.pytho
args[0] = y;
return (org.python.Object) ((org.python.types.Method) comparator).invoke(args, null);
}

public static boolean isSequence(org.python.Object other) {
if (other instanceof org.python.types.ByteArray || other instanceof org.python.types.Bytes ||
other instanceof org.python.types.List || other instanceof org.python.types.Str ||
other instanceof org.python.types.Tuple) {

return true;
}
return false;
}

}
77 changes: 77 additions & 0 deletions python/common/org/python/types/Slice.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,81 @@ public org.python.Object __le__(org.python.Object other) {
return org.python.types.NotImplementedType.NOT_IMPLEMENTED;
}
}

@org.python.Method(
__doc__ = "",
args = {"index"}
)
public org.python.Object __getitem__(org.python.Object index) {
throw new org.python.exceptions.TypeError("'slice' object is not subscriptable");
}

@org.python.Method(
__doc__ = "",
args = {"index", "value"}
)
public void __setitem__(org.python.Object index, org.python.Object value) {
throw new org.python.exceptions.TypeError("'slice' object does not support item assignment");
}

@org.python.Method(
__doc__ = "",
args = {"index"}
)
public void __delitem__(org.python.Object index) {
throw new org.python.exceptions.TypeError("'slice' object does not support item deletion");
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __mul__(org.python.Object other) {
if (org.python.types.Object.isSequence(other)) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type 'slice'");
} else {
return super.__mul__(other);
}
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __imul__(org.python.Object other) {
if (org.python.types.Object.isSequence(other)) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type 'slice'");
} else {
return super.__imul__(other);
}
}


@org.python.Method(
__doc__ = ""
)
public org.python.Object __pos__() {
throw new org.python.exceptions.TypeError("bad operand type for unary +: 'slice'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __neg__() {
throw new org.python.exceptions.TypeError("bad operand type for unary -: 'slice'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __invert__() {
throw new org.python.exceptions.TypeError("bad operand type for unary ~: 'slice'");
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object __not__() {
return new org.python.types.Bool(false);
}
}
41 changes: 0 additions & 41 deletions tests/datatypes/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,51 +223,10 @@ def test_indices(self):
class UnarySliceOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'slice'

not_implemented = [
'test_unary_invert',
'test_unary_negative',
'test_unary_not',
'test_unary_positive',
]


class BinarySliceOperationTests(BinaryOperationTestCase, TranspileTestCase):
data_type = 'slice'

not_implemented = [
'test_multiply_bytearray',
'test_multiply_bytes',
'test_multiply_list',
'test_multiply_str',
'test_multiply_tuple',

'test_subscr_bool',
'test_subscr_bytearray',
'test_subscr_bytes',
'test_subscr_class',
'test_subscr_complex',
'test_subscr_dict',
'test_subscr_float',
'test_subscr_frozenset',
'test_subscr_int',
'test_subscr_list',
'test_subscr_None',
'test_subscr_NotImplemented',
'test_subscr_range',
'test_subscr_set',
'test_subscr_slice',
'test_subscr_str',
'test_subscr_tuple',
]


class InplaceSliceOperationTests(InplaceOperationTestCase, TranspileTestCase):
data_type = 'slice'

not_implemented = [
'test_multiply_bytearray',
'test_multiply_bytes',
'test_multiply_list',
'test_multiply_str',
'test_multiply_tuple',
]

0 comments on commit e2fc0d2

Please sign in to comment.