2 * This wrapper program executes a python executable hidden inside an
3 * application bundle inside the Python framework. This is needed to run
4 * GUI code: some GUI API's don't work unless the program is inside an
7 * This program uses posix_spawn rather than plain execv because we need
8 * slightly more control over how the "real" interpreter is executed.
10 * On OSX 10.4 (and earlier) this falls back to using exec because the
11 * posix_spawnv functions aren't available there.
13 #pragma weak_import posix_spawnattr_init
14 #pragma weak_import posix_spawnattr_setbinpref_np
15 #pragma weak_import posix_spawnattr_setflags
16 #pragma weak_import posix_spawn
31 extern char** environ;
34 * Locate the python framework by looking for the
35 * library that contains Py_Initialize.
37 * In a regular framework the structure is:
39 * Python.framework/Versions/2.7
41 * /Resources/Python.app/Contents/MacOS/Python
43 * In a virtualenv style structure the expected
48 * /.Python <- the dylib
49 * /.Resources/Python.app/Contents/MacOS/Python
51 * NOTE: virtualenv's are not an officially supported
52 * feature, support for that structure is provided as
55 static char* get_python_path(void)
62 if (dladdr(Py_Initialize, &info) == 0) {
66 len = strlen(info.dli_fname);
68 g_path = malloc(len+60);
73 strcpy(g_path, info.dli_fname);
74 end = g_path + len - 1;
75 while (end != g_path && *end != '/') {
82 strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK);
89 setup_spawnattr(posix_spawnattr_t* spawnattr)
93 cpu_type_t cpu_types[1];
99 if ((errno = posix_spawnattr_init(spawnattr)) != 0) {
100 err(2, "posix_spawnattr_int");
106 /* Run the real python executable using the same architecture as this
107 * executable, this allows users to control the architecture using
111 #if defined(__ppc64__)
112 cpu_types[0] = CPU_TYPE_POWERPC64;
114 #elif defined(__x86_64__)
115 cpu_types[0] = CPU_TYPE_X86_64;
117 #elif defined(__ppc__)
118 cpu_types[0] = CPU_TYPE_POWERPC;
119 #elif defined(__i386__)
120 cpu_types[0] = CPU_TYPE_X86;
122 # error "Unknown CPU"
125 if (posix_spawnattr_setbinpref_np(spawnattr, count,
126 cpu_types, &ocount) == -1) {
127 err(1, "posix_spawnattr_setbinpref");
130 if (count != ocount) {
131 fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n");
138 * Set flag that causes posix_spawn to behave like execv
140 flags |= POSIX_SPAWN_SETEXEC;
141 if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) {
142 err(1, "posix_spawnattr_setflags");
149 main(int argc, char **argv) {
150 char* exec_path = get_python_path();
153 * Let argv[0] refer to the new interpreter. This is needed to
154 * get the effect we want on OSX 10.5 or earlier. That is, without
155 * changing argv[0] the real interpreter won't have access to
162 /* We're weak-linking to posix-spawnv to ensure that
163 * an executable build on 10.5 can work on 10.4.
165 if (posix_spawn != NULL) {
166 posix_spawnattr_t spawnattr = NULL;
169 setup_spawnattr(&spawnattr);
170 posix_spawn(NULL, exec_path, NULL,
171 &spawnattr, argv, environ);
172 err(1, "posix_spawn: %s", exec_path);
175 execve(exec_path, argv, environ);
176 err(1, "execve: %s", argv[0]);