@@ -18,15 +18,13 @@ using the pxlib_ library.
1818"""
1919
2020import datetime
21-
2221import sys
22+ import atexit
2323
2424cimport cpython
2525from cpython.mem cimport PyMem_Malloc, PyMem_Free
2626from cpython.version cimport PY_MAJOR_VERSION
2727
28- from libc.stdlib cimport *
29-
3028from paradox cimport *
3129
3230cdef 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
311311cdef 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
349356cdef 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