Skip to content

Commit

Permalink
Added implementation of set constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Nov 30, 2015
1 parent 0f4774f commit 3d1acd6
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 178 deletions.
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ What it does:

It *doesn't* currently support:

* Dict and Set constants

* Importing and using native Java APIs

* Extending native java classes and interfaces.
Expand Down
13 changes: 11 additions & 2 deletions python/common/org/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -1359,8 +1359,17 @@ public static org.python.types.Set set(
if (args == null || args.size() == 0) {
return new org.python.types.Set();
} else if (args.size() == 1) {
// return new org.python.types.Set(args.get(0));
throw new org.python.exceptions.NotImplementedError("Builtin function 'set' with iterator not implemented");
try {
// If the object is iterable, the underlying value should be
// a Java Collection.
return new org.python.types.Set(
new java.util.HashSet<org.python.Object>(
(java.util.Collection) args.get(0).toValue()
)
);
} catch (java.lang.ClassCastException e) {
throw new org.python.exceptions.TypeError("'" + org.Python.typeName(args.get(0).getClass()) + "' object is not iterable");
}
} else {
throw new org.python.exceptions.TypeError("set() expected at most 1 arguments ( got " + args.size() + ")");
}
Expand Down
25 changes: 21 additions & 4 deletions python/common/org/python/types/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public Set(java.util.Set<org.python.Object> set) {
// }

public org.python.types.Str __repr__() {
// Representation of an empty set is different
if (this.value.size() == 0) {
return new org.python.types.Str("set()");
}

java.lang.StringBuilder buffer = new java.lang.StringBuilder("{");
boolean first = true;
for (org.python.Object obj: this.value) {
Expand Down Expand Up @@ -106,8 +111,12 @@ public org.python.Iterable __iter__() {
throw new org.python.exceptions.NotImplementedError("set.__iter__() has not been implemented");
}

public org.python.Object __contains__() {
throw new org.python.exceptions.NotImplementedError("set.__contains__() has not been implemented");
public org.python.Object __contains__(org.python.Object item) {
return new org.python.types.Bool(this.value.contains(item));
}

public org.python.Object __not_contains__(org.python.Object item) {
return new org.python.types.Bool(!this.value.contains(item));
}

public org.python.Object __sub__() {
Expand Down Expand Up @@ -163,11 +172,19 @@ public org.python.Object add(java.util.List<org.python.Object> args, java.util.M
}

public org.python.Object clear(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
throw new org.python.exceptions.NotImplementedError("set.clear() has not been implemented.");
if (kwargs != null || kwargs.size() > 0) {
throw new org.python.exceptions.TypeError("clear() takes no keyword arguments");
}
if (args != null || args.size() > 0) {
throw new org.python.exceptions.TypeError("clear() takes no arguments (" + args.size() + " given)");
}
this.clear();
return org.python.types.NoneType.NONE;
}

public org.python.Object clear() {
throw new org.python.exceptions.NotImplementedError("set.clear() has not been implemented.");
this.value.clear();
return org.python.types.NoneType.NONE;
}

public org.python.Object copy(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs) {
Expand Down
12 changes: 0 additions & 12 deletions tests/datatypes/test_NoneType.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,7 @@ class BinaryNoneTypeOperationTests(BinaryOperationTestCase, TranspileTestCase):
'test_gt_list',
'test_ge_list',

'test_add_set',
'test_subtract_set',
'test_multiply_set',
'test_floor_divide_set',
'test_true_divide_set',
'test_modulo_set',
'test_power_set',
'test_subscr_set',
'test_lshift_set',
'test_rshift_set',
'test_and_set',
'test_xor_set',
'test_or_set',
'test_lt_set',
'test_le_set',
'test_eq_set',
Expand Down
12 changes: 0 additions & 12 deletions tests/datatypes/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,7 @@ class BinaryDictOperationTests(BinaryOperationTestCase, TranspileTestCase):
'test_gt_list',
'test_ge_list',

'test_add_set',
'test_subtract_set',
'test_multiply_set',
'test_floor_divide_set',
'test_true_divide_set',
'test_modulo_set',
'test_power_set',
'test_subscr_set',
'test_lshift_set',
'test_rshift_set',
'test_and_set',
'test_xor_set',
'test_or_set',
'test_lt_set',
'test_le_set',
'test_eq_set',
Expand Down
7 changes: 0 additions & 7 deletions tests/datatypes/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,9 @@ class BinaryFloatOperationTests(BinaryOperationTestCase, TranspileTestCase):
'test_subtract_set',
'test_multiply_set',
'test_floor_divide_set',
'test_true_divide_set',
'test_modulo_set',
'test_power_set',
'test_subscr_set',
'test_lshift_set',
'test_rshift_set',
'test_and_set',
'test_xor_set',
'test_or_set',
'test_lt_set',
'test_le_set',
'test_eq_set',
'test_ne_set',
Expand Down
11 changes: 0 additions & 11 deletions tests/datatypes/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,14 @@ class BinaryIntOperationTests(BinaryOperationTestCase, TranspileTestCase):
'test_eq_list',
'test_ne_list',

'test_add_set',
'test_subtract_set',
'test_multiply_set',
'test_floor_divide_set',
'test_true_divide_set',
'test_modulo_set',
'test_power_set',
'test_subscr_set',
'test_lshift_set',
'test_rshift_set',
'test_and_set',
'test_xor_set',
'test_or_set',
'test_lt_set',
'test_le_set',
'test_eq_set',
'test_ne_set',
'test_gt_set',
'test_ge_set',

'test_subtract_str',
'test_subscr_str',
Expand Down
12 changes: 0 additions & 12 deletions tests/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,6 @@ class BinaryListOperationTests(BinaryOperationTestCase, TranspileTestCase):
'test_ge_list',

'test_add_set',
'test_subtract_set',
'test_multiply_set',
'test_floor_divide_set',
'test_true_divide_set',
'test_modulo_set',
'test_power_set',
'test_subscr_set',
'test_lshift_set',
'test_rshift_set',
'test_and_set',
'test_xor_set',
'test_or_set',
'test_lt_set',
'test_le_set',
'test_eq_set',
Expand Down
Loading

0 comments on commit 3d1acd6

Please sign in to comment.