Kaydet (Commit) d26c18ad authored tarafından Nick Coghlan's avatar Nick Coghlan

Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module…

Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
üst 46e63805
...@@ -95,8 +95,9 @@ source. ...@@ -95,8 +95,9 @@ source.
file is not available. file is not available.
If this option is given, the first element of :data:`sys.argv` will be the If this option is given, the first element of :data:`sys.argv` will be the
full path to the module file. As with the :option:`-c` option, the current full path to the module file (while the module file is being located, the
directory will be added to the start of :data:`sys.path`. first element will be set to ``"-m"``). As with the :option:`-c` option,
the current directory will be added to the start of :data:`sys.path`.
Many standard library modules contain code that is invoked on their execution Many standard library modules contain code that is invoked on their execution
as a script. An example is the :mod:`timeit` module:: as a script. An example is the :mod:`timeit` module::
......
...@@ -86,9 +86,9 @@ def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): ...@@ -86,9 +86,9 @@ def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
# zip_file.close() # zip_file.close()
return zip_name, os.path.join(zip_name, name_in_zip) return zip_name, os.path.join(zip_name, name_in_zip)
def make_pkg(pkg_dir): def make_pkg(pkg_dir, init_source=''):
os.mkdir(pkg_dir) os.mkdir(pkg_dir)
make_script(pkg_dir, '__init__', '') make_script(pkg_dir, '__init__', init_source)
def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
source, depth=1, compiled=False): source, depth=1, compiled=False):
......
...@@ -419,21 +419,32 @@ elif sys.platform != 'darwin': ...@@ -419,21 +419,32 @@ elif sys.platform != 'darwin':
SAVEDCWD = os.getcwd() SAVEDCWD = os.getcwd()
@contextlib.contextmanager @contextlib.contextmanager
def temp_cwd(name='tempcwd', quiet=False): def temp_cwd(name='tempcwd', quiet=False, path=None):
""" """
Context manager that creates a temporary directory and set it as CWD. Context manager that temporarily changes the CWD.
The new CWD is created in the current directory and it's named *name*. An existing path may be provided as *path*, in which case this
If *quiet* is False (default) and it's not possible to create or change function makes no changes to the file system.
the CWD, an error is raised. If it's True, only a warning is raised
and the original CWD is used. Otherwise, the new CWD is created in the current directory and it's
named *name*. If *quiet* is False (default) and it's not possible to
create or change the CWD, an error is raised. If it's True, only a
warning is raised and the original CWD is used.
""" """
saved_dir = os.getcwd() saved_dir = os.getcwd()
is_temporary = False is_temporary = False
if path is None:
path = name
try:
os.mkdir(name)
is_temporary = True
except OSError:
if not quiet:
raise
warnings.warn('tests may fail, unable to create temp CWD ' + name,
RuntimeWarning, stacklevel=3)
try: try:
os.mkdir(name) os.chdir(path)
os.chdir(name)
is_temporary = True
except OSError: except OSError:
if not quiet: if not quiet:
raise raise
......
# Tests invocation of the interpreter with various command line arguments # Tests invocation of the interpreter with various command line arguments
# All tests are executed with environment variables ignored # Most tests are executed with environment variables ignored
# See test_cmd_line_script.py for testing of script execution # See test_cmd_line_script.py for testing of script execution
import test.support, unittest import test.support, unittest
...@@ -7,10 +7,6 @@ import os ...@@ -7,10 +7,6 @@ import os
import sys import sys
from test.script_helper import spawn_python, kill_python, python_exit_code from test.script_helper import spawn_python, kill_python, python_exit_code
# XXX (ncoghlan): there are assorted gratuitous inconsistencies between the
# support code in the Py3k version and the 2.x version that unnecessarily
# complicate test suite merges. See issue 7331
# spawn_python normally enforces use of -E to avoid environmental effects # spawn_python normally enforces use of -E to avoid environmental effects
# but one test checks PYTHONPATH behaviour explicitly # but one test checks PYTHONPATH behaviour explicitly
# XXX (ncoghlan): Give script_helper.spawn_python an option to switch # XXX (ncoghlan): Give script_helper.spawn_python an option to switch
......
...@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 2? ...@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 2?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #8202: sys.argv[0] is now set to '-m' instead of '-c' when searching
for the module file to be executed with the -m command line option.
- Issue #9599: Create PySys_FormatStdout() and PySys_FormatStderr() functions - Issue #9599: Create PySys_FormatStdout() and PySys_FormatStderr() functions
to write a message formatted by PyUnicode_FromFormatV() to sys.stdout and to write a message formatted by PyUnicode_FromFormatV() to sys.stdout and
sys.stderr. sys.stderr.
......
...@@ -604,10 +604,9 @@ Py_Main(int argc, wchar_t **argv) ...@@ -604,10 +604,9 @@ Py_Main(int argc, wchar_t **argv)
} }
if (module != NULL) { if (module != NULL) {
/* Backup _PyOS_optind and force sys.argv[0] = '-c' /* Backup _PyOS_optind and force sys.argv[0] = '-m'*/
so that PySys_SetArgv correctly sets sys.path[0] to ''*/
_PyOS_optind--; _PyOS_optind--;
argv[_PyOS_optind] = L"-c"; argv[_PyOS_optind] = L"-m";
} }
PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
......
...@@ -1723,6 +1723,10 @@ _wrealpath(const wchar_t *path, wchar_t *resolved_path) ...@@ -1723,6 +1723,10 @@ _wrealpath(const wchar_t *path, wchar_t *resolved_path)
} }
#endif #endif
#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
(argc > 0 && argv0 != NULL && \
wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
void void
PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
{ {
...@@ -1747,7 +1751,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) ...@@ -1747,7 +1751,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
wchar_t link[MAXPATHLEN+1]; wchar_t link[MAXPATHLEN+1];
wchar_t argv0copy[2*MAXPATHLEN+1]; wchar_t argv0copy[2*MAXPATHLEN+1];
int nr = 0; int nr = 0;
if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
nr = _Py_wreadlink(argv0, link, MAXPATHLEN); nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
if (nr > 0) { if (nr > 0) {
/* It's a symlink */ /* It's a symlink */
...@@ -1772,7 +1776,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) ...@@ -1772,7 +1776,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
} }
#endif /* HAVE_READLINK */ #endif /* HAVE_READLINK */
#if SEP == '\\' /* Special case for MS filename syntax */ #if SEP == '\\' /* Special case for MS filename syntax */
if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
wchar_t *q; wchar_t *q;
#if defined(MS_WINDOWS) && !defined(MS_WINCE) #if defined(MS_WINDOWS) && !defined(MS_WINCE)
/* This code here replaces the first element in argv with the full /* This code here replaces the first element in argv with the full
...@@ -1798,7 +1802,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) ...@@ -1798,7 +1802,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
} }
} }
#else /* All other filename syntaxes */ #else /* All other filename syntaxes */
if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) { if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
#if defined(HAVE_REALPATH) #if defined(HAVE_REALPATH)
if (_wrealpath(argv0, fullpath)) { if (_wrealpath(argv0, fullpath)) {
argv0 = fullpath; argv0 = fullpath;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment