Skip to content

Commit

Permalink
fixes intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
deep110 committed Apr 9, 2017
1 parent b511beb commit 066ad67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 9 additions & 10 deletions python/common/org/python/types/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public org.python.Object __mul__(org.python.Object other) {
// }

@org.python.Method(
__doc__ = ""
__doc__ = ""
)
public org.python.Object __and__(org.python.Object other) {
java.util.Set set = ((org.python.types.Set) this.copy()).value;
Expand Down Expand Up @@ -424,9 +424,12 @@ public org.python.Object __iadd__(org.python.Object other) {
args = {"other"}
)
public org.python.Object intersection(org.python.Object other) {
java.util.Set set = ((Set) this.copy()).value;
set.retainAll(((Set) other).value);
return new Set(set);
try {
org.python.types.Set otherSet = new org.python.types.Set(new org.python.Object[] {other}, null);
return this.__and__(otherSet);
} catch (org.python.exceptions.AttributeError e) {
throw new org.python.exceptions.TypeError("'" + other.typeName() + "' object is not iterable");
}
}

@org.python.Method(
Expand All @@ -451,9 +454,7 @@ public org.python.Object isdisjoint(org.python.Object other) {
)
public org.python.Object issubset(org.python.Object other) {
try {
org.python.Object[] arrObject = new org.python.Object[1];
arrObject[0] = other;
org.python.types.Set otherSet = new org.python.types.Set(arrObject, null);
org.python.types.Set otherSet = new org.python.types.Set(new org.python.Object[] {other}, null);
return this.__le__(otherSet);
} catch (org.python.exceptions.AttributeError e) {
throw new org.python.exceptions.TypeError("'" + other.typeName() + "' object is not iterable");
Expand All @@ -466,9 +467,7 @@ public org.python.Object issubset(org.python.Object other) {
)
public org.python.Object issuperset(org.python.Object other) {
try {
org.python.Object[] arrObject = new org.python.Object[1];
arrObject[0] = other;
org.python.types.Set otherSet = new org.python.types.Set(arrObject, null);
org.python.types.Set otherSet = new org.python.types.Set(new org.python.Object[] {other}, null);
return this.__ge__(otherSet);
} catch (org.python.exceptions.AttributeError e) {
throw new org.python.exceptions.TypeError("'" + other.typeName() + "' object is not iterable");
Expand Down
15 changes: 13 additions & 2 deletions tests/datatypes/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ def test_intersection(self):
x = {1, 2, 3}
y = {3, 4, 5}
z = x.intersection(y)
w = x.intersection([3,4,5])
print(x)
print(y)
print(z)
print(w)
""")

self.assertCodeExecution("""
Expand All @@ -128,6 +130,15 @@ def test_intersection(self):
print(z)
""")

# not iterable test
self.assertCodeExecution("""
x = set([1, 2, 3])
try:
print(x.intersection(1))
except TypeError as err:
print(err)
""")

def test_remove(self):
self.assertCodeExecution("""
x = {1, 2, 3}
Expand Down Expand Up @@ -188,7 +199,7 @@ def test_issubset(self):
self.assertCodeExecution("""
a = set('abc')
b = set('abcde')
print(a.issubset(b)
print(a.issubset(b))
print(a.issubset('ab'))
""")

Expand All @@ -205,7 +216,7 @@ def test_issuperset(self):
self.assertCodeExecution("""
a = set('abcd')
b = set('ab')
print(a.issuperset(b)
print(a.issuperset(b))
print(a.issuperset('ab1'))
""")

Expand Down

0 comments on commit 066ad67

Please sign in to comment.