Compare commits

..

No commits in common. "4e5021089cca14e42ec18fbb9bc7b7967fbaf2e8" and "ac8f4a1c2db55a184f1e4f11ee0ef81f50695475" have entirely different histories.

5 changed files with 33 additions and 75 deletions

View File

@ -99,12 +99,12 @@ struct type_caster<T, std::enable_if_t<is_floating_point_v<T>>> {
bool load(handle src, bool convert) { bool load(handle src, bool convert) {
if(isinstance<pkbind::float_>(src)) { if(isinstance<pkbind::float_>(src)) {
data = static_cast<T>(py_tofloat(src.ptr())); data = py_tofloat(src.ptr());
return true; return true;
} }
if(convert && isinstance<pkbind::int_>(src)) { if(convert && isinstance<pkbind::int_>(src)) {
data = static_cast<T>(py_toint(src.ptr())); data = py_toint(src.ptr());
return true; return true;
} }

View File

@ -585,14 +585,24 @@ 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()) : property(handle getter, handle setter = none()) : object() {
object(type::of<property>()(getter, setter)) {} auto start = py_peek(0);
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(type::of<staticmethod>()(method)) {} staticmethod(handle method) : object() {
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 {

View File

@ -1,5 +1,5 @@
#include "test.h" #include "test.h"
#include "pybind11/operators.h" #include <pybind11/operators.h>
namespace { namespace {
@ -148,44 +148,3 @@ 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
)");
}

View File

@ -1,5 +1,5 @@
#include "test.h" #include "test.h"
#include "pybind11/stl.h" #include <pybind11/stl.h>
namespace { namespace {
@ -119,8 +119,7 @@ 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 = std::unordered_map<std::string, Point> m2 = obj.cast<std::unordered_map<std::string, Point>>();
obj.cast<std::unordered_map<std::string, Point>>();
EXPECT_EQ(m, m2); EXPECT_EQ(m, m2);
} }
} }

View File

@ -472,18 +472,8 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
// initialize local variables to py_NIL // initialize local variables to py_NIL
memset(p1, 0, (char*)self->stack.sp - (char*)p1); memset(p1, 0, (char*)self->stack.sp - (char*)p1);
// submit the call // submit the call
if(!fn->cfunc) {
// python function
VM__push_frame(self, Frame__new(co, &fn->module, p0, argv, true)); VM__push_frame(self, Frame__new(co, &fn->module, p0, argv, true));
return opcall ? RES_CALL : VM__run_top_frame(self); return opcall ? RES_CALL : VM__run_top_frame(self);
} else {
// decl-based binding
self->__curr_function = p0;
bool ok = py_callcfunc(fn->cfunc, co->nlocals, argv);
self->stack.sp = p0;
self->__curr_function = NULL;
return ok ? RES_RETURN : RES_ERROR;
}
case FuncType_GENERATOR: { case FuncType_GENERATOR: {
bool ok = prepare_py_call(self->__vectorcall_buffer, argv, p1, kwargc, fn->decl); bool ok = prepare_py_call(self->__vectorcall_buffer, argv, p1, kwargc, fn->decl);
if(!ok) return RES_ERROR; if(!ok) return RES_ERROR;