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: Call Mac API Crash via ctypes
Type: crash Stage: resolved
Components: ctypes Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: theller Nosy List: heyt1981, ned.deily, ronaldoussoren, theller
Priority: normal Keywords:

Created on 2011-03-11 05:52 by heyt1981, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg130546 - (view) Author: yufun (heyt1981) Date: 2011-03-11 05:52
appsLib =cdll.LoadLibrary(util.find_library('ApplicationServices'))
event = appsLib.CGEventCreate(None);
pt = appsLib.CGEventGetLocation(event);

Crash after execute above python code. But these code works well in python3.13.
msg130559 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2011-03-11 11:45
On OS X 10.6, the code segment segfaults for me in 64-bit mode but not in 32-bit mode.  If you are using a 64-bit/32-bit Python 3.2, try running your failing code in 32-bit mode:

arch -i386 python3.2

The 3.1.3 Python is most likely a 32-bit-only executable.
msg130643 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2011-03-11 23:17
This is almost certainly not a bug in Python, but in the code in this issue.


CGEventGetLocation() is a function that returns a CGPoint, while ctypes assumes that function returns value of C type int. The effect of this is that ctypes will create the wrong stack frame during the call and that messes things up. 

That this works in 32-bit mode is more or less an accident, the calling conventions in 32-bit mode are different than that in 64-bit mode and this happens to work.

To get this code to function correctly you need to add annotations to the function object using ctypes, that documentation for ctypes in the stdlib reference explains how to do so.

I therefore propose to close this issue.
msg130649 - (view) Author: yufun (heyt1981) Date: 2011-03-12 01:25
thanks Ned and Ronald, I totally agree with you, I think it's a 32/64 issue. But, I'm not clear about the 32 and 64 bit in Python and don't know how to do let my code works, could you please explain more detail about that?
msg130688 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2011-03-12 16:14
yufun, you better ask on python-list or the pythonmac-sig e-mail list (<http://www.python.org/community/sigs/current/pythonmac-sig/>). The added bonus is that you'll get into contact with other users of Python on OSX.

The ctypes API is explained here: <http://docs.python.org/library/ctypes.html>. This page also explains how to specify the function signature for the function you try to call.
History
Date User Action Args
2022-04-11 14:57:14adminsetgithub: 55673
2011-03-12 16:14:48ronaldoussorensetstatus: open -> closed
nosy: theller, ronaldoussoren, ned.deily, heyt1981
messages: + msg130688

resolution: not a bug
stage: resolved
2011-03-12 01:25:40heyt1981setnosy: theller, ronaldoussoren, ned.deily, heyt1981
messages: + msg130649
2011-03-11 23:17:34ronaldoussorensettype: crash

messages: + msg130643
nosy: + ronaldoussoren
2011-03-11 11:45:07ned.deilysetnosy: + ned.deily
messages: + msg130559
2011-03-11 05:52:45heyt1981create