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

Skip to content

Commit 71d305c

Browse files
committed
Merged revisions 81400 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r81400 | antoine.pitrou | 2010-05-21 19:25:34 +0200 (ven., 21 mai 2010) | 12 lines Merged revisions 81398 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r81398 | antoine.pitrou | 2010-05-21 19:12:38 +0200 (ven., 21 mai 2010) | 6 lines Issue #5753: A new C API function, :cfunc:`PySys_SetArgvEx`, allows embedders of the interpreter to set sys.argv without also modifying sys.path. This helps fix `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_. ........ ................
1 parent 06f018d commit 71d305c

4 files changed

Lines changed: 50 additions & 7 deletions

File tree

Doc/c-api/init.rst

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Initialization, Finalization, and Threads
2222
module: sys
2323
triple: module; search; path
2424
single: PySys_SetArgv()
25+
single: PySys_SetArgvEx()
2526
single: Py_Finalize()
2627

2728
Initialize the Python interpreter. In an application embedding Python, this
@@ -31,7 +32,7 @@ Initialization, Finalization, and Threads
3132
the table of loaded modules (``sys.modules``), and creates the fundamental
3233
modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes
3334
the module search path (``sys.path``). It does not set ``sys.argv``; use
34-
:cfunc:`PySys_SetArgv` for that. This is a no-op when called for a second time
35+
:cfunc:`PySys_SetArgvEx` for that. This is a no-op when called for a second time
3536
(without calling :cfunc:`Py_Finalize` first). There is no return value; it is a
3637
fatal error if the initialization fails.
3738

@@ -344,7 +345,7 @@ Initialization, Finalization, and Threads
344345
``sys.version``.
345346

346347

347-
.. cfunction:: void PySys_SetArgv(int argc, wchar_t **argv)
348+
.. cfunction:: void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
348349

349350
.. index::
350351
single: main()
@@ -359,14 +360,41 @@ Initialization, Finalization, and Threads
359360
string. If this function fails to initialize :data:`sys.argv`, a fatal
360361
condition is signalled using :cfunc:`Py_FatalError`.
361362

362-
This function also prepends the executed script's path to :data:`sys.path`.
363-
If no script is executed (in the case of calling ``python -c`` or just the
364-
interactive interpreter), the empty string is used instead.
363+
If *updatepath* is zero, this is all the function does. If *updatepath*
364+
is non-zero, the function also modifies :data:`sys.path` according to the
365+
following algorithm:
366+
367+
- If the name of an existing script is passed in ``argv[0]``, the absolute
368+
path of the directory where the script is located is prepended to
369+
:data:`sys.path`.
370+
- Otherwise (that is, if *argc* is 0 or ``argv[0]`` doesn't point
371+
to an existing file name), an empty string is prepended to
372+
:data:`sys.path`, which is the same as prepending the current working
373+
directory (``"."``).
374+
375+
.. note::
376+
It is recommended that applications embedding the Python interpreter
377+
for purposes other than executing a single script pass 0 as *updatepath*,
378+
and update :data:`sys.path` themselves if desired.
379+
See `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
380+
381+
On versions before 3.1.3, you can achieve the same effect by manually
382+
popping the first :data:`sys.path` element after having called
383+
:cfunc:`PySys_SetArgv`, for example using::
384+
385+
PyRun_SimpleString("import sys; sys.path.pop(0)\n");
386+
387+
.. versionadded:: 3.1.3
365388

366389
.. XXX impl. doesn't seem consistent in allowing 0/NULL for the params;
367390
check w/ Guido.
368391
369392
393+
.. cfunction:: void PySys_SetArgv(int argc, wchar_t **argv)
394+
395+
This function works like :cfunc:`PySys_SetArgv` with *updatepath* set to 1.
396+
397+
370398
.. cfunction:: void Py_SetPythonHome(wchar_t *home)
371399

372400
Set the default "home" directory, that is, the location of the standard

Include/sysmodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010
PyAPI_FUNC(PyObject *) PySys_GetObject(const char *);
1111
PyAPI_FUNC(int) PySys_SetObject(const char *, PyObject *);
1212
PyAPI_FUNC(void) PySys_SetArgv(int, wchar_t **);
13+
PyAPI_FUNC(void) PySys_SetArgvEx(int, wchar_t **, int);
1314
PyAPI_FUNC(void) PySys_SetPath(const wchar_t *);
1415

1516
PyAPI_FUNC(void) PySys_WriteStdout(const char *format, ...)

Misc/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ Core and Builtins
4343

4444
- Issue #7072: isspace(0xa0) is true on Mac OS X
4545

46+
C-API
47+
-----
48+
49+
- Issue #5753: A new C API function, :cfunc:`PySys_SetArgvEx`, allows
50+
embedders of the interpreter to set sys.argv without also modifying
51+
sys.path. This helps fix `CVE-2008-5983
52+
<http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
53+
4654
Library
4755
-------
4856

Python/sysmodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ _wrealpath(const wchar_t *path, wchar_t *resolved_path)
15551555
#endif
15561556

15571557
void
1558-
PySys_SetArgv(int argc, wchar_t **argv)
1558+
PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
15591559
{
15601560
#if defined(HAVE_REALPATH)
15611561
wchar_t fullpath[MAXPATHLEN];
@@ -1568,7 +1568,7 @@ PySys_SetArgv(int argc, wchar_t **argv)
15681568
Py_FatalError("no mem for sys.argv");
15691569
if (PySys_SetObject("argv", av) != 0)
15701570
Py_FatalError("can't assign sys.argv");
1571-
if (path != NULL) {
1571+
if (updatepath && path != NULL) {
15721572
wchar_t *argv0 = argv[0];
15731573
wchar_t *p = NULL;
15741574
Py_ssize_t n = 0;
@@ -1655,6 +1655,12 @@ PySys_SetArgv(int argc, wchar_t **argv)
16551655
Py_DECREF(av);
16561656
}
16571657

1658+
void
1659+
PySys_SetArgv(int argc, wchar_t **argv)
1660+
{
1661+
PySys_SetArgvEx(argc, argv, 1);
1662+
}
1663+
16581664
/* Reimplementation of PyFile_WriteString() no calling indirectly
16591665
PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
16601666

0 commit comments

Comments
 (0)