diff --git a/test/cpp/test_status_common.h b/test/cpp/test_status_common.h index 17b0ef29f5f..e09d2e58d28 100644 --- a/test/cpp/test_status_common.h +++ b/test/cpp/test_status_common.h @@ -80,8 +80,6 @@ class StatusTest : public testing::TestWithParam { namespace cpp_test { // Prefix of the C++ stacktrace PyTorch adds to the error message. -constexpr inline char kTorchCppStacktracePrefixDeprecated[] = - "Exception raised from OkOrThrow at torch_xla/csrc/status.cpp:"; constexpr inline char kTorchCppStacktracePrefix[] = "Exception raised from ThrowStatusError at torch_xla/csrc/status.cpp:"; @@ -102,50 +100,6 @@ inline std::string GetStatusPropagationTrace(const absl::Status& status) { : ""; } -TEST_P(StatusTest, OkOrThrowWithOkStatus) { - absl::Status ok_status = absl::OkStatus(); - EXPECT_NO_THROW(OkOrThrow(ok_status)); -} - -TEST_P(StatusTest, OkOrThrowWithErrorStatus) { - try { - absl::Status error_status = absl::InvalidArgumentError(kMessage); - OkOrThrow(error_status); - } catch (const c10::Error& error) { - if (IsShowCppStacktracesMode()) { - EXPECT_THAT(std::string_view(error.what()), - ::testing::StartsWith(absl::StrCat( - kMessage, "\n\n", kTorchCppStacktracePrefixDeprecated))); - } else { - EXPECT_EQ(std::string_view(error.what_without_backtrace()), - std::string_view(kMessage)); - } - } -} - -TEST_P(StatusTest, GetValueOrThrowWithOkStatusOr) { - int value = 42; - absl::StatusOr status_or = value; - int result = GetValueOrThrow(std::move(status_or)); - EXPECT_EQ(result, value); -} - -TEST_P(StatusTest, GetValueOrThrowWithErrorStatusOr) { - try { - absl::StatusOr error_status = absl::InvalidArgumentError(kMessage); - int value = GetValueOrThrow(error_status); - } catch (const c10::Error& error) { - if (IsShowCppStacktracesMode()) { - EXPECT_THAT(std::string_view(error.what()), - ::testing::StartsWith(absl::StrCat( - kMessage, "\n\n", kTorchCppStacktracePrefixDeprecated))); - } else { - EXPECT_EQ(std::string_view(error.what_without_backtrace()), - std::string_view(kMessage)); - } - } -} - TEST_P(StatusTest, MaybeWithLocationPropagatesErrorStatus) { absl::Status error_status = absl::InvalidArgumentError(kMessage); absl::Status result = @@ -345,61 +299,6 @@ TEST_P(StatusTest, MacroErrorWithLocation) { } } -TEST_P(StatusTest, OkOrThrowWithErrorPropagationWithNewMessage) { - int32_t errline0 = __LINE__ + 2; - auto innerfn = [&]() -> absl::Status { - return XLA_ERROR_WITH_LOCATION(absl::InvalidArgumentError(kMessage)); - }; - - int32_t errline1 = __LINE__ + 2; - auto midfn = [&]() -> absl::Status { - XLA_RETURN_IF_ERROR(innerfn(), kNewMessage); - return absl::OkStatus(); - }; - - int32_t errline2 = __LINE__ + 2; - auto outerfn = [&]() -> absl::Status { - XLA_RETURN_IF_ERROR(midfn()); - return absl::OkStatus(); - }; - - try { - OkOrThrow(outerfn()); - } catch (const c10::Error& error) { - if (IsShowCppStacktracesMode()) { - // Expected Error Message Prefix - // ============================= - // - // New test error kMessage - // - // Status Propagation Stacktrace: - // From: ./test/cpp/test_status_common.h:329 (error: Test error - // kMessage) From: ./test/cpp/test_status_common.h:335 (error: New - // test error kMessage) From: ./test/cpp/test_status_common.h:342 - // - // C++ Stacktrace: - // - std::ostringstream oss; - oss << kNewMessage; - oss << "\n\n"; - oss << "Status Propagation Trace:"; - oss << kEntryPrefix << "From: operator() at " << __FILE__ << ":" - << errline0 << " (error: " << kMessage << ")"; - oss << kEntryPrefix << "From: operator() at " << __FILE__ << ":" - << errline1 << " (error: " << kNewMessage << ")"; - oss << kEntryPrefix << "From: operator() at " << __FILE__ << ":" - << errline2; - oss << "\n\n"; - oss << kTorchCppStacktracePrefixDeprecated; - EXPECT_THAT(std::string_view(error.what()), - ::testing::StartsWith(oss.str())); - } else { - EXPECT_EQ(std::string_view(error.what_without_backtrace()), - std::string_view(kNewMessage)); - } - } -} - TEST_P(StatusTest, MacroThrowIfErrorWithErrorPropagationWithNewMessage) { int32_t errline0 = __LINE__ + 2; auto innerfn = [&]() -> absl::Status { diff --git a/torch_xla/csrc/status.cpp b/torch_xla/csrc/status.cpp index 56636874f0b..44f732fd6e9 100644 --- a/torch_xla/csrc/status.cpp +++ b/torch_xla/csrc/status.cpp @@ -129,13 +129,6 @@ void status_internal::ThrowStatusError(const absl::Status& status, LineBreakIfCppStacktracesEnabled())); } -void OkOrThrow(const absl::Status& status) { - TORCH_CHECK(status.ok(), absl::StrCat(BuildStatusErrorMessage(status), - LineBreakIfCppStacktracesEnabled())); -} - -void GetValueOrThrow(const absl::Status& status) { OkOrThrow(status); } - void status_internal::OkOrDie(const absl::Status& status, const char* file, const int32_t line, const char* function, std::string_view message) { diff --git a/torch_xla/csrc/status.h b/torch_xla/csrc/status.h index 87a9227672b..bbf3bad1ed5 100644 --- a/torch_xla/csrc/status.h +++ b/torch_xla/csrc/status.h @@ -297,35 +297,6 @@ void OkOrDie(const absl::Status& status, const char* file, const int32_t line, // It doesn't add a trailing line break. std::string BuildStatusErrorMessage(const absl::Status& status); -// Throws an exception if `status` has a non-ok code. -// -// Ideally, this function should be used only used in the project's -// boundary, e.g. when we need to throw an exception for the user to see. -void OkOrThrow(const absl::Status& status); - -// Either returns the value `status` holds, if it's an ok-status, or throw an -// exception from its error status. -template -T& GetValueOrThrow(absl::StatusOr& status) { - OkOrThrow(status.status()); - return status.value(); -} - -template -const T& GetValueOrThrow(const absl::StatusOr& status) { - OkOrThrow(status.status()); - return status.value(); -} - -template -T GetValueOrThrow(absl::StatusOr&& status) { - OkOrThrow(status.status()); - return std::move(status).value(); -} - -// `GetValueOrThrow` overload for `Status`. -void GetValueOrThrow(const absl::Status& status); - } // namespace torch_xla #endif // XLA_TORCH_XLA_CSRC_STATUS_H_