[pypy-dev] r55005 - in pypy/branch/win32port/pypy: lib lib/_ctypes module/_rawffi module/_rawffi/test rlib rlib/test

Thomas Heller theller at ctypes.org
Wed May 21 18:48:33 CEST 2008


Maciej Fijalkowski schrieb:
>> Even cooler that I can read from day to day how you reimplemented
>> ctypes for pypy :-)
>>
>> "how to find c library" - is only a problem on windows (which I hope can
>> now be solved better).  On linux (not sure about *nix) simply call
>> LoadLibrary(None) [=> dlopen(NULL) in C], and you can get all functions
>> from all loaded shared libs from that handle.
> 
> No. That does not sound to me like a robust solution. (We should
> probably move this discussion to ctypes-users though).

I've cc'd to ctypes.users.

> Because you can
> have strange stuff in there and I really want to access libc, not
> something else. I think that helper in ctypes that gives you *exactly*
> libc, not only on windows is a good thing to have.

That may be, but AFAICT it doesn't work this way on linux.  Once you have
a loaded shared libraries (in the current process), you can access *all*
exported functions from this process from it:

theller at tubu610:~$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> from ctypes.util import find_library
>>> m = CDLL(find_library("m"))
>>> m
<CDLL 'libm.so.6', handle b7f652b0 at b7d867ac>
>>> c = CDLL(find_library("c"))
>>> c
<CDLL 'libc.so.6', handle b7f65558 at b7d8606c>
>>> c.printf
<_FuncPtr object at 0xb7da41cc>
>>> m.printf
<_FuncPtr object at 0xb7da4234>
>>> c.printf("hello, world\n")
hello, world
13
>>> m.printf("hello, world\n")
hello, world
13
>>> find_library("c")
'libc.so.6'
>>> find_library("m")
'libm.so.6'
>>>

It may be possible to get different behaviour when the libraries are loaded
using different RTLD_ flags.

>>
>> "how to get errno" - the problem with this is that errno is only valid
>> (in the calling thread) until another stdlib library function has been
>> called in this thread.
>>
>> So, since arbitrary code can run (in CPython's ctypes, at least) between
>> the time a function in a shared lib has been called and the time you access
>> errno there is no guarantee that it is still valid.
>> There has been some discussion on the ctypes-users list some months ago
>> about that, and the only reliable way to get errno for the function call
>> seems to be to get errno immediately after the function call.
>> The question is how to make this value available to the calling Python code.
>> Two ways were discussed: To attach the errno value in thread local storage
>> to the function object itself, or to pass it to the '.errcheck' function.
>> Nothing of this has yet been implemented in ctypes.
>>
> 
> Ah! Good :) I think I should start reading ctypes mailing list. We
> have support for such stuff already, as we need to get errno when
> testing posix stuff (I can explain in details if you like :)
> 
> It sounds like vastly better solution that ours, please implement it,
> we'll follow :)

There's an entry in the python bug tracker at
http://bugs.python.org/issue1798
where you could add comments if you have any.

> Cheers,
> fijal


-- 
Thanks,
Thomas



More information about the Pypy-dev mailing list