51 #if PY_MAJOR_VERSION >= 3
52 #define PyInt_Check PyLong_Check
53 #define PyInt_AsLong PyLong_AsLong
54 #define PyInt_FromLong PyLong_FromLong
61 namespace repeated_composite_container {
66 static Py_ssize_t
Length(PyObject* pself) {
72 self->parent_field_descriptor);
87 self->parent_field_descriptor,
88 self->child_message_class->py_message_factory->message_factory);
90 self->parent_field_descriptor, sub_message,
self->child_message_class);
93 message->GetReflection()->RemoveLast(
102 static PyObject*
AddMethod(PyObject*
self, PyObject*
args, PyObject* kwargs) {
114 py_cmsg =
Add(
self,
nullptr,
nullptr);
115 if (py_cmsg ==
nullptr)
return nullptr;
130 if (py_cmsg ==
nullptr) {
150 if (py_cmsg ==
nullptr) {
157 const FieldDescriptor* field_descriptor =
self->parent_field_descriptor;
159 Py_ssize_t end_index =
index;
160 if (end_index < 0) end_index +=
length;
161 if (end_index < 0) end_index = 0;
162 for (Py_ssize_t
i =
length;
i > end_index;
i --) {
176 PyErr_SetString(PyExc_TypeError,
"Value must be iterable");
180 while ((
next.reset(PyIter_Next(iter.
get()))) !=
NULL) {
182 PyErr_SetString(PyExc_TypeError,
"Not a cmessage");
186 if (new_message ==
NULL) {
195 if (PyErr_Occurred()) {
206 return Extend(
self, other);
222 PyErr_Format(PyExc_IndexError,
"list index (%zd) out of range",
index);
229 ->BuildSubMessageFromPointer(
self->parent_field_descriptor, sub_message,
230 self->child_message_class)
240 if (PyIndex_Check(
item)) {
242 index = PyNumber_AsSsize_t(
item, PyExc_IndexError);
243 if (
index == -1 && PyErr_Occurred())
return NULL;
246 }
else if (PySlice_Check(
item)) {
247 Py_ssize_t from, to, step, slicelength, cur,
i;
250 #if PY_MAJOR_VERSION >= 3
251 if (PySlice_GetIndicesEx(
item,
252 length, &from, &to, &step, &slicelength) == -1) {
254 if (PySlice_GetIndicesEx(
reinterpret_cast<PySliceObject*
>(
item),
255 length, &from, &to, &step, &slicelength) == -1) {
260 if (slicelength <= 0) {
261 return PyList_New(0);
263 result = PyList_New(slicelength);
264 if (!result)
return NULL;
266 for (cur = from,
i = 0;
i < slicelength; cur += step,
i++) {
273 PyErr_Format(PyExc_TypeError,
"indices must be integers, not %.200s",
274 item->ob_type->tp_name);
287 PyErr_SetString(PyExc_TypeError,
"does not support assignment");
292 self->parent_field_descriptor, slice);
304 Py_ssize_t
len =
Length(
reinterpret_cast<PyObject*
>(
self));
306 for (Py_ssize_t
i = 0;
i <
len;
i++) {
311 int result = PyObject_RichCompareBool(
item.get(),
value, Py_EQ);
323 PyErr_SetString(PyExc_ValueError,
"Item to delete not in list");
327 static PyObject*
RichCompare(PyObject* pself, PyObject* other,
int opid) {
332 PyErr_SetString(PyExc_TypeError,
333 "Can only compare repeated composite fields "
334 "against other repeated composite fields.");
337 if (opid == Py_EQ || opid == Py_NE) {
340 if (full_slice ==
NULL) {
350 if (other_list ==
NULL) {
353 return PyObject_RichCompare(list.
get(), other_list.
get(), opid);
355 Py_INCREF(Py_NotImplemented);
356 return Py_NotImplemented;
360 static PyObject*
ToStr(PyObject* pself) {
362 if (full_slice ==
NULL) {
370 return PyObject_Repr(list.
get());
377 PyObject* child_list) {
381 const Py_ssize_t
length =
Length(
reinterpret_cast<PyObject*
>(
self));
389 for (Py_ssize_t
i = 0;
i <
length; ++
i) {
391 PyList_GET_ITEM(child_list,
i));
402 PySequence_List(
reinterpret_cast<PyObject*
>(
self)));
403 if (child_list ==
NULL) {
415 static PyObject*
Sort(PyObject* pself, PyObject*
args, PyObject* kwds) {
422 PyObject* sort_func = PyDict_GetItemString(kwds,
"sort_function");
423 if (sort_func !=
NULL) {
426 PyDict_SetItemString(kwds,
"cmp", sort_func);
427 PyDict_DelItemString(kwds,
"sort_function");
439 static PyObject*
Item(PyObject* pself, Py_ssize_t
index) {
445 static PyObject*
Pop(PyObject* pself, PyObject*
args) {
449 Py_ssize_t
index = -1;
450 if (!PyArg_ParseTuple(
args,
"|n", &
index)) {
466 PyObject*
DeepCopy(PyObject* pself, PyObject* arg) {
487 self->parent = parent;
489 Py_INCREF(child_message_class);
490 self->child_message_class = child_message_class;
498 Py_CLEAR(
self->child_message_class);
516 {
"__deepcopy__",
DeepCopy, METH_VARARGS,
517 "Makes a deep copy of the class." },
518 {
"add", (PyCFunction)
AddMethod, METH_VARARGS | METH_KEYWORDS,
519 "Adds an object to the repeated container." },
521 "Appends a message to the end of the repeated container."},
522 {
"insert",
Insert, METH_VARARGS,
523 "Inserts a message before the specified index." },
525 "Adds objects to the repeated container." },
526 {
"pop",
Pop, METH_VARARGS,
527 "Removes an object from the repeated container and returns it." },
528 {
"remove",
Remove, METH_O,
529 "Removes an object from the repeated container." },
530 {
"sort", (PyCFunction)
Sort, METH_VARARGS | METH_KEYWORDS,
531 "Sorts the repeated container." },
533 "Adds objects to the repeated container." },
553 PyObject_HashNotImplemented,
560 "A Repeated scalar container",