eryksun added the comment:

> 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

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22552>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to