Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f13196

Browse files
committed
Sequence methods, failing sq_ass_item.
1 parent fdef5a2 commit 3f13196

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

‎src/cpy/Object/cSeqObject.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,13 @@ SequenceLongObject_sq_item(PyObject *self, Py_ssize_t index) {
215215
}
216216
return PyLong_FromLong(((SequenceLongObject *) self)->array_long[my_index]);
217217
}
218+
218219
static int
219220
SequenceLongObject_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) {
221+
fprintf(
222+
stdout, "%s()#%d: self=%p index=%zd value=%p\n",
223+
__FUNCTION__, __LINE__, (void *) self, index, (void *) value
224+
);
220225
Py_ssize_t my_index = index;
221226
if (my_index < 0) {
222227
my_index += SequenceLongObject_sq_length(self);
@@ -235,9 +240,9 @@ SequenceLongObject_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value
235240
/* Just set the value. */
236241
if (!PyLong_Check(value)) {
237242
PyErr_Format(
238-
PyExc_TypeError,
239-
"sq_ass_item value needs to be an int, not type %s",
240-
Py_TYPE(value)->tp_name
243+
PyExc_TypeError,
244+
"sq_ass_item value needs to be an int, not type %s",
245+
Py_TYPE(value)->tp_name
241246
);
242247
return -1;
243248
}
@@ -248,46 +253,51 @@ SequenceLongObject_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value
248253
SequenceLongObject *self_as_slo = (SequenceLongObject *) self;
249254
/* Special case: deleting the only item in the array. */
250255
if (self_as_slo->size == 1) {
256+
fprintf(stdout, "%s()#%d: deleting empty index\n", __FUNCTION__, __LINE__);
251257
free(self_as_slo->array_long);
252258
self_as_slo->array_long = NULL;
253259
self_as_slo->size = 0;
254260
} else {
255261
/* Delete the value and re-compose the array. */
262+
fprintf(stdout, "%s()#%d: deleting index=%zd\n", __FUNCTION__, __LINE__, index);
256263
long *new_array = malloc((self_as_slo->size - 1) * sizeof(long));
257264
if (!new_array) {
258265
PyErr_Format(
259-
PyExc_MemoryError,
260-
"sq_ass_item can not allocate new array. %s#%d",
261-
__FILE__, __LINE__
266+
PyExc_MemoryError,
267+
"sq_ass_item can not allocate new array. %s#%d",
268+
__FILE__, __LINE__
262269
);
263270
return -1;
264271
}
265-
/* memcpy across to the new array, firstly up to the index. */
272+
/* memcpy parameters. */
266273
void *dest = NULL;
267274
void *src = NULL;
268275
size_t count = 0;
269276

277+
/* memcpy across to the new array, firstly up to the index. */
270278
dest = new_array;
271279
src = self_as_slo->array_long;
272280
count = my_index * sizeof(long);
281+
fprintf(stdout, "%s()#%d: First: dest=%p src=%p count=%zu\n", __FUNCTION__, __LINE__, dest, src, count);
273282
if (memcpy(dest, src, count) != dest) {
274283
PyErr_Format(
275-
PyExc_MemoryError,
276-
"sq_ass_item can not memcpy into new array. %s#%d",
277-
__FILE__, __LINE__
284+
PyExc_MemoryError,
285+
"sq_ass_item can not memcpy into new array. %s#%d",
286+
__FILE__, __LINE__
278287
);
279288
return -1;
280289
}
281-
/* memcpy across to the new array, from the index to the end. */
290+
/* memcpy from the index to the end. */
282291
dest = new_array + count;
283-
src = self_as_slo->array_long + (count-sizeof(long));
292+
src = self_as_slo->array_long + count;
284293
/* Example size=4, index=2 copy one value */
285294
count = (self_as_slo->size - my_index - 1) * sizeof(long);
295+
fprintf(stdout, "%s()#%d: Next: dest=%p src=%p count=%zu\n", __FUNCTION__, __LINE__, dest, src, count);
286296
if (memcpy(dest, src, count) != dest) {
287297
PyErr_Format(
288-
PyExc_MemoryError,
289-
"sq_ass_item can not memcpy into new array. %s#%d",
290-
__FILE__, __LINE__
298+
PyExc_MemoryError,
299+
"sq_ass_item can not memcpy into new array. %s#%d",
300+
__FILE__, __LINE__
291301
);
292302
return -1;
293303
}

‎tests/unit/test_c_seqobject.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,20 @@ def test_SequenceLongObject_item_raises(initial_sequence, index, expected):
156156
(
157157
[7, 4, 1, ], -1, 14, [7, 4, 14, ],
158158
),
159+
# (
160+
# [], 0, None, [],
161+
# ),
159162
(
160163
[7,], 0, None, [],
161164
),
162165
(
163166
[7,], -1, None, [],
164167
),
165168
(
166-
[7, 4, 1, ], 0, None, [4, 14, ],
169+
[7, 4, 1, ], 1, None, [7, 1, ],
170+
),
171+
(
172+
[7, 4, ], 0, None, [4, ],
167173
),
168174
)
169175
)
@@ -176,6 +182,23 @@ def test_SequenceLongObject_setitem(initial_sequence, index, value, expected):
176182
assert list(obj) == expected
177183

178184

185+
@pytest.mark.parametrize(
186+
'initial_sequence, index, value, expected',
187+
(
188+
(
189+
[7, 4, 1, ], 1, None, [7, 1, ],
190+
),
191+
)
192+
)
193+
def test_SequenceLongObject_setitem_debug(initial_sequence, index, value, expected):
194+
obj = cSeqObject.SequenceLongObject(initial_sequence)
195+
if value is not None:
196+
obj[index] = value
197+
else:
198+
del obj[index]
199+
assert list(obj) == expected
200+
201+
179202
# @pytest.mark.skipif(not (sys.version_info.minor < 7), reason='Python < 3.7')
180203
# def test_str_dir_pre_37():
181204
# s = cObject.Str()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /