]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/test/dict.cpp
4f3490a42189152e239a65cbd8431ea43b637255
[ceph.git] / ceph / src / boost / libs / python / test / dict.cpp
1 // Copyright David Abrahams 2004. Distributed under the Boost
2 // Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4 #include <boost/python/module.hpp>
5 #define BOOST_ENABLE_ASSERT_HANDLER
6 #include <boost/assert.hpp>
7
8 #include <boost/python/def.hpp>
9 #include <boost/python/class.hpp>
10 #include <boost/python/dict.hpp>
11 #include <exception>
12 #include <string>
13
14 using namespace boost::python;
15
16 object new_dict()
17 {
18 return dict();
19 }
20
21 object data_dict()
22 {
23 dict tmp1;
24
25 dict tmp2;
26 tmp2["key2"] = "value2";
27 tmp1[1] = tmp2;
28
29 tmp1["key1"] = "value1";
30
31 return tmp1;
32 }
33
34 object dict_from_sequence(object sequence)
35 {
36 return dict(sequence);
37 }
38
39 object dict_keys(dict data)
40 {
41 return data.keys();
42 }
43
44 object dict_values(dict data)
45 {
46 return data.values();
47 }
48
49 object dict_items(dict data)
50 {
51 return data.items();
52 }
53
54 void work_with_dict(dict data1, dict data2)
55 {
56 if (!data1.has_key("k1")) {
57 throw std::runtime_error("dict does not have key 'k1'");
58 }
59 data1.update(data2);
60 }
61
62 void test_templates(object print)
63 {
64 std::string key = "key";
65
66 dict tmp;
67 tmp[1.5] = 13;
68 print(tmp.get(1.5));
69 tmp[1] = "a test string";
70 print(tmp.get(1));
71 print(tmp.get(44));
72 print(tmp);
73 print(tmp.get(2,"default"));
74 print(tmp.setdefault(3,"default"));
75
76 BOOST_ASSERT(!tmp.has_key(key));
77 }
78
79 BOOST_PYTHON_MODULE(dict_ext)
80 {
81 def("new_dict", new_dict);
82 def("data_dict", data_dict);
83 def("dict_keys", dict_keys);
84 def("dict_values", dict_values);
85 def("dict_items", dict_items);
86 def("dict_from_sequence", dict_from_sequence);
87 def("work_with_dict", work_with_dict);
88 def("test_templates", test_templates);
89 }
90
91 #include "module_tail.cpp"