aa2fa3b
diff -up Python-3.1.2/Doc/c-api/init.rst.CVE-2008-5983 Python-3.1.2/Doc/c-api/init.rst
aa2fa3b
--- Python-3.1.2/Doc/c-api/init.rst.CVE-2008-5983	2010-01-09 13:48:46.000000000 -0500
aa2fa3b
+++ Python-3.1.2/Doc/c-api/init.rst	2010-06-04 15:19:26.724089244 -0400
aa2fa3b
@@ -22,6 +22,7 @@ Initialization, Finalization, and Thread
aa2fa3b
       module: sys
aa2fa3b
       triple: module; search; path
aa2fa3b
       single: PySys_SetArgv()
aa2fa3b
+      single: PySys_SetArgvEx()
aa2fa3b
       single: Py_Finalize()
aa2fa3b
 
aa2fa3b
    Initialize the Python interpreter.  In an application embedding  Python, this
aa2fa3b
@@ -31,7 +32,7 @@ Initialization, Finalization, and Thread
aa2fa3b
    the table of loaded modules (``sys.modules``), and creates the fundamental
aa2fa3b
    modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`.  It also initializes
aa2fa3b
    the module search path (``sys.path``). It does not set ``sys.argv``; use
aa2fa3b
-   :cfunc:`PySys_SetArgv` for that.  This is a no-op when called for a second time
aa2fa3b
+   :cfunc:`PySys_SetArgvEx` for that.  This is a no-op when called for a second time
aa2fa3b
    (without calling :cfunc:`Py_Finalize` first).  There is no return value; it is a
aa2fa3b
    fatal error if the initialization fails.
aa2fa3b
 
aa2fa3b
@@ -344,7 +345,7 @@ Initialization, Finalization, and Thread
aa2fa3b
    ``sys.version``.
aa2fa3b
 
aa2fa3b
 
aa2fa3b
-.. cfunction:: void PySys_SetArgv(int argc, wchar_t **argv)
aa2fa3b
+.. cfunction:: void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
aa2fa3b
 
aa2fa3b
    .. index::
aa2fa3b
       single: main()
aa2fa3b
@@ -359,14 +360,41 @@ Initialization, Finalization, and Thread
aa2fa3b
    string.  If this function fails to initialize :data:`sys.argv`, a fatal
aa2fa3b
    condition is signalled using :cfunc:`Py_FatalError`.
aa2fa3b
 
aa2fa3b
-   This function also prepends the executed script's path to :data:`sys.path`.
aa2fa3b
-   If no script is executed (in the case of calling ``python -c`` or just the
aa2fa3b
-   interactive interpreter), the empty string is used instead.
aa2fa3b
+   If *updatepath* is zero, this is all the function does.  If *updatepath*
aa2fa3b
+   is non-zero, the function also modifies :data:`sys.path` according to the
aa2fa3b
+   following algorithm:
aa2fa3b
+
aa2fa3b
+   - If the name of an existing script is passed in ``argv[0]``, the absolute
aa2fa3b
+     path of the directory where the script is located is prepended to
aa2fa3b
+     :data:`sys.path`.
aa2fa3b
+   - Otherwise (that is, if *argc* is 0 or ``argv[0]`` doesn't point
aa2fa3b
+     to an existing file name), an empty string is prepended to
aa2fa3b
+     :data:`sys.path`, which is the same as prepending the current working
aa2fa3b
+     directory (``"."``).
aa2fa3b
+
aa2fa3b
+   .. note::
aa2fa3b
+      It is recommended that applications embedding the Python interpreter
aa2fa3b
+      for purposes other than executing a single script pass 0 as *updatepath*,
aa2fa3b
+      and update :data:`sys.path` themselves if desired.
aa2fa3b
+      See `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
aa2fa3b
+
aa2fa3b
+      On versions before 3.1.3, you can achieve the same effect by manually
aa2fa3b
+      popping the first :data:`sys.path` element after having called
aa2fa3b
+      :cfunc:`PySys_SetArgv`, for example using::
aa2fa3b
+
aa2fa3b
+         PyRun_SimpleString("import sys; sys.path.pop(0)\n");
aa2fa3b
+
aa2fa3b
+   .. versionadded:: 3.1.3
aa2fa3b
 
aa2fa3b
    .. XXX impl. doesn't seem consistent in allowing 0/NULL for the params;
aa2fa3b
       check w/ Guido.
aa2fa3b
 
aa2fa3b
 
aa2fa3b
+.. cfunction:: void PySys_SetArgv(int argc, wchar_t **argv)
aa2fa3b
+
aa2fa3b
+   This function works like :cfunc:`PySys_SetArgv` with *updatepath* set to 1.
aa2fa3b
+
aa2fa3b
+
aa2fa3b
 .. cfunction:: void Py_SetPythonHome(wchar_t *home)
aa2fa3b
 
aa2fa3b
    Set the default "home" directory, that is, the location of the standard
aa2fa3b
diff -up Python-3.1.2/Include/sysmodule.h.CVE-2008-5983 Python-3.1.2/Include/sysmodule.h
aa2fa3b
--- Python-3.1.2/Include/sysmodule.h.CVE-2008-5983	2008-04-13 09:53:33.000000000 -0400
aa2fa3b
+++ Python-3.1.2/Include/sysmodule.h	2010-06-04 15:19:26.721088968 -0400
aa2fa3b
@@ -10,6 +10,7 @@ extern "C" {
aa2fa3b
 PyAPI_FUNC(PyObject *) PySys_GetObject(const char *);
aa2fa3b
 PyAPI_FUNC(int) PySys_SetObject(const char *, PyObject *);
aa2fa3b
 PyAPI_FUNC(void) PySys_SetArgv(int, wchar_t **);
aa2fa3b
+PyAPI_FUNC(void) PySys_SetArgvEx(int, wchar_t **, int);
aa2fa3b
 PyAPI_FUNC(void) PySys_SetPath(const wchar_t *);
aa2fa3b
 
aa2fa3b
 PyAPI_FUNC(void) PySys_WriteStdout(const char *format, ...)
aa2fa3b
diff -up Python-3.1.2/Misc/NEWS.CVE-2008-5983 Python-3.1.2/Misc/NEWS
aa2fa3b
diff -up Python-3.1.2/Python/sysmodule.c.CVE-2008-5983 Python-3.1.2/Python/sysmodule.c
aa2fa3b
--- Python-3.1.2/Python/sysmodule.c.CVE-2008-5983	2010-06-04 15:19:26.000000000 -0400
aa2fa3b
+++ Python-3.1.2/Python/sysmodule.c	2010-06-04 15:20:59.932964188 -0400
aa2fa3b
@@ -1561,7 +1561,7 @@ _wrealpath(const wchar_t *path, wchar_t 
aa2fa3b
 #endif
aa2fa3b
 
aa2fa3b
 void
aa2fa3b
-PySys_SetArgv(int argc, wchar_t **argv)
aa2fa3b
+PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
aa2fa3b
 {
aa2fa3b
 #if defined(HAVE_REALPATH)
aa2fa3b
 	wchar_t fullpath[MAXPATHLEN];
aa2fa3b
@@ -1574,7 +1574,7 @@ PySys_SetArgv(int argc, wchar_t **argv)
aa2fa3b
 		Py_FatalError("no mem for sys.argv");
aa2fa3b
 	if (PySys_SetObject("argv", av) != 0)
aa2fa3b
 		Py_FatalError("can't assign sys.argv");
aa2fa3b
-	if (path != NULL) {
aa2fa3b
+	if (updatepath && path != NULL) {
aa2fa3b
 		wchar_t *argv0 = argv[0];
aa2fa3b
 		wchar_t *p = NULL;
aa2fa3b
 		Py_ssize_t n = 0;
aa2fa3b
@@ -1661,6 +1661,12 @@ PySys_SetArgv(int argc, wchar_t **argv)
aa2fa3b
 	Py_DECREF(av);
aa2fa3b
 }
aa2fa3b
 
aa2fa3b
+void
aa2fa3b
+PySys_SetArgv(int argc, wchar_t **argv)
aa2fa3b
+{
aa2fa3b
+	PySys_SetArgvEx(argc, argv, 1);
aa2fa3b
+}
aa2fa3b
+
aa2fa3b
 
aa2fa3b
 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
aa2fa3b
    Adapted from code submitted by Just van Rossum.