From 7ccf3d80e70dd1284868b4ce255a2527e22b84a9 Mon Sep 17 00:00:00 2001 From: William Meehan Date: Thu, 28 Apr 2022 06:48:18 -0700 Subject: [PATCH] Add set.pop() to strict analyzer Summary: This method was missing from the strict analyzer, causing STRICTSTATIC linker errors in D35905142. Reviewed By: sinancepel Differential Revision: D35942257 fbshipit-source-id: bd74eab286239b71ff7782bafbdd821cc1d19e07 --- StrictModules/Objects/iterable_objects.cpp | 14 ++++++++++++++ StrictModules/Objects/iterable_objects.h | 4 ++++ .../Tests/comparison_tests/interpreter_test.txt | 4 +++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/StrictModules/Objects/iterable_objects.cpp b/StrictModules/Objects/iterable_objects.cpp index 41cf23101af..99c367c7420 100644 --- a/StrictModules/Objects/iterable_objects.cpp +++ b/StrictModules/Objects/iterable_objects.cpp @@ -1002,6 +1002,19 @@ std::shared_ptr StrictSet::set__init__( return NoneObject(); } +std::shared_ptr StrictSet::setPop( + std::shared_ptr self, + const CallerContext& caller) { + checkExternalModification(self, caller); + if(self->data_.empty()) { + caller.raiseExceptionStr(KeyErrorType(), "pop from an empty set"); + } + auto iter = self->data_.begin(); + auto result = std::move(*iter); + self->data_.erase(iter); + return result; +} + std::shared_ptr StrictSet::setUpdate( std::shared_ptr self, const CallerContext& caller, @@ -1047,6 +1060,7 @@ void StrictSetType::addMethods() { StrictSetLikeType::addMethods(); addMethod("add", StrictSet::setAdd); addMethodDefault("__init__", StrictSet::set__init__, nullptr); + addMethod("pop", StrictSet::setPop); addMethod("update", StrictSet::setUpdate); addPyWrappedMethodObj<>( kDunderRepr, diff --git a/StrictModules/Objects/iterable_objects.h b/StrictModules/Objects/iterable_objects.h index 0e6d8172a34..c90a3fe5266 100644 --- a/StrictModules/Objects/iterable_objects.h +++ b/StrictModules/Objects/iterable_objects.h @@ -422,6 +422,10 @@ class StrictSet final : public StrictSetLike { const CallerContext& caller, std::shared_ptr arg = nullptr); + static std::shared_ptr setPop( + std::shared_ptr self, + const CallerContext& caller); + static std::shared_ptr setUpdate( std::shared_ptr self, const CallerContext& caller, diff --git a/StrictModules/Tests/comparison_tests/interpreter_test.txt b/StrictModules/Tests/comparison_tests/interpreter_test.txt index 6e85765b002..81491943776 100644 --- a/StrictModules/Tests/comparison_tests/interpreter_test.txt +++ b/StrictModules/Tests/comparison_tests/interpreter_test.txt @@ -3680,11 +3680,13 @@ s.update([5, 6]) l4 = len(s) s.update([5, 6]) l5 = len(s) +s.pop() # cannot guarantee the same element popped +l6 = len(s) b1 = set().issubset(s) b2 = set() <= s b3 = s <= set() --- -l1 l2 l3 l4 l5 b1 b2 b3 +l1 l2 l3 l4 l5 l6 b1 b2 b3 --- --- test_frozenset_init