Mercurial > cpython-lru_cache-weakref
comparison Python/sysmodule.c @ 5340:db673e98f28d legacy-trunk
Expand one level of symbolic link in sys.argv[0] before inserting its
dirname in sys.path. This means that you can create a symbolic link
foo in /usr/local/bin pointing to /usr/yourname/src/foo/foo.py, and
then invoking foo will insert /usr/yourname/src/foo in sys.path, not
/usr/local/bin. This makes it easier to have multifile programs
(before, the program would have to do an os.readlink(sys.argv[0])
itself and insert the resulting directory in sys.path -- Grail does
this).
Note that the expansion is only used for sys.path; sys.argv[0] is
still the original, unadorned filename (/usr/local/bin/foo in the
example).
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 25 Apr 1997 15:38:31 +0000 |
| parents | 7a9cac7cb910 |
| children | 424922afc1bb |
comparison
equal
deleted
inserted
replaced
| 5339:b0a3140cc9b1 | 5340:db673e98f28d |
|---|---|
| 51 #include "modsupport.h" | 51 #include "modsupport.h" |
| 52 #include "osdefs.h" | 52 #include "osdefs.h" |
| 53 | 53 |
| 54 object *sys_trace, *sys_profile; | 54 object *sys_trace, *sys_profile; |
| 55 int sys_checkinterval = 10; | 55 int sys_checkinterval = 10; |
| 56 | |
| 57 #if HAVE_UNISTD_H | |
| 58 #include <unistd.h> | |
| 59 #endif | |
| 56 | 60 |
| 57 static object *sysdict; | 61 static object *sysdict; |
| 58 | 62 |
| 59 #ifdef MS_COREDLL | 63 #ifdef MS_COREDLL |
| 60 extern void *PyWin_DLLhModule; | 64 extern void *PyWin_DLLhModule; |
| 385 if (av == NULL) | 389 if (av == NULL) |
| 386 fatal("no mem for sys.argv"); | 390 fatal("no mem for sys.argv"); |
| 387 if (sysset("argv", av) != 0) | 391 if (sysset("argv", av) != 0) |
| 388 fatal("can't assign sys.argv"); | 392 fatal("can't assign sys.argv"); |
| 389 if (path != NULL) { | 393 if (path != NULL) { |
| 394 char *argv0 = argv[0]; | |
| 390 char *p = NULL; | 395 char *p = NULL; |
| 391 int n = 0; | 396 int n = 0; |
| 392 object *a; | 397 object *a; |
| 398 #ifdef HAVE_READLINK | |
| 399 char link[MAXPATHLEN+1]; | |
| 400 char argv0copy[2*MAXPATHLEN+1]; | |
| 401 int nr = 0; | |
| 402 if (argc > 0 && argv0 != NULL) | |
| 403 nr = readlink(argv0, link, MAXPATHLEN); | |
| 404 if (nr > 0) { | |
| 405 /* It's a symlink */ | |
| 406 link[nr] = '\0'; | |
| 407 if (link[0] == SEP) | |
| 408 argv0 = link; /* Link to absolute path */ | |
| 409 else if (strchr(link, SEP) == NULL) | |
| 410 ; /* Link without path */ | |
| 411 else { | |
| 412 /* Must join(dirname(argv0), link) */ | |
| 413 char *q = strrchr(argv0, SEP); | |
| 414 if (q == NULL) | |
| 415 argv0 = link; /* argv0 without path */ | |
| 416 else { | |
| 417 /* Must make a copy */ | |
| 418 strcpy(argv0copy, argv0); | |
| 419 q = strrchr(argv0copy, SEP); | |
| 420 strcpy(q+1, link); | |
| 421 argv0 = argv0copy; | |
| 422 } | |
| 423 } | |
| 424 } | |
| 425 #endif /* HAVE_READLINK */ | |
| 393 #if SEP == '\\' /* Special case for MS filename syntax */ | 426 #if SEP == '\\' /* Special case for MS filename syntax */ |
| 394 if (argc > 0 && argv[0] != NULL) { | 427 if (argc > 0 && argv0 != NULL) { |
| 395 char *q; | 428 char *q; |
| 396 p = strrchr(argv[0], SEP); | 429 p = strrchr(argv0, SEP); |
| 397 /* Test for alternate separator */ | 430 /* Test for alternate separator */ |
| 398 q = strrchr(p ? p : argv[0], '/'); | 431 q = strrchr(p ? p : argv0, '/'); |
| 399 if (q != NULL) | 432 if (q != NULL) |
| 400 p = q; | 433 p = q; |
| 401 if (p != NULL) { | 434 if (p != NULL) { |
| 402 n = p + 1 - argv[0]; | 435 n = p + 1 - argv0; |
| 403 if (n > 1 && p[-1] != ':') | 436 if (n > 1 && p[-1] != ':') |
| 404 n--; /* Drop trailing separator */ | 437 n--; /* Drop trailing separator */ |
| 405 } | 438 } |
| 406 } | 439 } |
| 407 #else /* All other filename syntaxes */ | 440 #else /* All other filename syntaxes */ |
| 408 if (argc > 0 && argv[0] != NULL) | 441 if (argc > 0 && argv0 != NULL) |
| 409 p = strrchr(argv[0], SEP); | 442 p = strrchr(argv0, SEP); |
| 410 if (p != NULL) { | 443 if (p != NULL) { |
| 411 n = p + 1 - argv[0]; | 444 n = p + 1 - argv0; |
| 412 #if SEP == '/' /* Special case for Unix filename syntax */ | 445 #if SEP == '/' /* Special case for Unix filename syntax */ |
| 413 if (n > 1) | 446 if (n > 1) |
| 414 n--; /* Drop trailing separator */ | 447 n--; /* Drop trailing separator */ |
| 415 #endif /* Unix */ | 448 #endif /* Unix */ |
| 416 } | 449 } |
| 417 #endif /* All others */ | 450 #endif /* All others */ |
| 418 a = newsizedstringobject(argv[0], n); | 451 a = newsizedstringobject(argv0, n); |
| 419 if (a == NULL) | 452 if (a == NULL) |
| 420 fatal("no mem for sys.path insertion"); | 453 fatal("no mem for sys.path insertion"); |
| 421 if (inslistitem(path, 0, a) < 0) | 454 if (inslistitem(path, 0, a) < 0) |
| 422 fatal("sys.path.insert(0) failed"); | 455 fatal("sys.path.insert(0) failed"); |
| 423 DECREF(a); | 456 DECREF(a); |
