This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Ivan.Pozdeev, amaury.forgeotdarc, belopolsky, eryksun, meador.inge, r.david.murray
Date 2014-10-05.03:29:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1412479755.16.0.8765385362.issue22552@psf.upfronthosting.co.za>
In-reply-to
Content
> Packages do this because it's the natural thing to do

I guess the tutorial is channeling projects toward using the cdll/windll LibraryLoader instances on Windows. It even shows using cdll.LoadLibrary('libc.so.6') on Linux. That's equivalent to CDLL('libc.so.6'); I don't know why one would bother with cdll.LoadLibrary.

> there's not even a notion they _need_ to be cloned. 

The ctypes reference has always explained how CDLL instances cache function pointers via __getattr__ and (formerly) __getitem__. 

The same section also documents that LibraryLoader.__getattr__ caches libraries. However, it's missing an explanation of LibraryLoader.__getitem__, which returns getattr(self, name), for use when the library name isn't a valid Python identifier.

> there's no apparent way to clone a pointer

You can use pointer casting or from_buffer_copy to create a new function pointer. It isn't a clone because it only uses the function pointer type, not the current value of restype, argtypes, and errcheck. But this may be all you need. For example:

    >>> from ctypes import *
    >>> libm = CDLL('libm.so.6')

cast:

    >>> sin = cast(libm.sin, CFUNCTYPE(c_double, c_double))
    >>> sin(3.14/2)
    0.9999996829318346

    >>> sin2 = cast(sin, type(sin))
    >>> sin2.argtypes
    (<class 'ctypes.c_double'>,)
    >>> sin2.restype
    <class 'ctypes.c_double'>

from_buffer_copy:

    >>> sin = CFUNCTYPE(c_double, c_double).from_buffer_copy(libm.sin)
    >>> sin(3.14/2)
    0.9999996829318346

https://docs.python.org/3/library/ctypes.html#ctypes.cast
https://docs.python.org/3/library/ctypes.html#function-prototypes
History
Date User Action Args
2014-10-05 03:29:15eryksunsetrecipients: + eryksun, amaury.forgeotdarc, belopolsky, r.david.murray, meador.inge, Ivan.Pozdeev
2014-10-05 03:29:15eryksunsetmessageid: <1412479755.16.0.8765385362.issue22552@psf.upfronthosting.co.za>
2014-10-05 03:29:15eryksunlinkissue22552 messages
2014-10-05 03:29:14eryksuncreate