Skip to content

Commit

Permalink
REplace internal std::vector backup by a backup[] for std::set
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Oct 21, 2015
1 parent 3170103 commit bf8389b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/py2cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,15 @@ struct CppBuilder<std::set<T>>
if (PySet_Check(pyo))
{
long size { PySet_Size(pyo) };
std::vector<PyObject*> backup(size);
PyObject* backup[size];
value_type s;
for (long i { 0 } ; i != size ; ++i)
for (auto& elt : backup)
{
PyObject* popped { PySet_Pop(pyo) };
backup[i] = popped;
elt = popped;
s.insert(ToBuildable<T>()(popped));
}
for (PyObject* popped : backup)
for (auto& popped : backup)
{
PySet_Add(pyo, popped);
Py_DECREF(popped);
Expand Down
13 changes: 13 additions & 0 deletions test/test-py2cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,19 @@ TEST(CppBuilder_set, FromSet)
EXPECT_EQ(expected, CppBuilder<std::set<int>>()(pyo.get()));
EXPECT_FALSE(uncaught_exception());
}
TEST(CppBuilder_set, InputUnmodified)
{
PyRun_SimpleString("CppBuilder_set_InputUnmodified = set([1,8,3])");
std::unique_ptr<PyObject, decref> pyo { PyRun_String("CppBuilder_set_InputUnmodified", Py_eval_input, py_dict, NULL) };
std::set<int> expected { 1, 8, 3 };
ASSERT_NE(nullptr, pyo.get());
EXPECT_EQ(expected, CppBuilder<std::set<int>>()(pyo.get()));
EXPECT_FALSE(uncaught_exception());

std::unique_ptr<PyObject, decref> pyo_check { PyRun_String("CppBuilder_set_InputUnmodified == set([1,8,3])", Py_eval_input, py_dict, NULL) };
EXPECT_TRUE(CppBuilder<bool>()(pyo_check.get()));
EXPECT_FALSE(uncaught_exception());
}

/** map **/

Expand Down

0 comments on commit bf8389b

Please sign in to comment.