Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebacd26

Browse files
committed
add the gestalt module back as _gestalt
1 parent 7dca3eb commit ebacd26

3 files changed

Lines changed: 83 additions & 5 deletions

File tree

Lib/platform.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,14 +676,13 @@ def win32_ver(release='',version='',csd='',ptype=''):
676676

677677
def _mac_ver_lookup(selectors,default=None):
678678

679-
from gestalt import gestalt
680-
import MacOS
679+
from _gestalt import gestalt
681680
l = []
682681
append = l.append
683682
for selector in selectors:
684683
try:
685684
append(gestalt(selector))
686-
except (RuntimeError, MacOS.Error):
685+
except (RuntimeError, OSError):
687686
append(default)
688687
return l
689688

@@ -709,8 +708,7 @@ def mac_ver(release='',versioninfo=('','',''),machine=''):
709708
"""
710709
# Check whether the version info module is available
711710
try:
712-
import gestalt
713-
import MacOS
711+
import _gestalt
714712
except ImportError:
715713
return release,versioninfo,machine
716714
# Get the infos

Modules/_gestalt.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/***********************************************************
2+
Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
3+
The Netherlands.
4+
5+
All Rights Reserved
6+
7+
Permission to use, copy, modify, and distribute this software and its
8+
documentation for any purpose and without fee is hereby granted,
9+
provided that the above copyright notice appear in all copies and that
10+
both that copyright notice and this permission notice appear in
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;
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+
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))
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+
63+
static struct PyMethodDef gestalt_methods[] = {
64+
{"gestalt", gestalt_gestalt, METH_VARARGS},
65+
{NULL, NULL} /* Sentinel */
66+
};
67+
68+
void
69+
init_gestalt(void)
70+
{
71+
Py_InitModule("_gestalt", gestalt_methods);
72+
}

setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,14 @@ class db_found(Exception): pass
11181118
else:
11191119
missing.append('ossaudiodev')
11201120

1121+
if sys.platform == 'darwin':
1122+
exts.append(
1123+
Extension('_gestalt', ['_gestalt.c'],
1124+
extra_link_args=['-framework', 'Carbon'])
1125+
)
1126+
else:
1127+
missing.append('_gestalt')
1128+
11211129
self.extensions.extend(exts)
11221130

11231131
# Call the method for detecting whether _tkinter can be compiled

0 commit comments

Comments
 (0)