Skip to content

Commit 445b716

Browse files
committed
Wow, this was a lesson. My full hatred goes to python, and C, and everything ... cool if you control everything, but not cool if an Object_New call doesn't do anything for you - creating a new instance of an own type in python doesn't appear to be that easy after all, at least not if you want your initializers/new procs to be called
1 parent f030fa1 commit 445b716

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

_fun.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,12 @@ DeltaChunk* DCV_end(DeltaChunkVector* vec)
192192
void DCV_dealloc(DeltaChunkVector* vec)
193193
{
194194
if (vec->mem){
195-
if (vec->size){
196-
const DeltaChunk* end = DCV_end(vec);
197-
DeltaChunk* i;
198-
for(i = &vec->mem[0]; i < end; i++){
199-
DC_destroy(i);
200-
}
195+
const DeltaChunk* end = DCV_end(vec);
196+
DeltaChunk* i;
197+
for(i = vec->mem; i < end; i++){
198+
DC_destroy(i);
201199
}
200+
202201
PyMem_Free(vec->mem);
203202
vec->size = 0;
204203
vec->reserved_size = 0;
@@ -207,7 +206,7 @@ void DCV_dealloc(DeltaChunkVector* vec)
207206
}
208207

209208
// Append num-chunks to the end of the list, possibly reallocating existing ones
210-
// Return a pointer to the first of the added items. They are not yet initialized
209+
// Return a pointer to the first of the added items. They are already null initialized
211210
// If num-chunks == 0, it returns the end pointer of the allocated memory
212211
static inline
213212
DeltaChunk* DCV_append_multiple(DeltaChunkVector* vec, uint num_chunks)
@@ -220,6 +219,11 @@ DeltaChunk* DCV_append_multiple(DeltaChunkVector* vec, uint num_chunks)
220219
Py_FatalError("Could not allocate memory for append operation");
221220
Py_ssize_t old_size = vec->size;
222221
vec->size += num_chunks;
222+
223+
for(;old_size < vec->size; ++old_size){
224+
DC_init(DCV_get(vec, old_size), 0, 0, 0, NULL);
225+
}
226+
223227
return &vec->mem[old_size];
224228
}
225229

@@ -235,21 +239,15 @@ typedef struct {
235239

236240

237241
static
238-
int DCL_init(DeltaChunkList *self, PyObject *args, PyObject *kwds)
242+
int DCL_init(DeltaChunkList*self, PyObject *args, PyObject *kwds)
239243
{
240-
if(PySequence_Size(args) > 1){
241-
PyErr_SetString(PyExc_ValueError, "Zero or one arguments are allowed, providing the initial size of the queue in DeltaChunks");
242-
return 0;
244+
if(args && PySequence_Size(args) > 0){
245+
PyErr_SetString(PyExc_ValueError, "Too many arguments");
246+
return -1;
243247
}
244248

245-
assert(self->vec.mem == NULL);
246-
247-
ull init_size = 0;
248-
PyArg_ParseTuple(args, "K", &init_size);
249-
if (init_size == 0){
250-
init_size = 12500;
251-
}
252-
return DCV_init(&self->vec, init_size);
249+
DCV_init(&self->vec, 0);
250+
return 0;
253251
}
254252

255253
static
@@ -285,8 +283,6 @@ PyObject* DCL_apply(PyObject* self, PyObject* args)
285283
Py_RETURN_NONE;
286284
}
287285

288-
289-
290286
static PyMethodDef DCL_methods[] = {
291287
{"apply", (PyCFunction)DCL_apply, METH_VARARGS, "Apply the given iterable of delta streams" },
292288
{"__len__", (PyCFunction)DCL_len, METH_NOARGS, NULL},
@@ -331,12 +327,24 @@ static PyTypeObject DeltaChunkListType = {
331327
0, /* tp_descr_get */
332328
0, /* tp_descr_set */
333329
0, /* tp_dictoffset */
334-
(initproc)DCL_init, /* tp_init */
330+
(initproc)DCL_init, /* tp_init */
335331
0, /* tp_alloc */
336-
0, /* tp_new */
332+
0, /* tp_new */
337333
};
338334

339335

336+
// Makes a new copy of the DeltaChunkList - you have to do everything yourselve
337+
// in C ... want C++ !!
338+
DeltaChunkList* DCL_new_instance(void)
339+
{
340+
DeltaChunkList* dcl = (DeltaChunkList*) PyType_GenericNew(&DeltaChunkListType, 0, 0);
341+
assert(dcl);
342+
343+
DCL_init(dcl, 0, 0);
344+
assert(dcl->vec.size == 0);
345+
return dcl;
346+
}
347+
340348
static inline
341349
ull msb_size(const char* data, Py_ssize_t dlen, Py_ssize_t offset, Py_ssize_t* out_bytes_read){
342350
ull size = 0;
@@ -351,7 +359,7 @@ ull msb_size(const char* data, Py_ssize_t dlen, Py_ssize_t offset, Py_ssize_t* o
351359
}// END while in range
352360

353361
*out_bytes_read = i+offset;
354-
assert((*out_bytes_read * 8) - (*out_bytes_read - 1) <= sizeof(ull));
362+
assert((*out_bytes_read * 8) - (*out_bytes_read - 1) <= sizeof(ull) * 8);
355363
return size;
356364
}
357365

@@ -373,7 +381,8 @@ static PyObject* connect_deltas(PyObject *self, PyObject *dstreams)
373381
DeltaChunkList* tdcl = 0;
374382
DeltaChunkList* dcl = 0;
375383

376-
dcl = tdcl = PyObject_New(DeltaChunkList, &DeltaChunkListType);
384+
dcl = tdcl = DCL_new_instance();
385+
assert(dcl != NULL);
377386
if (!dcl){
378387
PyErr_SetString(PyExc_RuntimeError, "Couldn't allocate list");
379388
return NULL;
@@ -478,7 +487,6 @@ PyMODINIT_FUNC init_fun(void)
478487
{
479488
PyObject *m;
480489

481-
DeltaChunkListType.tp_new = PyType_GenericNew;
482490
if (PyType_Ready(&DeltaChunkListType) < 0)
483491
return;
484492

stream.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,6 @@ def _set_cache_(self, attr):
339339
# the final delta data stream.
340340
dcl = connect_deltas(self._dstreams)
341341
assert dcl is not None
342-
print "got dcl"
343-
del(dcl)
344-
print "dealloc worked"
345342

346343
# call len directly, as the (optional) c version doesn't implement the sequence
347344
# protocol

0 commit comments

Comments
 (0)