Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cast_from_pyobject to throw on unsupported types rather than returning null #451

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions python/mrc/_pymrc/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2021-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-FileCopyrightText: Copyright (c) 2021-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,6 +17,8 @@

#include "pymrc/utils.hpp"

#include "pymrc/utilities/acquire_gil.hpp"

#include <nlohmann/json.hpp>
#include <pybind11/cast.h>
#include <pybind11/detail/internals.h>
Expand Down Expand Up @@ -170,8 +172,11 @@ json cast_from_pyobject(const py::object& source)
return json(py::cast<std::string>(source));
}

// else unsupported return null
return json();
// else unsupported return throw a type error
{
AcquireGIL gil;
throw py::type_error("Object is not JSON serializable");
}
// NOLINTEND(modernize-return-braced-init-list)
}

Expand Down
22 changes: 21 additions & 1 deletion python/mrc/_pymrc/tests/test_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2021-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-FileCopyrightText: Copyright (c) 2021-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -34,6 +34,7 @@
#include <climits>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -143,6 +144,25 @@ TEST_F(TestUtils, CastFromPyObject)
}
}

TEST_F(TestUtils, CastFromPyObjectSerializeErrors)
{
// Test to verify that cast_from_pyobject throws a python TypeError when encountering something that is not json
// serializable issue #450
{
// Exceptions are not serializable. Keep in mind we aren't throwins a `std::runtime_error`, we are just trying
// to serialize it
py::object o = py::cast(std::runtime_error("test error"));
EXPECT_THROW(pymrc::cast_from_pyobject(o), py::type_error);
}

{
// decimal.Decimal is not serializable
py::object Decimal = py::module_::import("decimal").attr("Decimal");
py::object o = Decimal("1.0");
EXPECT_THROW(pymrc::cast_from_pyobject(o), py::type_error);
}
}

TEST_F(TestUtils, PyObjectWrapper)
{
py::list test_list;
Expand Down
Loading