[liblouis-liblouisxml] Re: ctypes and bad trailing pad byte

  • From: Christian Egli <christian.egli@xxxxxxxx>
  • To: liblouis-liblouisxml <liblouis-liblouisxml@xxxxxxxxxxxxx>
  • Date: Wed, 03 Feb 2010 12:11:29 +0100

Christian Egli <christian.egli@xxxxxxxx> writes:

> interpreter crashes with a segmentation fault. I instrumented the Python
> interface a bit to get some more debugging information (see attached
> file). 

Naturally I forgot to attach the file. 

from ctypes import *
import struct
import atexit

try:
    # Native win32
    _loader = windll
except NameError:
    # Unix/Cygwin
    _loader = cdll
liblouis = _loader["liblouis.so.2"]

atexit.register(liblouis.lou_free)

liblouis.lou_version.restype = c_char_p

liblouis.lou_translateString.argtypes = (
    c_char_p, c_wchar_p, POINTER(c_int), c_wchar_p, 
         POINTER(c_int), POINTER(c_char), POINTER(c_char), c_int)

def version():
    """Obtain version information for liblouis.
    @return: The version of liblouis, plus other information, such as
        the release date and perhaps notable changes.
    @rtype: str
    """
    return liblouis.lou_version()

def translateString(tran_tables, inbuf, typeform = None, mode = 0):
    """Translate a string of characters.
    @param tran_tables: A list of translation tables.
        The first table in the list must be a full pathname, unless the tables 
are in the current directory.
    @type tran_tables: list of str
    @param inbuf: The string to translate.
    @type inbuf: unicode
    @param typeform: A list of typeform constants indicating the typeform for 
each position in inbuf,
        C{None} for no typeform information.
    @type typeform: list of int
    @param mode: The translation mode; add multiple values for a combined mode.
    @type mode: int
    @return: The translated string.
    @rtype: unicode
    @raise RuntimeError: If a complete translation could not be done.
    @see: lou_translateString in the liblouis documentation
    """
    tablesString = ",".join([str(x) for x in tran_tables])
    inbuf = unicode(inbuf)
    if typeform:
        typeform = struct.pack('B'*len(typeform), *typeform)
    else:
        typeform = None
    inlen = c_int(len(inbuf))
    outlen = c_int(inlen.value*2)
    outbuf = create_unicode_buffer(outlen.value)
    print locals()
    if not liblouis.lou_translateString(tablesString, inbuf, byref(inlen), 
                                        outbuf, byref(outlen),  typeform, 
                                        None, mode):
        raise RuntimeError("can't translate: tables %s, inbuf %s, typeform %s, 
mode %s"%(tran_tables, inbuf, typeform, mode))
    print outbuf.value
    return outbuf.value

#{ Typeforms
plain_text = 0
italic = 1
underline = 2
bold = 4
computer_braille = 8

if __name__ == '__main__':
    # Just some common tests.
    print version()
    string = u'USA Today'
    typeform = len(string)*[italic]
    result = translateString(['de-ch-g2.ctb'], string, typeform=typeform)
    print "result: %s" % result
-- 
Christian Egli
Swiss Library for the Blind, Visually Impaired and Print Disabled
Grubenstrasse 12, CH-8045 Zürich, Switzerland

Other related posts: