From 23e58ecbfc36749cf789b9ef5ad485df8f401a88 Mon Sep 17 00:00:00 2001 From: deepankar Date: Thu, 20 Apr 2017 03:18:38 +0530 Subject: [PATCH] implements issuperset --- python/common/org/python/types/FrozenSet.java | 15 +++++++++++ tests/datatypes/test_frozenset.py | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/python/common/org/python/types/FrozenSet.java b/python/common/org/python/types/FrozenSet.java index 9e26a92005..114e5ad61e 100644 --- a/python/common/org/python/types/FrozenSet.java +++ b/python/common/org/python/types/FrozenSet.java @@ -371,4 +371,19 @@ public org.python.Object issubset(org.python.Object other) { throw new org.python.exceptions.TypeError("'" + other.typeName() + "' object is not iterable"); } } + + @org.python.Method( + __doc__ = "Whether every element in other is in the set", + args = {"other"} + ) + public org.python.Object issuperset(org.python.Object other) { + try { + if (!(other instanceof org.python.types.Set || other instanceof org.python.types.FrozenSet)) { + other = iterToFrozenSet(other); + } + return this.__ge__(other); + } catch (org.python.exceptions.AttributeError e) { + throw new org.python.exceptions.TypeError("'" + other.typeName() + "' object is not iterable"); + } + } } diff --git a/tests/datatypes/test_frozenset.py b/tests/datatypes/test_frozenset.py index af8d1e44ca..0969d7c37f 100644 --- a/tests/datatypes/test_frozenset.py +++ b/tests/datatypes/test_frozenset.py @@ -191,6 +191,31 @@ def test_issubset(self): print(err) """) + def test_issuperset(self): + self.assertCodeExecution(""" + a = frozenset('abcd') + b = frozenset('ab') + c = set() + d = 'ab' + + print(a.issuperset(b)) + print(a.issuperset(a)) + print(a.issuperset(c)) + + # iterable test + print(a.issuperset(d)) + """) + + # not-iterable test + self.assertCodeExecution(""" + a = frozenset({1, 2, 3}) + b = 1 + try: + print(a.issuperset(b)) + except TypeError as err: + print(err) + """) + class UnaryFrozensetOperationTests(UnaryOperationTestCase, TranspileTestCase): data_type = 'frozenset'