forked from boostorg/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
140 lines (120 loc) · 3.02 KB
/
list.cpp
File metadata and controls
140 lines (120 loc) · 3.02 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python/list.hpp>
namespace boost { namespace python { namespace detail {
detail::new_non_null_reference list_base::call(object const& arg_)
{
return (detail::new_non_null_reference)
(expect_non_null)(
PyObject_CallFunction(
(PyObject*)&PyList_Type, "(O)",
arg_.ptr()));
}
list_base::list_base()
: object(detail::new_reference(PyList_New(0)))
{}
list_base::list_base(object_cref sequence)
: object(list_base::call(sequence))
{}
void list_base::append(object_cref x)
{
if (PyList_CheckExact(this->ptr()))
{
if (PyList_Append(this->ptr(), x.ptr()) == -1)
throw_error_already_set();
}
else
{
this->attr("append")(x);
}
}
//long list_base::count(object_cref value) const;
void list_base::extend(object_cref sequence)
{
this->attr("extend")(sequence);
}
long list_base::index(object_cref value) const
{
object result_obj(this->attr("index")(value));
long result = PyInt_AsLong(result_obj.ptr());
if (result == -1)
throw_error_already_set();
return result;
}
void list_base::insert(int index, object_cref item)
{
if (PyList_CheckExact(this->ptr()))
{
if (PyList_Insert(this->ptr(), index, item.ptr()) == -1)
throw_error_already_set();
}
else
{
this->attr("insert")(index, item);
}
}
void list_base::insert(object const& index, object_cref x)
{
long index_ = PyInt_AsLong(index.ptr());
if (index_ == -1 && PyErr_Occurred())
throw_error_already_set();
this->insert(index_, x);
}
object list_base::pop()
{
return this->attr("pop")();
}
object list_base::pop(long index)
{
return this->pop(object(index));
}
object list_base::pop(object const& index)
{
return this->attr("pop")(index);
}
void list_base::remove(object_cref value)
{
this->attr("remove")(value);
}
void list_base::reverse()
{
if (PyList_CheckExact(this->ptr()))
{
if (PyList_Reverse(this->ptr()) == -1)
throw_error_already_set();
}
else
{
this->attr("reverse")();
}
}
void list_base::sort()
{
if (PyList_CheckExact(this->ptr()))
{
if (PyList_Sort(this->ptr()) == -1)
throw_error_already_set();
}
else
{
this->attr("sort")();
}
}
void list_base::sort(object_cref cmpfunc)
{
this->attr("sort")(cmpfunc);
}
// For some reason, moving this to the end of the TU suppresses an ICE
// with vc6.
long list_base::count(object_cref value) const
{
object result_obj(this->attr("count")(value));
long result = PyInt_AsLong(result_obj.ptr());
if (result == -1)
throw_error_already_set();
return result;
}
}}} // namespace boost::python