From 066ad67bdb446d79177e42e15771ce4bd60b53dc Mon Sep 17 00:00:00 2001 From: deepankar Date: Thu, 30 Mar 2017 17:28:49 +0530 Subject: [PATCH] fixes intersection --- python/common/org/python/types/Set.java | 19 +++++++++---------- tests/datatypes/test_set.py | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/python/common/org/python/types/Set.java b/python/common/org/python/types/Set.java index f539495bc2..392eb5e74d 100644 --- a/python/common/org/python/types/Set.java +++ b/python/common/org/python/types/Set.java @@ -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; @@ -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( @@ -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"); @@ -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"); diff --git a/tests/datatypes/test_set.py b/tests/datatypes/test_set.py index 55f6ef0aa2..e9ff8ff6a6 100644 --- a/tests/datatypes/test_set.py +++ b/tests/datatypes/test_set.py @@ -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(""" @@ -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} @@ -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')) """) @@ -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')) """)