My ctypes interface fails on OSX 10.8 (Mountain Lion) but works on Red Hat Linux 6

Phil Elson <[email protected]> Thu, 13 Sep 2012 10:54:56 +0100
Newsgroups gmane.comp.python.ctypes
Message-ID <CA+L60sA6FVTXfDW-EPRp416p28FGiajhBeu-5Qdhu_vEofQFYA@mail.gmail.com>
Hi,

I've been working on a ctypes interface to udunits2 (
http://www.unidata.ucar.edu/software/udunits/udunits-2/udunits2.html) and
have hit a strange issue when testing on OSX 10.8 (Mountain Lion).

I have built a udunits2.so (on Linux) and a udunits2.dylib (on OSx) which I
can interface to with the attached c code (ud.c) from both the command line
and via ctypes as per "example1.py". This works great across both Linux and
OSX.

However, the point of me using ctypes is to avoid the need for writing the
wrapping C code, so attempting to replicate "ud.c" with python ctypes code
in "example2.py" works great on Linux, but results in a udunits failure on
OSX.

I'm not sure what I'm doing wrong, the fact that this code works
beautifully from C means that I am suspicious about my ctypes code rather
than the udunits library itself is at fault, but I can't understand why my
ctypes code would work on one OS and not on the other.

Should you need it to understand my C code the udunits2 C docs can be found
here:
http://www.unidata.ucar.edu/software/udunits/udunits-2/udunits2lib.html

I'm using python 2.7.3, udunits 2.1.24 and the builtin ctypes python module
on both Red Hat 6 and OSX 10.8.

Does anyone have any idea what I'm doing wrong?

Any further questions, please just ask. Your help would be greatly
appreciated.

Thanks,

Phil

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
example1.py (application/octet-stream, 175 B)
import ctypes

import sys


if sys.platform == 'darwin':
    ud = ctypes.CDLL('/Users/pelson/ud.dylib')
else:
    ud = ctypes.CDLL('/home/pelson/ud.so')

ud.my_unit_name('s')
example2.py (application/octet-stream, 662 B)
import ctypes
import sys

def my_unit_name(unit_thing):
    if sys.platform == 'darwin':
        udunits = ctypes.CDLL('/usr/local/lib/libudunits2.dylib')
    else:
        udunits = ctypes.CDLL('/usr/local/lib/libudunits2.so')    

    u_system = udunits.ut_read_xml(None)
    if not u_system:
        raise RuntimeError('Error initializing udunits-2 unit system')
    
    u_1 = udunits.ut_parse(u_system, unit_thing, 0)
    if not u_1:
        raise RuntimeError('Error parsing units string %s' % unit_thing)

    udunits.ut_get_name.restype = ctypes.c_char_p
    print 'Test: %s' % udunits.ut_get_name(u_1)


if __name__ == '__main__':
    my_unit_name('m')
make.linux.sh (application/x-sh, 91 B)
gcc -w -O3 -c ud.c -o ud.o

gcc -ludunits2 -shared ud.o -o ud.so
gcc -ludunits2 ud.o -o ud
make.osx.sh (application/x-sh, 98 B)
gcc -w -O3 -c ud.c -o ud.o

gcc -ludunits2 -dynamiclib ud.o -o ud.dylib
gcc -ludunits2 ud.o -o ud
ud.c (text/x-csrc, 715 B)
#include <stdio.h>
#include <stdlib.h>

#include <udunits2.h>

int my_unit_name(const char * unit_thing)
{

    ut_system *u_system;
    ut_unit *u_1;

    /* Initialize the udunits-2 library */
    if( (u_system = ut_read_xml( NULL )) == NULL ) {
        fprintf( stderr, "Error initializing udunits-2 unit system\n" );
        exit(-1);
        }

    /* Parse the units strings */
    if( (u_1 = ut_parse( u_system, unit_thing, UT_ASCII )) == NULL ) {
        fprintf( stderr, "Error parsing units string \"%s\"\n", unit_thing );
        exit(-1);
        }

    printf( "Test: %s\n", ut_get_name(u_1, UT_ASCII));

    return(0);

}

int main()
{
    char *unit_name = "m";
    return my_unit_name(unit_name);
}