Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/PC/os2emx/getpathp.c

    r2 r391  
    55/* ----------------------------------------------------------------
    66   PATH RULES FOR OS/2+EMX:
    7    This describes how sys.path is formed on OS/2+EMX.  It describes the 
    8    functionality, not the implementation (ie, the order in which these 
     7   This describes how sys.path is formed on OS/2+EMX.  It describes the
     8   functionality, not the implementation (ie, the order in which these
    99   are actually fetched is different)
    1010
     
    1717     is set, we believe it.  Otherwise, we use the path of our host .EXE's
    1818     to try and locate our "landmark" (lib\\os.py) and deduce our home.
    19      - If we DO have a Python Home: The relevant sub-directories (Lib, 
     19     - If we DO have a Python Home: The relevant sub-directories (Lib,
    2020       plat-win, lib-tk, etc) are based on the Python Home
    2121     - If we DO NOT have a Python Home, the core Python Path is
    22        loaded from the registry.  This is the main PythonPath key, 
     22       loaded from the registry.  This is the main PythonPath key,
    2323       and both HKLM and HKCU are combined to form the path)
    2424
     
    3333    the core path is deduced.
    3434
    35   * When Python is hosted in another exe (different directory, embedded via 
     35  * When Python is hosted in another exe (different directory, embedded via
    3636    COM, etc), the Python Home will not be deduced, so the core path from
    37     the registry is used.  Other "application paths "in the registry are 
     37    the registry is used.  Other "application paths "in the registry are
    3838    always read.
    3939
     
    8686
    8787static int
    88 is_sep(char ch) /* determine if "ch" is a separator character */
     88is_sep(char ch) /* determine if "ch" is a separator character */
    8989{
    9090#ifdef ALTSEP
    91         return ch == SEP || ch == ALTSEP;
     91    return ch == SEP || ch == ALTSEP;
    9292#else
    93         return ch == SEP;
     93    return ch == SEP;
    9494#endif
    9595}
     
    101101reduce(char *dir)
    102102{
    103         size_t i = strlen(dir);
    104         while (i > 0 && !is_sep(dir[i]))
    105                 --i;
    106         dir[i] = '\0';
    107 }
    108        
     103    size_t i = strlen(dir);
     104    while (i > 0 && !is_sep(dir[i]))
     105        --i;
     106    dir[i] = '\0';
     107}
     108
    109109static int
    110110exists(char *filename)
    111111{
    112         struct stat buf;
    113         return stat(filename, &buf) == 0;
     112    struct stat buf;
     113    return stat(filename, &buf) == 0;
    114114}
    115115
    116116/* Is module  (check for .pyc/.pyo too)
    117  * Assumes 'filename' MAXPATHLEN+1 bytes long - 
     117 * Assumes 'filename' MAXPATHLEN+1 bytes long -
    118118 * may extend 'filename' by one character.
    119119 */
     
    121121ismodule(char *filename)
    122122{
    123         if (exists(filename))
    124                 return 1;
    125 
    126         /* Check for the compiled version of prefix. */
    127         if (strlen(filename) < MAXPATHLEN) {
    128                 strcat(filename, Py_OptimizeFlag ? "o" : "c");
    129                 if (exists(filename))
    130                         return 1;
    131         }
    132         return 0;
     123    if (exists(filename))
     124        return 1;
     125
     126    /* Check for the compiled version of prefix. */
     127    if (strlen(filename) < MAXPATHLEN) {
     128        strcat(filename, Py_OptimizeFlag ? "o" : "c");
     129        if (exists(filename))
     130            return 1;
     131    }
     132    return 0;
    133133}
    134134
     
    146146join(char *buffer, char *stuff)
    147147{
    148         size_t n, k;
    149         if (is_sep(stuff[0]))
    150                 n = 0;
    151         else {
    152                 n = strlen(buffer);
    153                 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
    154                         buffer[n++] = SEP;
    155         }
    156         if (n > MAXPATHLEN)
    157                 Py_FatalError("buffer overflow in getpathp.c's joinpath()");
    158         k = strlen(stuff);
    159         if (n + k > MAXPATHLEN)
    160                 k = MAXPATHLEN - n;
    161         strncpy(buffer+n, stuff, k);
    162         buffer[n+k] = '\0';
     148    size_t n, k;
     149    if (is_sep(stuff[0]))
     150        n = 0;
     151    else {
     152        n = strlen(buffer);
     153        if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
     154            buffer[n++] = SEP;
     155    }
     156    if (n > MAXPATHLEN)
     157        Py_FatalError("buffer overflow in getpathp.c's joinpath()");
     158    k = strlen(stuff);
     159    if (n + k > MAXPATHLEN)
     160        k = MAXPATHLEN - n;
     161    strncpy(buffer+n, stuff, k);
     162    buffer[n+k] = '\0';
    163163}
    164164
     
    170170gotlandmark(char *landmark)
    171171{
    172         int n, ok;
    173 
    174         n = strlen(prefix);
    175         join(prefix, landmark);
    176         ok = ismodule(prefix);
    177         prefix[n] = '\0';
    178         return ok;
    179 }
    180 
    181 /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd. 
     172    int n, ok;
     173
     174    n = strlen(prefix);
     175    join(prefix, landmark);
     176    ok = ismodule(prefix);
     177    prefix[n] = '\0';
     178    return ok;
     179}
     180
     181/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
    182182 * assumption provided by only caller, calculate_path()
    183183 */
     
    185185search_for_prefix(char *argv0_path, char *landmark)
    186186{
    187         /* Search from argv0_path, until landmark is found */
    188         strcpy(prefix, argv0_path);
    189         do {
    190                 if (gotlandmark(landmark))
    191                         return 1;
    192                 reduce(prefix);
    193         } while (prefix[0]);
    194         return 0;
     187    /* Search from argv0_path, until landmark is found */
     188    strcpy(prefix, argv0_path);
     189    do {
     190        if (gotlandmark(landmark))
     191            return 1;
     192        reduce(prefix);
     193    } while (prefix[0]);
     194    return 0;
    195195}
    196196
     
    199199get_progpath(void)
    200200{
    201         extern char *Py_GetProgramName(void);
    202         char *path = getenv("PATH");
    203         char *prog = Py_GetProgramName();
    204 
    205         PPIB pib;
    206         if ((DosGetInfoBlocks(NULL, &pib) == 0) &&
    207             (DosQueryModuleName(pib->pib_hmte, sizeof(progpath), progpath) == 0))
    208                 return;
    209 
    210         if (prog == NULL || *prog == '\0')
    211                 prog = "python";
    212 
    213         /* If there is no slash in the argv0 path, then we have to
    214         * assume python is on the user's $PATH, since there's no
    215         * other way to find a directory to start the search from.  If
    216         * $PATH isn't exported, you lose.
    217         */
     201    extern char *Py_GetProgramName(void);
     202    char *path = getenv("PATH");
     203    char *prog = Py_GetProgramName();
     204
     205    PPIB pib;
     206    if ((DosGetInfoBlocks(NULL, &pib) == 0) &&
     207        (DosQueryModuleName(pib->pib_hmte, sizeof(progpath), progpath) == 0))
     208        return;
     209
     210    if (prog == NULL || *prog == '\0')
     211        prog = "python";
     212
     213    /* If there is no slash in the argv0 path, then we have to
     214    * assume python is on the user's $PATH, since there's no
     215    * other way to find a directory to start the search from.  If
     216    * $PATH isn't exported, you lose.
     217    */
    218218#ifdef ALTSEP
    219         if (strchr(prog, SEP) || strchr(prog, ALTSEP))
     219    if (strchr(prog, SEP) || strchr(prog, ALTSEP))
    220220#else
    221         if (strchr(prog, SEP))
    222 #endif
    223                 strncpy(progpath, prog, MAXPATHLEN);
    224         else if (path) {
    225                 while (1) {
    226                         char *delim = strchr(path, DELIM);
    227 
    228                         if (delim) {
    229                                 size_t len = delim - path;
    230                                 /* ensure we can't overwrite buffer */
     221    if (strchr(prog, SEP))
     222#endif
     223        strncpy(progpath, prog, MAXPATHLEN);
     224    else if (path) {
     225        while (1) {
     226            char *delim = strchr(path, DELIM);
     227
     228            if (delim) {
     229                size_t len = delim - path;
     230                /* ensure we can't overwrite buffer */
    231231#if !defined(PYCC_GCC)
    232                                 len = min(MAXPATHLEN,len);
     232                len = min(MAXPATHLEN,len);
    233233#else
    234                                 len = MAXPATHLEN < len ? MAXPATHLEN : len;
    235 #endif
    236                                 strncpy(progpath, path, len);
    237                                 *(progpath + len) = '\0';
    238                         }
    239                         else
    240                                 strncpy(progpath, path, MAXPATHLEN);
    241 
    242                         /* join() is safe for MAXPATHLEN+1 size buffer */
    243                         join(progpath, prog);
    244                         if (exists(progpath))
    245                                 break;
    246 
    247                         if (!delim) {
    248                                 progpath[0] = '\0';
    249                                 break;
    250                         }
    251                         path = delim + 1;
    252                 }
    253         }
    254         else
    255                 progpath[0] = '\0';
     234                len = MAXPATHLEN < len ? MAXPATHLEN : len;
     235#endif
     236                strncpy(progpath, path, len);
     237                *(progpath + len) = '\0';
     238            }
     239            else
     240                strncpy(progpath, path, MAXPATHLEN);
     241
     242            /* join() is safe for MAXPATHLEN+1 size buffer */
     243            join(progpath, prog);
     244            if (exists(progpath))
     245                break;
     246
     247            if (!delim) {
     248                progpath[0] = '\0';
     249                break;
     250            }
     251            path = delim + 1;
     252        }
     253    }
     254    else
     255        progpath[0] = '\0';
    256256}
    257257
     
    259259calculate_path(void)
    260260{
    261         char argv0_path[MAXPATHLEN+1];
    262         char *buf;
    263         size_t bufsz;
    264         char *pythonhome = Py_GetPythonHome();
    265         char *envpath = getenv("PYTHONPATH");
    266         char zip_path[MAXPATHLEN+1];
    267         size_t len;
    268 
    269         get_progpath();
    270         /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
    271         strcpy(argv0_path, progpath);
    272         reduce(argv0_path);
    273         if (pythonhome == NULL || *pythonhome == '\0') {
    274                 if (search_for_prefix(argv0_path, LANDMARK))
    275                         pythonhome = prefix;
    276                 else
    277                         pythonhome = NULL;
    278         }
    279         else
    280                 strncpy(prefix, pythonhome, MAXPATHLEN);
    281 
    282         if (envpath && *envpath == '\0')
    283                 envpath = NULL;
    284 
    285         /* Calculate zip archive path */
    286         strncpy(zip_path, progpath, MAXPATHLEN);
    287         zip_path[MAXPATHLEN] = '\0';
    288         len = strlen(zip_path);
    289         if (len > 4) {
    290                 zip_path[len-3] = 'z';  /* change ending to "zip" */
    291                 zip_path[len-2] = 'i';
    292                 zip_path[len-1] = 'p';
    293         }
    294         else {
    295                 zip_path[0] = 0;
    296         }
    297 
    298         /* We need to construct a path from the following parts.
    299         * (1) the PYTHONPATH environment variable, if set;
    300         * (2) the zip archive file path;
    301         * (3) the PYTHONPATH config macro, with the leading "."
    302         *     of each component replaced with pythonhome, if set;
    303         * (4) the directory containing the executable (argv0_path).
    304         * The length calculation calculates #3 first.
    305         */
    306 
    307         /* Calculate size of return buffer */
    308         if (pythonhome != NULL) {
    309                 char *p;
    310                 bufsz = 1;     
    311                 for (p = PYTHONPATH; *p; p++) {
    312                         if (*p == DELIM)
    313                                 bufsz++; /* number of DELIM plus one */
    314                 }
    315                 bufsz *= strlen(pythonhome);
    316         }
    317         else
    318                 bufsz = 0;
    319         bufsz += strlen(PYTHONPATH) + 1;
    320         bufsz += strlen(argv0_path) + 1;
    321         bufsz += strlen(zip_path) + 1;
    322         if (envpath != NULL)
    323                 bufsz += strlen(envpath) + 1;
    324 
    325         module_search_path = buf = malloc(bufsz);
    326         if (buf == NULL) {
    327                 /* We can't exit, so print a warning and limp along */
    328                 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
    329                 if (envpath) {
    330                         fprintf(stderr, "Using environment $PYTHONPATH.\n");
    331                         module_search_path = envpath;
    332                 }
    333                 else {
    334                         fprintf(stderr, "Using default static path.\n");
    335                         module_search_path = PYTHONPATH;
    336                 }
    337                 return;
    338         }
    339 
    340         if (envpath) {
    341                 strcpy(buf, envpath);
    342                 buf = strchr(buf, '\0');
    343                 *buf++ = DELIM;
    344         }
    345         if (zip_path[0]) {
    346                 strcpy(buf, zip_path);
    347                 buf = strchr(buf, '\0');
    348                 *buf++ = DELIM;
    349         }
    350 
    351         if (pythonhome == NULL) {
    352                 strcpy(buf, PYTHONPATH);
    353                 buf = strchr(buf, '\0');
    354         }
    355         else {
    356                 char *p = PYTHONPATH;
    357                 char *q;
    358                 size_t n;
    359                 for (;;) {
    360                         q = strchr(p, DELIM);
    361                         if (q == NULL)
    362                                 n = strlen(p);
    363                         else
    364                                 n = q-p;
    365                         if (p[0] == '.' && is_sep(p[1])) {
    366                                 strcpy(buf, pythonhome);
    367                                 buf = strchr(buf, '\0');
    368                                 p++;
    369                                 n--;
    370                         }
    371                         strncpy(buf, p, n);
    372                         buf += n;
    373                         if (q == NULL)
    374                                 break;
    375                         *buf++ = DELIM;
    376                         p = q+1;
    377                 }
    378         }
    379         if (argv0_path) {
    380                 *buf++ = DELIM;
    381                 strcpy(buf, argv0_path);
    382                 buf = strchr(buf, '\0');
    383         }
    384         *buf = '\0';
     261    char argv0_path[MAXPATHLEN+1];
     262    char *buf;
     263    size_t bufsz;
     264    char *pythonhome = Py_GetPythonHome();
     265    char *envpath = getenv("PYTHONPATH");
     266    char zip_path[MAXPATHLEN+1];
     267    size_t len;
     268
     269    get_progpath();
     270    /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
     271    strcpy(argv0_path, progpath);
     272    reduce(argv0_path);
     273    if (pythonhome == NULL || *pythonhome == '\0') {
     274        if (search_for_prefix(argv0_path, LANDMARK))
     275            pythonhome = prefix;
     276        else
     277            pythonhome = NULL;
     278    }
     279    else
     280        strncpy(prefix, pythonhome, MAXPATHLEN);
     281
     282    if (envpath && *envpath == '\0')
     283        envpath = NULL;
     284
     285    /* Calculate zip archive path */
     286    strncpy(zip_path, progpath, MAXPATHLEN);
     287    zip_path[MAXPATHLEN] = '\0';
     288    len = strlen(zip_path);
     289    if (len > 4) {
     290        zip_path[len-3] = 'z';  /* change ending to "zip" */
     291        zip_path[len-2] = 'i';
     292        zip_path[len-1] = 'p';
     293    }
     294    else {
     295        zip_path[0] = 0;
     296    }
     297
     298    /* We need to construct a path from the following parts.
     299    * (1) the PYTHONPATH environment variable, if set;
     300    * (2) the zip archive file path;
     301    * (3) the PYTHONPATH config macro, with the leading "."
     302    *     of each component replaced with pythonhome, if set;
     303    * (4) the directory containing the executable (argv0_path).
     304    * The length calculation calculates #3 first.
     305    */
     306
     307    /* Calculate size of return buffer */
     308    if (pythonhome != NULL) {
     309        char *p;
     310        bufsz = 1;
     311        for (p = PYTHONPATH; *p; p++) {
     312            if (*p == DELIM)
     313                bufsz++; /* number of DELIM plus one */
     314        }
     315        bufsz *= strlen(pythonhome);
     316    }
     317    else
     318        bufsz = 0;
     319    bufsz += strlen(PYTHONPATH) + 1;
     320    bufsz += strlen(argv0_path) + 1;
     321    bufsz += strlen(zip_path) + 1;
     322    if (envpath != NULL)
     323        bufsz += strlen(envpath) + 1;
     324
     325    module_search_path = buf = malloc(bufsz);
     326    if (buf == NULL) {
     327        /* We can't exit, so print a warning and limp along */
     328        fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
     329        if (envpath) {
     330            fprintf(stderr, "Using environment $PYTHONPATH.\n");
     331            module_search_path = envpath;
     332        }
     333        else {
     334            fprintf(stderr, "Using default static path.\n");
     335            module_search_path = PYTHONPATH;
     336        }
     337        return;
     338    }
     339
     340    if (envpath) {
     341        strcpy(buf, envpath);
     342        buf = strchr(buf, '\0');
     343        *buf++ = DELIM;
     344    }
     345    if (zip_path[0]) {
     346        strcpy(buf, zip_path);
     347        buf = strchr(buf, '\0');
     348        *buf++ = DELIM;
     349    }
     350
     351    if (pythonhome == NULL) {
     352        strcpy(buf, PYTHONPATH);
     353        buf = strchr(buf, '\0');
     354    }
     355    else {
     356        char *p = PYTHONPATH;
     357        char *q;
     358        size_t n;
     359        for (;;) {
     360            q = strchr(p, DELIM);
     361            if (q == NULL)
     362                n = strlen(p);
     363            else
     364                n = q-p;
     365            if (p[0] == '.' && is_sep(p[1])) {
     366                strcpy(buf, pythonhome);
     367                buf = strchr(buf, '\0');
     368                p++;
     369                n--;
     370            }
     371            strncpy(buf, p, n);
     372            buf += n;
     373            if (q == NULL)
     374                break;
     375            *buf++ = DELIM;
     376            p = q+1;
     377        }
     378    }
     379    if (argv0_path) {
     380        *buf++ = DELIM;
     381        strcpy(buf, argv0_path);
     382        buf = strchr(buf, '\0');
     383    }
     384    *buf = '\0';
    385385}
    386386
     
    391391Py_GetPath(void)
    392392{
    393         if (!module_search_path)
    394                 calculate_path();
    395         return module_search_path;
     393    if (!module_search_path)
     394        calculate_path();
     395    return module_search_path;
    396396}
    397397
     
    399399Py_GetPrefix(void)
    400400{
    401         if (!module_search_path)
    402                 calculate_path();
    403         return prefix;
     401    if (!module_search_path)
     402        calculate_path();
     403    return prefix;
    404404}
    405405
     
    407407Py_GetExecPrefix(void)
    408408{
    409         return Py_GetPrefix();
     409    return Py_GetPrefix();
    410410}
    411411
     
    413413Py_GetProgramFullPath(void)
    414414{
    415         if (!module_search_path)
    416                 calculate_path();
    417         return progpath;
    418 }
     415    if (!module_search_path)
     416        calculate_path();
     417    return progpath;
     418}
Note: See TracChangeset for help on using the changeset viewer.