Author Michael.Felt
Recipients David.Edelsohn, Michael.Felt, martin.panter
Date 2016-04-30.08:04:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1462003476.91.0.65856036906.issue26439@psf.upfronthosting.co.za>
In-reply-to
Content
Question - before I submit a patch.

A. Is there a PEP I should read re: ctypes/util and/or ctypes/cdll?

B. I show two different behaviors of responding - My question is, what does the community think should be the response? My preference is to bring the request back to it's stub - (strip lib from the beginning, and .so* or .a from the suffix, so that it is in it's -lFOO form - as if being offered to a linker (with -lFOO, we do not use -lFOO.so.6 (e.g., for libc.so.6).
I have seen a lot of variance in how cdll is used (in only two python based projects I am currently interested in porting). Calls are frequently made using some variation including .so somewhere in the name and/or code is basing decesions based on a the number (###) returned by find_library (e.g., libFOO.so.###, and ### MUST be one of a list - and is rejected if not a match (even if newer)). I would think if there is a version dependancy this should be a call to the library loaded rather than an external string.
In any case, - for one example - as libc.so.* will never exist, by default, on AIX, for ease of use I believe that translating libFOO.* to FOO and then doing the 'find' comes closest to what was intended (long long ago) - again, is there a PEP with guidance on this AND my preference, for AIX, is to look in .a archives first for a .so member, and then look for an AIX legacy member (shr.*.o) and then look for an external .so.* file.

With my current working version - this is the output of the test in util.py:

root@x064:[/data/prj/aixtools/python/python-2.7.11.3/Lib/ctypes]../../python ./util.py
libm.a
libc.a
libbz2.a
<CDLL 'None', handle b at 700000000216630>
<CDLL 'libcrypt.a(shr_64.o)', handle c at 700000000216630>
libcrypt.a

Additional Tests for AIX
call find_library("foo")
c:  libc.a
c.a:  None
c.so:  None
libc:  libc.a
libc.a:  libc.a
libc.so.6:  libc.a
crypt:  libcrypt.a
crypto:  libcrypto.a
crypto.so:  None
libcrypto.so:  libcrypto.a

call cdll.LoadLibrary("foo")
m:  <CDLL 'None', handle d at 700000000216860>
libm.so:  <CDLL 'None', handle e at 700000000216860>
c:  <CDLL 'libc.a(shr_64.o)', handle f at 700000000216860>
c.a:  <CDLL 'libc.a(shr_64.o)', handle 10 at 700000000216860>
libc.a:  <CDLL 'libc.a(shr_64.o)', handle 11 at 700000000216860>
libc.so.6:  <CDLL 'libc.a(shr_64.o)', handle 12 at 700000000216860>
libc.so.9:  <CDLL 'libc.a(shr_64.o)', handle 13 at 700000000216860>
bz2:  <CDLL 'libbz2.a(libbz2.so.1)', handle 14 at 700000000216860>
libbz2:  <CDLL 'libbz2.a(libbz2.so.1)', handle 15 at 700000000216860>
crypt:  <CDLL 'libcrypt.a(shr_64.o)', handle 16 at 700000000216860>
crypto:  <CDLL 'libcrypto.a(libcrypto64.so)', handle 17 at 700000000216860>
crypto.so:  <CDLL 'libcrypto.a(libcrypto64.so)', handle 18 at 700000000216860>
libcrypto.so:  <CDLL 'libcrypto.a(libcrypto64.so)', handle 19 at 700000000216860>
iconv:  <CDLL 'libiconv.a(libiconv.so.2)', handle 1a at 700000000216860>
intl:  <CDLL 'libintl.a(libintl.so.1)', handle 1b at 700000000216860>

The test looks like:
################################################################
# test code

def test():
    from ctypes import cdll
    if os.name == "nt":
        print cdll.msvcrt
        print cdll.load("msvcrt")
        print find_library("msvcrt")

    if os.name == "posix":
        # find and load_version
        print find_library("m")
        print find_library("c")
        print find_library("bz2")

        # getattr
##        print cdll.m
##        print cdll.bz2

        # load
        if sys.platform == "darwin":
            print cdll.LoadLibrary("libm.dylib")
            print cdll.LoadLibrary("libcrypto.dylib")
            print cdll.LoadLibrary("libSystem.dylib")
            print cdll.LoadLibrary("System.framework/System")
        else:
            print cdll.LoadLibrary("libm.so")
            print cdll.LoadLibrary("libcrypt.so")
            print find_library("crypt")

        if sys.platform.startswith("aix"):
            print "\nAdditional Tests for AIX"
            print "call find_library(\"foo\")"
            print "c: ", find_library("c")
            print "c.a: ", find_library("c.a")
            print "c.so: ", find_library("c.so")
            print "libc: ", find_library("libc")
            print "libc.a: ", find_library("libc.a")
            print "libc.so.6: ", find_library("libc.so.6")
            print "crypt: ", find_library("crypt")
            print "crypto: ", find_library("crypto")
            print "crypto.so: ", find_library("crypto.so")
            print "libcrypto.so: ", find_library("libcrypto.so")
###
            print "\ncall cdll.LoadLibrary(\"foo\")"
            print "m: ", cdll.LoadLibrary("m")
            print "libm.so: ", cdll.LoadLibrary("libm.so")
            print "c: ", cdll.LoadLibrary("c")
            print "c.a: ", cdll.LoadLibrary("c.a")
            print "libc.a: ", cdll.LoadLibrary("libc.a")
            print "libc.so.6: ", cdll.LoadLibrary("libc.so.6")
            print "libc.so.9: ", cdll.LoadLibrary("libc.so.9")
            print "bz2: ", cdll.LoadLibrary("bz2")
            print "libbz2: ", cdll.LoadLibrary("libbz2")
            print "crypt: ", cdll.LoadLibrary("crypt")
            print "crypto: ", cdll.LoadLibrary("crypto")
            print "crypto.so: ", cdll.LoadLibrary("crypto.so")
            print "libcrypto.so: ", cdll.LoadLibrary("libcrypto.so")
            print "iconv: ", cdll.LoadLibrary("iconv")
            print "intl: ", cdll.LoadLibrary("intl")

if __name__ == "__main__":
    test()

Thank you for your considered comments. My goal is ease of use for (porting) existing projects to AIX, i.e., ideally without code change.
History
Date User Action Args
2016-04-30 08:04:37Michael.Feltsetrecipients: + Michael.Felt, martin.panter, David.Edelsohn
2016-04-30 08:04:36Michael.Feltsetmessageid: <1462003476.91.0.65856036906.issue26439@psf.upfronthosting.co.za>
2016-04-30 08:04:36Michael.Feltlinkissue26439 messages
2016-04-30 08:04:35Michael.Feltcreate