Back to index

python3.2  3.2.2
Defines | Functions | Variables
getpathp.c File Reference
#include "Python.h"
#include "osdefs.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

Go to the source code of this file.

Defines

#define LANDMARK   "lib\\os.py"

Functions

static int is_sep (char ch)
static void reduce (char *dir)
static int exists (char *filename)
static void join (char *buffer, char *stuff)
static int search_for_prefix (char *argv0_path, char *landmark)
static void get_progpath (void)
static void calculate_path (void)
char * Py_GetPath (void)
char * Py_GetPrefix (void)
char * Py_GetExecPrefix (void)
char * Py_GetProgramFullPath (void)

Variables

static char prefix [MAXPATHLEN+1]
static char exec_prefix [MAXPATHLEN+1]
static char progpath [MAXPATHLEN+1]
static char * module_search_path = NULL

Define Documentation

#define LANDMARK   "lib\\os.py"

Definition at line 48 of file getpathp.c.


Function Documentation

static void calculate_path ( void  ) [static]

Definition at line 297 of file getpathp.c.

{
    char argv0_path[MAXPATHLEN+1];
    char *buf;
    int bufsz;
    char *pythonhome = Py_GetPythonHome();
    char *envpath = Py_GETENV("PYTHONPATH");
#ifdef MS_WIN32
    char *machinepath, *userpath;

    /* Are we running under Windows 3.1(1) Win32s? */
    if (PyWin_IsWin32s()) {
        /* Only CLASSES_ROOT is supported */
        machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
        userpath = NULL;
    } else {
        machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
        userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
    }
#endif

    get_progpath();
    strcpy(argv0_path, progpath);
    reduce(argv0_path);
    if (pythonhome == NULL || *pythonhome == '\0') {
        if (search_for_prefix(argv0_path, LANDMARK))
            pythonhome = prefix;
        else
            pythonhome = NULL;
    }
    else {
    char *delim;

        strcpy(prefix, pythonhome);

    /* Extract Any Optional Trailing EXEC_PREFIX */
    /* e.g. PYTHONHOME=<prefix>:<exec_prefix>   */
    delim = strchr(prefix, DELIM);
    if (delim) {
        *delim = '\0';
        strcpy(exec_prefix, delim+1);
    } else
        strcpy(exec_prefix, EXEC_PREFIX);
    }

    if (envpath && *envpath == '\0')
        envpath = NULL;

    /* We need to construct a path from the following parts:
       (1) the PYTHONPATH environment variable, if set;
       (2) for Win32, the machinepath and userpath, if set;
       (3) the PYTHONPATH config macro, with the leading "."
           of each component replaced with pythonhome, if set;
       (4) the directory containing the executable (argv0_path).
       The length calculation calculates #3 first.
    */

    /* Calculate size of return buffer */
    if (pythonhome != NULL) {
        char *p;
        bufsz = 1;
        for (p = PYTHONPATH; *p; p++) {
            if (*p == DELIM)
                bufsz++; /* number of DELIM plus one */
        }
        bufsz *= strlen(pythonhome);
    }
    else
        bufsz = 0;
    bufsz += strlen(PYTHONPATH) + 1;
    if (envpath != NULL)
        bufsz += strlen(envpath) + 1;
    bufsz += strlen(argv0_path) + 1;
#ifdef MS_WIN32
    if (machinepath)
        bufsz += strlen(machinepath) + 1;
    if (userpath)
        bufsz += strlen(userpath) + 1;
#endif

    module_search_path = buf = malloc(bufsz);
    if (buf == NULL) {
        /* We can't exit, so print a warning and limp along */
        fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
        if (envpath) {
            fprintf(stderr, "Using default static $PYTHONPATH.\n");
            module_search_path = envpath;
        }
        else {
            fprintf(stderr, "Using environment $PYTHONPATH.\n");
            module_search_path = PYTHONPATH;
        }
        return;
    }

    if (envpath) {
        strcpy(buf, envpath);
        buf = strchr(buf, '\0');
        *buf++ = DELIM;
    }
#ifdef MS_WIN32
    if (machinepath) {
        strcpy(buf, machinepath);
        buf = strchr(buf, '\0');
        *buf++ = DELIM;
    }
    if (userpath) {
        strcpy(buf, userpath);
        buf = strchr(buf, '\0');
        *buf++ = DELIM;
    }
#endif
    if (pythonhome == NULL) {
        strcpy(buf, PYTHONPATH);
        buf = strchr(buf, '\0');
    }
    else {
        char *p = PYTHONPATH;
        char *q;
        int n;
        for (;;) {
            q = strchr(p, DELIM);
            if (q == NULL)
                n = strlen(p);
            else
                n = q-p;
            if (p[0] == '.' && is_sep(p[1])) {
                strcpy(buf, pythonhome);
                buf = strchr(buf, '\0');
                p++;
                n--;
            }
            strncpy(buf, p, n);
            buf += n;
            if (q == NULL)
                break;
            *buf++ = DELIM;
            p = q+1;
        }
    }
    if (argv0_path) {
        *buf++ = DELIM;
        strcpy(buf, argv0_path);
        buf = strchr(buf, '\0');
    }
    *buf = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int exists ( char *  filename) [static]

Definition at line 79 of file getpathp.c.

{
    struct stat buf;
    return stat(filename, &buf) == 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void get_progpath ( void  ) [static]

Definition at line 245 of file getpathp.c.

{
    extern char *Py_GetProgramName(void);
    char *path = getenv("PATH");
    char *prog = Py_GetProgramName();

#ifdef MS_WIN32
    if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
        return;
#endif
    if (prog == NULL || *prog == '\0')
        prog = "python";

    /* If there is no slash in the argv0 path, then we have to
     * assume python is on the user's $PATH, since there's no
     * other way to find a directory to start the search from.  If
     * $PATH isn't exported, you lose.
     */
#ifdef ALTSEP
    if (strchr(prog, SEP) || strchr(prog, ALTSEP))
#else
    if (strchr(prog, SEP))
#endif
        strcpy(progpath, prog);
    else if (path) {
        while (1) {
            char *delim = strchr(path, DELIM);

            if (delim) {
                int len = delim - path;
                strncpy(progpath, path, len);
                *(progpath + len) = '\0';
            }
            else
                strcpy(progpath, path);

            join(progpath, prog);
            if (exists(progpath))
                break;

            if (!delim) {
                progpath[0] = '\0';
                break;
            }
            path = delim + 1;
        }
    }
    else
        progpath[0] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int is_sep ( char  ch) [static]

Definition at line 58 of file getpathp.c.

{
#ifdef ALTSEP
    return ch == SEP || ch == ALTSEP;
#else
    return ch == SEP;
#endif
}

Here is the caller graph for this function:

static void join ( char *  buffer,
char *  stuff 
) [static]

Definition at line 96 of file getpathp.c.

{
    int n, k;
    if (is_sep(stuff[0]))
        n = 0;
    else {
        n = strlen(buffer);
        if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
            buffer[n++] = SEP;
    }
    if (n > MAXPATHLEN)
        Py_FatalError("buffer overflow in getpathp.c's joinpath()");
    k = strlen(stuff);
    if (n + k > MAXPATHLEN)
        k = MAXPATHLEN - n;
    strncpy(buffer+n, stuff, k);
    buffer[n+k] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* Py_GetExecPrefix ( void  )

Definition at line 467 of file getpathp.c.

Here is the call graph for this function:

char* Py_GetPath ( void  )

Definition at line 449 of file getpathp.c.

Here is the call graph for this function:

char* Py_GetPrefix ( void  )

Definition at line 458 of file getpathp.c.

Here is the call graph for this function:

Definition at line 476 of file getpathp.c.

Here is the call graph for this function:

static void reduce ( char *  dir) [static]

Definition at line 69 of file getpathp.c.

{
    int i = strlen(dir);
    while (i > 0 && !is_sep(dir[i]))
        --i;
    dir[i] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int search_for_prefix ( char *  argv0_path,
char *  landmark 
) [static]

Definition at line 117 of file getpathp.c.

{
    int n;

    /* Search from argv0_path, until root is found */
    strcpy(prefix, argv0_path);
    do {
        n = strlen(prefix);
        join(prefix, landmark);
        if (exists(prefix)) {
            prefix[n] = '\0';
            return 1;
        }
        prefix[n] = '\0';
        reduce(prefix);
    } while (prefix[0]);
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char exec_prefix[MAXPATHLEN+1] [static]

Definition at line 52 of file getpathp.c.

char* module_search_path = NULL [static]

Definition at line 54 of file getpathp.c.

char prefix[MAXPATHLEN+1] [static]

Definition at line 51 of file getpathp.c.

char progpath[MAXPATHLEN+1] [static]

Definition at line 53 of file getpathp.c.