pythonw.c 5.84 KB
Newer Older
1 2 3 4 5
/*
 * This wrapper program executes a python executable hidden inside an
 * application bundle inside the Python framework. This is needed to run
 * GUI code: some GUI API's don't work unless the program is inside an
 * application bundle.
6 7 8
 *
 * This program uses posix_spawn rather than plain execv because we need
 * slightly more control over how the "real" interpreter is executed.
9 10 11
 *
 * On OSX 10.4 (and earlier) this falls back to using exec because the
 * posix_spawnv functions aren't available there.
12
 */
13 14 15 16 17 18 19

#pragma weak_import posix_spawnattr_init
#pragma weak_import posix_spawnattr_setbinpref_np
#pragma weak_import posix_spawnattr_setflags
#pragma weak_import posix_spawn

#include <Python.h>
20
#include <unistd.h>
21
#ifdef HAVE_SPAWN_H
22
#include <spawn.h>
23
#endif
24 25 26
#include <stdio.h>
#include <string.h>
#include <errno.h>
27
#include <err.h>
28 29 30
#include <dlfcn.h>
#include <stdlib.h>
#include <Python.h>
31
#include <mach-o/dyld.h>
32 33


34 35 36 37 38 39 40 41 42
extern char** environ;

/*
 * Locate the python framework by looking for the
 * library that contains Py_Initialize.
 *
 * In a regular framework the structure is:
 *
 *    Python.framework/Versions/2.7
43 44
 *              /Python
 *              /Resources/Python.app/Contents/MacOS/Python
45 46 47 48 49 50 51 52 53
 *
 * In a virtualenv style structure the expected
 * structure is:
 *
 *    ROOT
 *       /bin/pythonw
 *       /.Python   <- the dylib
 *       /.Resources/Python.app/Contents/MacOS/Python
 *
54
 * NOTE: virtualenv's are not an officially supported
55 56 57 58 59
 * feature, support for that structure is provided as
 * a convenience.
 */
static char* get_python_path(void)
{
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    size_t len;
    Dl_info info;
    char* end;
    char* g_path;

    if (dladdr(Py_Initialize, &info) == 0) {
        return NULL;
    }

    len = strlen(info.dli_fname);

    g_path = malloc(len+60);
    if (g_path == NULL) {
        return NULL;
    }

    strcpy(g_path, info.dli_fname);
    end = g_path + len - 1;
    while (end != g_path && *end != '/') {
        end --;
    }
    end++;
    if (*end == '.') {
        end++;
    }
    strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK);

    return g_path;
88 89
}

90
#ifdef HAVE_SPAWN_H
91 92 93
static void
setup_spawnattr(posix_spawnattr_t* spawnattr)
{
94 95 96 97
    size_t ocount;
    size_t count;
    cpu_type_t cpu_types[1];
    short flags = 0;
98
#ifdef __LP64__
99
    int   ch;
100 101
#endif

102 103 104 105
    if ((errno = posix_spawnattr_init(spawnattr)) != 0) {
        err(2, "posix_spawnattr_int");
        /* NOTREACHTED */
    }
106

107
    count = 1;
108

Ezio Melotti's avatar
Ezio Melotti committed
109 110
    /* Run the real python executable using the same architecture as this
     * executable, this allows users to control the architecture using
111 112
     * "arch -ppc python"
     */
113 114

#if defined(__ppc64__)
115
    cpu_types[0] = CPU_TYPE_POWERPC64;
116 117

#elif defined(__x86_64__)
118
    cpu_types[0] = CPU_TYPE_X86_64;
119 120

#elif defined(__ppc__)
121
    cpu_types[0] = CPU_TYPE_POWERPC;
122
#elif defined(__i386__)
123
    cpu_types[0] = CPU_TYPE_X86;
124
#else
125
#       error "Unknown CPU"
126 127
#endif

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    if (posix_spawnattr_setbinpref_np(spawnattr, count,
                            cpu_types, &ocount) == -1) {
        err(1, "posix_spawnattr_setbinpref");
        /* NOTREACHTED */
    }
    if (count != ocount) {
        fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n");
        exit(1);
        /* NOTREACHTED */
    }


    /*
     * Set flag that causes posix_spawn to behave like execv
     */
    flags |= POSIX_SPAWN_SETEXEC;
    if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) {
        err(1, "posix_spawnattr_setflags");
        /* NOTREACHTED */
    }
148
}
149
#endif
150

151
int
152
main(int argc, char **argv) {
153
    char* exec_path = get_python_path();
154 155 156 157 158 159 160 161
    static char path[PATH_MAX * 2];
    static char real_path[PATH_MAX * 2];
    int status;
    uint32_t size = PATH_MAX * 2;

    /* Set the original executable path in the environment. */
    status = _NSGetExecutablePath(path, &size);
    if (status == 0) {
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        /*
         * Note: don't call 'realpath', that will
         * erase symlink information, and that
         * breaks "pyvenv --symlink"
         *
         * It is nice to have the directory name
         * as a cleaned up absolute path though,
         * therefore call realpath on dirname(path)
         */
        char* slash = strrchr(path, '/');
        if (slash) {
            char  replaced;
            replaced = slash[1];
            slash[1] = 0;
            if (realpath(path, real_path) == NULL) {
                err(1, "realpath: %s", path);
            }
            slash[1] = replaced;
            if (strlcat(real_path, slash, sizeof(real_path)) > sizeof(real_path)) {
                errno = EINVAL;
                err(1, "realpath: %s", path);
            }

        } else {
            if (realpath(".", real_path) == NULL) {
                err(1, "realpath: %s", path);
            }
            if (strlcat(real_path, "/", sizeof(real_path)) > sizeof(real_path)) {
                errno = EINVAL;
                err(1, "realpath: %s", path);
            }
            if (strlcat(real_path, path, sizeof(real_path)) > sizeof(real_path)) {
                errno = EINVAL;
                err(1, "realpath: %s", path);
            }
197
        }
198 199

        setenv("__PYVENV_LAUNCHER__", real_path, 1);
200
    }
201

202 203 204 205 206 207 208 209
    /*
     * Let argv[0] refer to the new interpreter. This is needed to
     * get the effect we want on OSX 10.5 or earlier. That is, without
     * changing argv[0] the real interpreter won't have access to
     * the Window Server.
     */
    argv[0] = exec_path;

210
#ifdef HAVE_SPAWN_H
211 212 213 214 215 216 217 218 219 220 221
    /* We're weak-linking to posix-spawnv to ensure that
     * an executable build on 10.5 can work on 10.4.
     */
    if (posix_spawn != NULL) {
        posix_spawnattr_t spawnattr = NULL;

        setup_spawnattr(&spawnattr);
        posix_spawn(NULL, exec_path, NULL,
            &spawnattr, argv, environ);
        err(1, "posix_spawn: %s", exec_path);
    }
222
#endif
223 224 225
    execve(exec_path, argv, environ);
    err(1, "execve: %s", argv[0]);
    /* NOTREACHED */
226
}