Create a subfunction for PySys_SetArgvEx()

Create sys_update_path() static function. Do nothing if argc==0.
This commit is contained in:
Victor Stinner 2010-10-06 22:44:06 +00:00
parent 7980eaa98d
commit c08ec9fdba
1 changed files with 104 additions and 88 deletions

View File

@ -1685,106 +1685,122 @@ _wrealpath(const wchar_t *path, wchar_t *resolved_path)
#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
(argc > 0 && argv0 != NULL && \
wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
void
PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
static void
sys_update_path(int argc, wchar_t **argv)
{
wchar_t *argv0;
wchar_t *p = NULL;
Py_ssize_t n = 0;
PyObject *a;
PyObject *path;
#ifdef HAVE_READLINK
extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t);
wchar_t link[MAXPATHLEN+1];
wchar_t argv0copy[2*MAXPATHLEN+1];
int nr = 0;
#endif
#if defined(HAVE_REALPATH)
wchar_t fullpath[MAXPATHLEN];
#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
wchar_t fullpath[MAX_PATH];
#endif
path = PySys_GetObject("path");
if (path == NULL)
return;
if (argc == 0)
return;
argv0 = argv[0];
#ifdef HAVE_READLINK
if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
if (nr > 0) {
/* It's a symlink */
link[nr] = '\0';
if (link[0] == SEP)
argv0 = link; /* Link to absolute path */
else if (wcschr(link, SEP) == NULL)
; /* Link without path */
else {
/* Must join(dirname(argv0), link) */
wchar_t *q = wcsrchr(argv0, SEP);
if (q == NULL)
argv0 = link; /* argv0 without path */
else {
/* Must make a copy */
wcscpy(argv0copy, argv0);
q = wcsrchr(argv0copy, SEP);
wcscpy(q+1, link);
argv0 = argv0copy;
}
}
}
#endif /* HAVE_READLINK */
#if SEP == '\\' /* Special case for MS filename syntax */
if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
wchar_t *q;
#if defined(MS_WINDOWS) && !defined(MS_WINCE)
/* This code here replaces the first element in argv with the full
path that it represents. Under CE, there are no relative paths so
the argument must be the full path anyway. */
wchar_t *ptemp;
if (GetFullPathNameW(argv0,
sizeof(fullpath)/sizeof(fullpath[0]),
fullpath,
&ptemp)) {
argv0 = fullpath;
}
#endif
p = wcsrchr(argv0, SEP);
/* Test for alternate separator */
q = wcsrchr(p ? p : argv0, '/');
if (q != NULL)
p = q;
if (p != NULL) {
n = p + 1 - argv0;
if (n > 1 && p[-1] != ':')
n--; /* Drop trailing separator */
}
}
#else /* All other filename syntaxes */
if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
#if defined(HAVE_REALPATH)
if (_wrealpath(argv0, fullpath)) {
argv0 = fullpath;
}
#endif
p = wcsrchr(argv0, SEP);
}
if (p != NULL) {
n = p + 1 - argv0;
#if SEP == '/' /* Special case for Unix filename syntax */
if (n > 1)
n--; /* Drop trailing separator */
#endif /* Unix */
}
#endif /* All others */
a = PyUnicode_FromWideChar(argv0, n);
if (a == NULL)
Py_FatalError("no mem for sys.path insertion");
if (PyList_Insert(path, 0, a) < 0)
Py_FatalError("sys.path.insert(0) failed");
Py_DECREF(a);
}
void
PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
{
PyObject *av = makeargvobject(argc, argv);
PyObject *path = PySys_GetObject("path");
if (av == NULL)
Py_FatalError("no mem for sys.argv");
if (PySys_SetObject("argv", av) != 0)
Py_FatalError("can't assign sys.argv");
if (updatepath && path != NULL) {
wchar_t *argv0 = argv[0];
wchar_t *p = NULL;
Py_ssize_t n = 0;
PyObject *a;
extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t);
#ifdef HAVE_READLINK
wchar_t link[MAXPATHLEN+1];
wchar_t argv0copy[2*MAXPATHLEN+1];
int nr = 0;
if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
if (nr > 0) {
/* It's a symlink */
link[nr] = '\0';
if (link[0] == SEP)
argv0 = link; /* Link to absolute path */
else if (wcschr(link, SEP) == NULL)
; /* Link without path */
else {
/* Must join(dirname(argv0), link) */
wchar_t *q = wcsrchr(argv0, SEP);
if (q == NULL)
argv0 = link; /* argv0 without path */
else {
/* Must make a copy */
wcscpy(argv0copy, argv0);
q = wcsrchr(argv0copy, SEP);
wcscpy(q+1, link);
argv0 = argv0copy;
}
}
}
#endif /* HAVE_READLINK */
#if SEP == '\\' /* Special case for MS filename syntax */
if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
wchar_t *q;
#if defined(MS_WINDOWS) && !defined(MS_WINCE)
/* This code here replaces the first element in argv with the full
path that it represents. Under CE, there are no relative paths so
the argument must be the full path anyway. */
wchar_t *ptemp;
if (GetFullPathNameW(argv0,
sizeof(fullpath)/sizeof(fullpath[0]),
fullpath,
&ptemp)) {
argv0 = fullpath;
}
#endif
p = wcsrchr(argv0, SEP);
/* Test for alternate separator */
q = wcsrchr(p ? p : argv0, '/');
if (q != NULL)
p = q;
if (p != NULL) {
n = p + 1 - argv0;
if (n > 1 && p[-1] != ':')
n--; /* Drop trailing separator */
}
}
#else /* All other filename syntaxes */
if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
#if defined(HAVE_REALPATH)
if (_wrealpath(argv0, fullpath)) {
argv0 = fullpath;
}
#endif
p = wcsrchr(argv0, SEP);
}
if (p != NULL) {
n = p + 1 - argv0;
#if SEP == '/' /* Special case for Unix filename syntax */
if (n > 1)
n--; /* Drop trailing separator */
#endif /* Unix */
}
#endif /* All others */
a = PyUnicode_FromWideChar(argv0, n);
if (a == NULL)
Py_FatalError("no mem for sys.path insertion");
if (PyList_Insert(path, 0, a) < 0)
Py_FatalError("sys.path.insert(0) failed");
Py_DECREF(a);
}
Py_DECREF(av);
if (updatepath)
sys_update_path(argc, argv);
}
void