Skip to content

Commit 31240da

Browse files
committed
Better memory managment.
1 parent f9993e8 commit 31240da

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

paradox.pxd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ cdef extern from "paradox.h":
115115
char isnull
116116
int type
117117
Pxval_value value
118-
119-
118+
119+
void PX_boot()
120+
void PX_shutdown()
121+
120122
pxdoc_t* PX_new()
121123
pxdoc_t* PX_new2(void (*errorhandler)(pxdoc_t *p, int type, const_char_ptr msg, void *data),
122124
void* (*allocproc)(pxdoc_t *p, size_t size, const_char_ptr caller),
@@ -132,8 +134,10 @@ cdef extern from "paradox.h":
132134
int PX_set_parameter(pxdoc_t *pxdoc, char *name, char *value)
133135
int PX_set_value(pxdoc_t *pxdoc, char *name, float value)
134136
int PX_set_blob_file(pxdoc_t *pxdoc, const_char_ptr filename)
135-
int PX_has_blob_file(pxdoc_t *pxdoc)
136-
int PX_close(pxdoc_t *pxdoc)
137+
bint PX_has_blob_file(pxdoc_t *pxdoc)
138+
void PX_close(pxdoc_t *pxdoc)
139+
void PX_delete(pxdoc_t *pxdoc)
140+
137141

138142
void* PX_get_record(pxdoc_t *pxdoc, int recno, void *data)
139143
void* PX_get_record2(pxdoc_t *pxdoc, int recno, void *data, int *deleted, pxdatablockinfo_t *pxdbinfo)

pxpy.pyx

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ using the pxlib_ library.
1818
"""
1919

2020
import datetime
21-
2221
import sys
22+
import atexit
2323

2424
cimport cpython
2525
from cpython.mem cimport PyMem_Malloc, PyMem_Free
2626
from cpython.version cimport PY_MAJOR_VERSION
2727

28-
from libc.stdlib cimport *
29-
3028
from paradox cimport *
3129

3230
cdef void errorhandler(pxdoc_t *p, int type, char *msg, void *data):
@@ -47,12 +45,12 @@ cdef class Table:
4745
cdef readonly Record record
4846
cdef int current_recno
4947
cdef PrimaryIndex primary_index
50-
targetEncoding = "utf-8"
51-
inputEncoding = "utf-8"
48+
targetEncoding = "utf8"
49+
inputEncoding = "utf8"
5250

5351
cdef fields
5452

55-
def __init__(self, filename):
53+
def __cinit__(self, filename):
5654
self.doc = PXDoc(filename)
5755

5856
def create(self, *fields):
@@ -174,7 +172,7 @@ cdef class PXDoc:
174172
cdef bytes filename
175173
cdef char isopen
176174

177-
def __init__(self, filename):
175+
def __cinit__(self, filename):
178176
"""
179177
Create a PXDoc instance, associated to the given external filename.
180178
"""
@@ -281,7 +279,7 @@ cdef class PXDoc:
281279
if v == None:
282280
l = 0
283281
if f.ftype == pxfAlpha:
284-
s = unicode(str(v or '').decode('utf-8'))
282+
s = str(v or '').decode(self.inputEncoding)
285283
s = s.encode(self.getCodePage())
286284
b = <char *>(self.px_doc.malloc(self.px_doc, f.flen, "Memory for alpha field"))
287285
memcpy(b, <char *>s, max(f.flen, len(s)))
@@ -306,6 +304,8 @@ cdef class PXDoc:
306304
Close the data file
307305
"""
308306
self.close()
307+
PX_delete(self.px_doc)
308+
309309

310310

311311
cdef class PrimaryIndex:
@@ -317,7 +317,7 @@ cdef class PrimaryIndex:
317317
cdef bytes filename
318318
cdef char isopen
319319

320-
def __init__(self, filename):
320+
def __cinit__(self, filename):
321321
"""
322322
Create a PXDoc instance, associated to the given external filename.
323323
"""
@@ -345,6 +345,13 @@ cdef class PrimaryIndex:
345345
PX_close(self.px_doc)
346346
self.isopen = 0
347347

348+
def __dealloc__(self):
349+
"""
350+
Close the data file
351+
"""
352+
self.close()
353+
PX_delete(self.px_doc)
354+
348355

349356
cdef class ParadoxField:
350357
cdef readonly fname
@@ -523,7 +530,8 @@ cdef class RecordField(ParadoxField):
523530

524531
if blobdata and size > 0:
525532
codepage = self.record.doc.getCodePage()
526-
py_string = PyString_FromStringAndSize(<char*> blobdata, size);
533+
py_string = PyString_FromStringAndSize(<char*> blobdata, size)
534+
self.record.doc.px_doc.free(self.record.doc.px_doc, blobdata)
527535
if not py_string:
528536
raise Exception("Cannot get value from string %s" % self.fname)
529537
return PyString_AsDecodedObject(py_string, codepage, "replace")
@@ -540,7 +548,10 @@ cdef class RecordField(ParadoxField):
540548
return None
541549

542550
if blobdata and size > 0:
543-
return PyString_FromStringAndSize(blobdata, size)
551+
py_string = PyString_FromStringAndSize(blobdata, size)
552+
self.record.doc.px_doc.free(self.record.doc.px_doc, blobdata)
553+
return py_string
554+
544555

545556
elif self.ftype == pxfOLE:
546557
pass
@@ -629,3 +640,11 @@ cdef class Record:
629640

630641
def __getitem__(self, key):
631642
return self.fields[key]
643+
644+
# Sets up locale for pxlib
645+
PX_boot()
646+
647+
# Shut down pxlib
648+
def __dealloc__():
649+
PX_shutdown()
650+
atexit.register(__dealloc__)

0 commit comments

Comments
 (0)