mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
fix memory leak in property and staticmethod. (#301)
* fix memory leak in property and staticmethod. * some format.
This commit is contained in:
parent
ac8f4a1c2d
commit
9564013c1d
@ -585,24 +585,14 @@ class cpp_function : public function {
|
|||||||
class property : public object {
|
class property : public object {
|
||||||
PKBIND_TYPE_IMPL(object, property, tp_property);
|
PKBIND_TYPE_IMPL(object, property, tp_property);
|
||||||
|
|
||||||
property(handle getter, handle setter = none()) : object() {
|
property(handle getter, handle setter = none()) :
|
||||||
auto start = py_peek(0);
|
object(type::of<property>()(getter, setter)) {}
|
||||||
py_push(getter.ptr());
|
|
||||||
py_push(setter.ptr());
|
|
||||||
raise_call<py_tpcall>(type::of<property>().index(), 2, start);
|
|
||||||
*this = object::from_ret();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class staticmethod : public object {
|
class staticmethod : public object {
|
||||||
PKBIND_TYPE_IMPL(object, staticmethod, tp_staticmethod);
|
PKBIND_TYPE_IMPL(object, staticmethod, tp_staticmethod);
|
||||||
|
|
||||||
staticmethod(handle method) : object() {
|
staticmethod(handle method) : object(type::of<staticmethod>()(method)) {}
|
||||||
auto start = py_peek(0);
|
|
||||||
py_push(method.ptr());
|
|
||||||
raise_call<py_tpcall>(type::of<staticmethod>().index(), 1, start);
|
|
||||||
*this = object::from_ret();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace impl {
|
namespace impl {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include "test.h"
|
#include "test.h"
|
||||||
#include <pybind11/operators.h>
|
#include "pybind11/operators.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -8,19 +8,19 @@ struct Int {
|
|||||||
|
|
||||||
Int(int x) : x(x) {}
|
Int(int x) : x(x) {}
|
||||||
|
|
||||||
#define OPERATOR_IMPL(op) \
|
#define OPERATOR_IMPL(op) \
|
||||||
template <typename LHS, typename RHS> \
|
template <typename LHS, typename RHS> \
|
||||||
friend int operator op (const LHS& lhs, const RHS& rhs) { \
|
friend int operator op (const LHS& lhs, const RHS& rhs) { \
|
||||||
int l, r; \
|
int l, r; \
|
||||||
if constexpr(std::is_same_v<LHS, Int>) \
|
if constexpr(std::is_same_v<LHS, Int>) \
|
||||||
l = lhs.x; \
|
l = lhs.x; \
|
||||||
else \
|
else \
|
||||||
l = lhs; \
|
l = lhs; \
|
||||||
if constexpr(std::is_same_v<RHS, Int>) \
|
if constexpr(std::is_same_v<RHS, Int>) \
|
||||||
r = rhs.x; \
|
r = rhs.x; \
|
||||||
else \
|
else \
|
||||||
r = rhs; \
|
r = rhs; \
|
||||||
return l op r; \
|
return l op r; \
|
||||||
}
|
}
|
||||||
|
|
||||||
OPERATOR_IMPL(+)
|
OPERATOR_IMPL(+)
|
||||||
@ -148,3 +148,44 @@ TEST_F(PYBIND11_TEST, logic_operators) {
|
|||||||
EXPECT_FALSE(a >= b);
|
EXPECT_FALSE(a >= b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(PYBIND11_TEST, item_operators) {
|
||||||
|
py::module_ m = py::module_::import("__main__");
|
||||||
|
|
||||||
|
py::class_<std::vector<int>>(m, "vector")
|
||||||
|
.def(py::init<>())
|
||||||
|
.def("__getitem__",
|
||||||
|
[](std::vector<int>& v, int i) {
|
||||||
|
return v[i];
|
||||||
|
})
|
||||||
|
.def("__setitem__",
|
||||||
|
[](std::vector<int>& v, int i, int x) {
|
||||||
|
v[i] = x;
|
||||||
|
})
|
||||||
|
.def("push_back",
|
||||||
|
[](std::vector<int>& v, int x) {
|
||||||
|
v.push_back(x);
|
||||||
|
})
|
||||||
|
.def("__str__", [](const std::vector<int>& v) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "[";
|
||||||
|
for(size_t i = 0; i < v.size(); i++) {
|
||||||
|
if(i > 0) os << ", ";
|
||||||
|
os << v[i];
|
||||||
|
}
|
||||||
|
os << "]";
|
||||||
|
return os.str();
|
||||||
|
});
|
||||||
|
|
||||||
|
py::exec(R"(
|
||||||
|
v = vector()
|
||||||
|
v.push_back(1)
|
||||||
|
v.push_back(2)
|
||||||
|
v.push_back(3)
|
||||||
|
print(v)
|
||||||
|
assert v[0] == 1
|
||||||
|
assert v[1] == 2
|
||||||
|
assert v[2] == 3
|
||||||
|
v[1] = 4
|
||||||
|
assert v[1] == 4
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include "test.h"
|
#include "test.h"
|
||||||
#include <pybind11/stl.h>
|
#include "pybind11/stl.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -119,7 +119,8 @@ TEST_F(PYBIND11_TEST, dict_like) {
|
|||||||
py::object obj = py::cast(m);
|
py::object obj = py::cast(m);
|
||||||
EXPECT_EVAL_EQ("{'a': Point(1, 2), 'b': Point(3, 4)}", obj);
|
EXPECT_EVAL_EQ("{'a': Point(1, 2), 'b': Point(3, 4)}", obj);
|
||||||
|
|
||||||
std::unordered_map<std::string, Point> m2 = obj.cast<std::unordered_map<std::string, Point>>();
|
std::unordered_map<std::string, Point> m2 =
|
||||||
|
obj.cast<std::unordered_map<std::string, Point>>();
|
||||||
EXPECT_EQ(m, m2);
|
EXPECT_EQ(m, m2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user