homepage

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.

classification
Title: Ctypes inconsistent between Linux and OS X
Type: behavior Stage:
Components: ctypes Versions: Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: Daniel.Standage, amaury.forgeotdarc, belopolsky, eryksun, meador.inge, ned.deily
Priority: normal Keywords:

Created on 2014年11月26日 04:46 by Daniel.Standage, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (5)
msg231692 - (view) Author: Daniel Standage (Daniel.Standage) Date: 2014年11月26日 04:46
The ctypes documentation describes the '._as_parameter_' attribute, which simplifies using Python objects to encapsulate C objects. If a ctypes object with the '._as_parameter_' attribute defined is passed as a parameter to a C function, the value of '._as_parameter_' is supplied to the C function instead of a pointer to the Python project.
At least that's how it's advertised to work. And it does indeed work as advertised on Linux, but on Mac OS X the mechanism doesn't appear to work correctly. I have created a small dummy example demonstrating this at https://github.com/standage/ctypes-demo.
msg231693 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014年11月26日 06:16
FWIW, your test does seem to run if the C test program is instead built in 32-bit mode (modifying CFLAGS to include "-arch i386"). For example, with the Apple OS X 10.10 system Python:
DYLD_LIBRARY_PATH=.. arch -i386 /usr/bin/python2.7 test.py
Gizmo 1: 15
Gizmo 2: 20
Empty data
msg231697 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2014年11月26日 08:13
c_void_p's getfunc returns the address as an integer. In turn, an integer argument gets converted to a C int. This mean you need to a create a new c_void_p from the address for _as_parameter_, e.g.
 self._as_parameter_ = c_void_p(self.g)
Or set restype to a subclass of c_void_p. ctypes won't call the getfunc in this case, e.g. 
 class my_void_p(c_void_p):
 pass
 mylib.my_gizmo_create.restype = my_void_p
ConvParam
https://hg.python.org/cpython/file/ee879c0ffa11/Modules/_ctypes/callproc.c#l613
GetResult
https://hg.python.org/cpython/file/ee879c0ffa11/Modules/_ctypes/callproc.c#l903 
msg232716 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014年12月16日 06:30
Thanks, eryksun. Any objections to closing this issue?
msg232744 - (view) Author: Daniel Standage (Daniel.Standage) Date: 2014年12月16日 14:35
Sorry for my radio silence. Your example was very helpful, and restores the behavior I was looking for. Thanks!
History
Date User Action Args
2022年04月11日 14:58:10adminsetgithub: 67134
2014年12月16日 14:35:05Daniel.Standagesetstatus: open -> closed
resolution: works for me
messages: + msg232744
2014年12月16日 06:30:14ned.deilysetmessages: + msg232716
2014年11月26日 08:13:35eryksunsetnosy: + eryksun
messages: + msg231697
2014年11月26日 06:16:51ned.deilysetnosy: + amaury.forgeotdarc, meador.inge, belopolsky, ned.deily
messages: + msg231693
2014年11月26日 04:47:11Daniel.Standagesethgrepos: - hgrepo281
2014年11月26日 04:46:50Daniel.Standagecreate

AltStyle によって変換されたページ (->オリジナル) /