Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Macintosh Gestalt interface */ |
| 26 | |
| 27 | #include "Python.h" |
| 28 | |
| 29 | #include <Carbon/Carbon.h> |
| 30 | |
| 31 | /* Convert a 4-char string object argument to an OSType value */ |
| 32 | static int |
| 33 | convert_to_OSType(PyObject *v, OSType *pr) |
| 34 | { |
| 35 | uint32_t tmp; |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame^] | 36 | if (!PyUnicode_Check(v) || PyUnicode_GetLength(v) != 4) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | PyErr_SetString(PyExc_TypeError, |
| 38 | "OSType arg must be string of 4 chars"); |
| 39 | return 0; |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 40 | } |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 41 | memcpy((char *)&tmp, _PyUnicode_AsString(v), 4); |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 42 | *pr = (OSType)ntohl(tmp); |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | static PyObject * |
| 47 | gestalt_gestalt(PyObject *self, PyObject *args) |
| 48 | { |
| 49 | OSErr iErr; |
| 50 | OSType selector; |
| 51 | SInt32 response; |
| 52 | if (!PyArg_ParseTuple(args, "O&", convert_to_OSType, &selector)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | return NULL; |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 54 | iErr = Gestalt(selector, &response); |
| 55 | if (iErr != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | PyErr_SetString(PyExc_OSError, |
| 57 | "non-zero exit code!"); |
| 58 | return NULL; |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 59 | } |
| 60 | return PyLong_FromLong(response); |
| 61 | } |
| 62 | |
| 63 | static struct PyMethodDef gestalt_methods[] = { |
| 64 | {"gestalt", gestalt_gestalt, METH_VARARGS}, |
| 65 | {NULL, NULL} /* Sentinel */ |
| 66 | }; |
| 67 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 68 | static struct PyModuleDef gestaltmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | PyModuleDef_HEAD_INIT, |
| 70 | "_gestalt", |
| 71 | NULL, |
| 72 | -1, |
| 73 | gestalt_methods, |
| 74 | NULL, |
| 75 | NULL, |
| 76 | NULL, |
| 77 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Benjamin Peterson | af488af | 2008-06-12 13:16:38 +0000 | [diff] [blame] | 80 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 81 | PyInit__gestalt(void) |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 82 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | return PyModule_Create(&gestaltmodule); |
Benjamin Peterson | ebacd26 | 2008-05-29 21:09:51 +0000 | [diff] [blame] | 84 | } |