Skip to content

Commit e774c9b

Browse files
committed
Sequence methods, finish concat.
1 parent 03fd2f8 commit e774c9b

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

doc/sphinx/source/new_types.rst

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,6 @@ Tests are in ``tests/unit/test_c_seqobject.py``:
617617
obj = cSeqObject.SequenceLongObject([7, 4, 1, ])
618618
assert len(obj) == 3
619619
620-
621-
622620
---------------
623621
``sq_concat``
624622
---------------
@@ -650,12 +648,7 @@ In ``src/cpy/Object/cSeqObject.c``:
650648
static PyTypeObject SequenceLongObjectType;
651649
static int is_sequence_of_long_type(PyObject *op);
652650
653-
/**
654-
* Returns a new SequenceLongObject composed of self + other.
655-
* @param self
656-
* @param other
657-
* @return
658-
*/
651+
/** Returns a new SequenceLongObject composed of self + other. */
659652
static PyObject *
660653
SequenceLongObject_sq_concat(PyObject *self, PyObject *other) {
661654
if (!is_sequence_of_long_type(other)) {
@@ -667,24 +660,32 @@ In ``src/cpy/Object/cSeqObject.c``:
667660
return NULL;
668661
}
669662
PyObject *ret = SequenceLongObject_new(&SequenceLongObjectType, NULL, NULL);
663+
if (!ret) {
664+
assert(PyErr_Occurred());
665+
return NULL;
666+
}
670667
/* For convenience. */
671-
SequenceLongObject *sol = (SequenceLongObject *) ret;
672-
sol->size = ((SequenceLongObject *) self)->size + ((SequenceLongObject *) other)->size;
673-
sol->array_long = malloc(sol->size * sizeof(long));
674-
if (!sol->array_long) {
668+
SequenceLongObject *ret_as_slo = (SequenceLongObject *) ret;
669+
ret_as_slo->size = ((SequenceLongObject *) self)->size + ((SequenceLongObject *) other)->size;
670+
ret_as_slo->array_long = malloc(ret_as_slo->size * sizeof(long));
671+
if (!ret_as_slo->array_long) {
675672
PyErr_Format(PyExc_MemoryError, "%s(): Can not create new object.", __FUNCTION__);
673+
Py_DECREF(ret);
674+
return NULL;
676675
}
677676
678677
ssize_t i = 0;
679678
ssize_t ub = ((SequenceLongObject *) self)->size;
680679
while (i < ub) {
681-
sol->array_long[i] = ((SequenceLongObject *) self)->array_long[i];
680+
ret_as_slo->array_long[i] = ((SequenceLongObject *) self)->array_long[i];
682681
i++;
683682
}
684-
ub += ((SequenceLongObject *) other)->size;
685-
while (i < ub) {
686-
sol->array_long[i] = ((SequenceLongObject *) other)->array_long[i];
683+
ssize_t j = 0;
684+
ub = ((SequenceLongObject *) other)->size;
685+
while (j < ub) {
686+
ret_as_slo->array_long[i] = ((SequenceLongObject *) other)->array_long[j];
687687
i++;
688+
j++;
688689
}
689690
return ret;
690691
}
@@ -697,6 +698,17 @@ Tests are in ``tests/unit/test_c_seqobject.py``:
697698

698699
.. code-block:: python
699700
701+
def test_SequenceLongObject_concat():
702+
obj_a = cSeqObject.SequenceLongObject([7, 4, 1, ])
703+
obj_b = cSeqObject.SequenceLongObject([70, 40, 100, ])
704+
assert id(obj_a) != id(obj_b)
705+
obj = obj_a + obj_b
706+
assert id(obj) != id(obj_a)
707+
assert id(obj) != id(obj_b)
708+
assert len(obj) == 6
709+
assert list(obj) == [7, 4, 1, ] + [70, 40, 100, ]
710+
711+
700712
701713
TOOD:
702714

src/cpy/Object/cSeqObject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ SequenceLongObject_sq_concat(PyObject *self, PyObject *other) {
145145
return ret;
146146
}
147147

148-
149148
static PyObject *
150149
SequenceLongObject_sq_item(PyObject *self, Py_ssize_t index) {
151150
// fprintf(stdout, "%s(): index=%zd\n", __FUNCTION__, index);

0 commit comments

Comments
 (0)