File: test_accessor.cpp

package info (click to toggle)
nanobind 2.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,060 kB
  • sloc: cpp: 11,838; python: 5,862; ansic: 4,820; makefile: 22; sh: 15
file content (39 lines) | stat: -rw-r--r-- 954 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <nanobind/nanobind.h>

namespace nb = nanobind;

struct A { int value; };

NB_MODULE(test_accessor_ext, m) {
    nb::class_<A>(m, "A")
    .def(nb::init<>())
    .def_rw("value", &A::value);

    m.def("test_str_attr_accessor_inplace_mutation", []() {
        nb::object a_ = nb::module_::import_("test_accessor_ext").attr("A")();
        a_.attr("value") += nb::int_(1);
        return a_;
    });

    m.def("test_str_item_accessor_inplace_mutation", []() {
        nb::dict d;
        d["a"] = nb::int_(0);
        d["a"] += nb::int_(1);
        return d;
    });

    m.def("test_num_item_list_accessor_inplace_mutation", []() {
        nb::list l;
        l.append(nb::int_(0));
        l[0] += nb::int_(1);
        return l;
    });

    m.def("test_obj_item_accessor_inplace_mutation", []() {
        nb::dict d;
        nb::int_ key = nb::int_(0);
        d[key] = nb::int_(0);
        d[key] += nb::int_(1);
        return d;
    });
}