blob: d106068dcedf3fa4abf5e6628ccb197b001d3706 [file] [log] [blame]
Benjamin Petersonebacd262008-05-29 21:09:51 +00001/***********************************************************
2Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF 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 */
32static int
33convert_to_OSType(PyObject *v, OSType *pr)
34{
35 uint32_t tmp;
36 if (!PyUnicode_Check(v) || PyUnicode_GetSize(v) != 4) {
37 PyErr_SetString(PyExc_TypeError,
38 "OSType arg must be string of 4 chars");
39 return 0;
40 }
41 memcpy((char *)&tmp, PyUnicode_AsString(v), 4);
42 *pr = (OSType)ntohl(tmp);
43 return 1;
44}
45
46static PyObject *
47gestalt_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))
53 return NULL;
54 iErr = Gestalt(selector, &response);
55 if (iErr != 0) {
56 PyErr_SetString(PyExc_OSError,
57 "non-zero exit code!");
58 return NULL;
59 }
60 return PyLong_FromLong(response);
61}
62
63static struct PyMethodDef gestalt_methods[] = {
64 {"gestalt", gestalt_gestalt, METH_VARARGS},
65 {NULL, NULL} /* Sentinel */
66};
67
68void
69init_gestalt(void)
70{
71 Py_InitModule("_gestalt", gestalt_methods);
72}