Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #112204
| From | eryk sun <eryksun@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: ctypes Usage Note |
| Date | 2016-08-02 05:17 +0000 |
| Message-ID | <mailman.107.1470115083.6033.python-list@python.org> (permalink) |
| References | <4747f520-8091-486c-9801-3b9cee209799@googlegroups.com> <CACL+1aszGNk_BFO7mtH2+FAZFVrKe3QFgKs2kxbGQ5fitpGZ8A@mail.gmail.com> |
On Tue, Aug 2, 2016 at 1:41 AM, Lawrence D’Oliveiro
<lawrencedo99@gmail.com> wrote:
>
> Don’t do that. Do this instead:
>
> libc = ctypes.cdll.LoadLibrary("libc.so.6")
I recommend using ctypes.CDLL instead. ctypes.cdll is pretty much
pointless on Unix systems (and a potential problem on Windows due to
cached function pointers). It also doesn't allow passing the mode,
handle, and use_errno parameters.
As to the library name, you may instead want to use find_library for
Unix shared libraries:
>>> ctypes.util.find_library('c')
'libc.so.6'
>>> ctypes.util.find_library('python3.5m')
'libpython3.5m.so.1.0'
It's pretty much pointless on Windows since it just searches PATH for
the given name. It doesn't match how Windows LoadLibrary(Ex) really
works, which allows precise control over exactly what gets searched
via AddDllDirectory, SetDefaultDllDirectories, and activation contexts
(i.e. ActivateActCtx). Plus find_library('c') no longer even works in
3.5+ on Windows (it returns None) because the CRT has been split into
API sets (currently all mapped to ucrtbase.dll, but presumably that
can change with servicing).
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
ctypes Usage Note Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-08-01 18:41 -0700
Re: ctypes Usage Note eryk sun <eryksun@gmail.com> - 2016-08-02 05:17 +0000
Re: ctypes Usage Note Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-08-01 23:46 -0700
Re: ctypes Usage Note Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-08-03 19:22 -0700
Re: ctypes Usage Note Nobody <nobody@nowhere.invalid> - 2016-08-02 23:13 +0100
csiph-web