Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a63d9f4

Browse files
committed
As a side effect of calling PySys_SetArgv (setpythonargv), the
directory containing argv[0] is inserted in front of sys.path. If argv[0] contains no directory, an empty string is inserted. If argv is empty, nothing happens.
1 parent 3b8e20d commit a63d9f4

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Python/sysmodule.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,23 @@ setpythonargv(argc, argv)
364364
fatal("no mem for sys.argv");
365365
if (sysset("argv", av) != 0)
366366
fatal("can't assign sys.argv");
367+
if (argc > 0) {
368+
object *path = sysget("path");
369+
if (path != NULL) {
370+
char *p = strrchr(argv[0], SEP);
371+
int n;
372+
object *a;
373+
if (p == NULL)
374+
n = 0;
375+
else
376+
n = p + 1 - argv[0];
377+
a = newsizedstringobject(argv[0], n);
378+
if (a == NULL)
379+
fatal("no mem for sys.path insertion");
380+
if (inslistitem(path, 0, a) < 0)
381+
fatal("sys.path.insert(0) failed");
382+
DECREF(a);
383+
}
384+
}
367385
DECREF(av);
368386
}

0 commit comments

Comments
 (0)